Error Creating Window Handle

Brain-BulbIt starts with “Can not create Window Handle”… and continuous random crashes. You find out your app reaches the max of 10000 user objects. (Just check the task manager, you’ll see I’m right.)

So, you’ve been using your own usercontrols on a flowlayoutpanel; you add, you remove, you clear… however the user objects count keeps growing and something is leaking somewhere…

You find that .controls.clear() does not work, you have to dispose() of all the objects first. Okay, do that, but it still doesn’t work.
So what’s the trick?
Simply remove first, before the dispose, and do this one at a time.

Just use the following routine:
ClearFlowLayoutPanel(myFlowLayoutPanel)
instead of
myFlowLayoutPanel.Controls.Clear()

Public Sub ClearFlowLayoutPanel(cFlowLayoutPanel As FlowLayoutPanel)
 Do While (cFlowLayoutPanel.Controls.Count > 0)
  Using controltoremove = cFlowLayoutPanel.Controls(0)
   cFlowLayoutPanel.Controls.Remove(controltoremove)
   controltoremove.Dispose()
  End Using
 Loop
 GC.Collect()
 GC.WaitForPendingFinalizers()
 cFlowLayoutPanel.Controls.Clear()
End Sub

Good Luck! …or ask My Brain.

sources or more info:
https://blogs.technet.microsoft.com/markrussinovich/2010/02/24/pushing-the-limits-of-windows-user-and-gdi-objects-part-1/
http://stackoverflow.com/questions/1378668/clearing-controls-from-flowlayoutpanel-not-calling-destructors/

 

VB.Net create mdb with a password

f_3dh_partyWhen developing an application with visual basic, you may want to store data. So you create a database during runtime, for example with the following piece of code, as many websites show on various forums:

Dim strMDBPath As String = "C:\SomePath\MyTestDB.mdb"
Dim cat As New ADOX.Catalog()
 cat.Create(Convert.ToString("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=") & strMDBPath)
 cat = Nothing

Next you wish to protect your data, so only your application can open the database. You google again, and find the following solution:

Dim cn As OleDbConnection = New OleDbConnection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strMDBPath & ";Mode=Share Deny Read|Share Deny Write;"
cn.Open()
Dim cmdpw As OleDbCommand = New OleDbCommand
cmdpw.Connection = cn
cmdpw.CommandText = "ALTER DATABASE PASSWORD [MYNEWPASSWORD] null"
cmdpw.ExecuteNonQuery()
cn.Close()

Unfortunately, now you are stuck with an error you can not solve:
You attempted to open a database that is already opened by user ‘Admin’ on machine ‘YOURPCNAME’. Try again when the database is available.
Creating the database seems to keep the connection open and you cannot close it to execute the password change on an exclusive connection.

The answer and solution is simple: Set the password in the connection string in the first piece of code when creating the database! The attempt to ALTER DATABASE PASSWORD is no longer needed and you can forget about that…
So:

Dim strMDBPath As String = "C:\SomePath\MyTestDB.mdb"
Dim cat As New ADOX.Catalog()
cat.Create(Convert.ToString("Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=MYNEWPASSWORD;Data Source=") & strMDBPath)
cat = Nothing

Enjoy this free advice from My Brain!

PHP (un)serialize and VB.net

SBrain-Bulberialization is changing any object, array or variable into a simple string, so it can be easily transfered over a network or stored into a database. Plus the change back into the original of course. There are many options available, and your best choice is using XML or JSON.

However, if you happen to have a PHP object serialized with the php function… you have a problem when you receive this serialized string from a web request and need to process the data and use the object in a windows visual basic .net application.

So what does My Brain do?
First search for a class or other code sample to do this. But when none is available? Simply write code and start developing my own solution.

Need something like this? Contact My Brain.

Self-Elevating App

Say What?aaaaaaa

Yes: My Brain wrote a Self-Elevating App.

I have been writing a Windows Application (32-bit x86 executable) in Visual Studio 2010, that uses the HKEY_LOCAL_MACHINE/Software/AppName registry key to save some basic settings. After some strange accesss errors I figured out that you need administrative access to actually be able to do this. Continue reading Self-Elevating App