Obtaining the IntPtr to/of an unmanaged resource.
I want to make something clear, I'm not a fan of using COM objects from managed code unless I have no choice as usually dictated by the situation here at work...

Now for my question:
I'm using the Microsoft Access 8.0 Object Library from inside VB.NET 2003. Correct me if I'm wrong but this is an example of Managed Code utilizing an UnManaged resource.
Because of difficulties I'm having closing an instance of Access.Apllication after I'm done with it, I'm gonna try to use:
<System.Runtime.InteropServices.DllImport("Kernel32")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
End Function
to try and close the window handle, but my problem is that I don't know how to get the handle of the Access.Application instance in the form of a IntPtr...Any advice is greatly appreciated.
Thanks a lot,
Andeezle
Well from what I got I dont think thats the right way of closing an app, by manually closing handles. It is likely you face memory leaks in the unmanaged heap.
Please specify why you have problems closing the Access through its API.
As long for your question:
if Access is managed object, you can use:
Dim gh As GCHandle = GCHandle.Alloc (Access.Instance, GCHandleType.Pinned) Dim AppAddress As IntPtr = gh.AddrOfPinnedObject()
gh.Free()For more information see:
http://www.codeproject.com/vb/net/Marshal.asp
I am not sure if the call to CloseHandle is not designed for closing windows handles particulary, in such a case you should obtain the handle of your host form.
Hi Petar, the problem is that when i run the following sub, the access.application instance won't close and remains visible, .visible = false doesn't work either. BTW, I only have one form, the main form and it's just a simple utility. But when I end the program, i.e. mainform.close, then the access.application instance is destroyed and the window goes away. Interestingly, if I just stop the code from the IDE, in other words stop it pre-maturely, then the access.app instance stays open. the only way i can close it then is to use the task manager and end the process. This is an important issue for me. I greatly appreciate your help and look forward to hearing more from you.
Public Sub RepairCorruptDatabase(ByRef corruptDB As FileInfo, ByVal destinationFileName As String)
Dim accessApp As New Access.Application 'from the access 97 object library
Try
accessApp.DBEngine.CompactDatabase(corruptDB.FullName, destinationFileName)
Catch ex As ApplicationException
MsgBox(ex.ToString, MsgBoxStyle.Critical, App.Title)
Finally
accessApp.Quit()
Marshal.ReleaseComObject(accessApp) 'this was recommended by another forum user. but still doesn't work
accessApp = Nothing
End Try
End Sub
-Thanks,
Andeezle