|
-
Apr 3rd, 2011, 05:29 PM
#1
Thread Starter
Junior Member
[RESOLVED] How to use LoadLibrary()/GetProcAddress()?
Hey -
The following code works great to interface *.dll that I built in Ada from VB.NET (2008) ...
Private Declare Function TestDelegate Lib "C:\ABC.dll" Alias "Test" _
(ByVal A As Single, _
ByVal B As Integer, _
ByVal C As String, _
ByRef D As TestType) As Single
... where TestType is a struct. The only problem I have with this is that the *.dll and its path has to be hard coded.
So I started looking at using LoadLibrary() and GetProcAddress(). I figured out the following interfaces ...
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Function LoadLibrary(<[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpFileName As String) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Function GetProcAddress(<[In]()> ByVal hModule As IntPtr, <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal lpProcName As String) As IntPtr
End Function
... that I can call like ...
Dim lLibraryHandle As IntPtr = LoadLibrary("C:\ABC.dll")
Dim lFunctionHandle As IntPtr = GetProcAddress(lLibraryHandle, "Test")
I check for IntPtr.Zero to make sure this all worked but now the question is how can I actually use the lFunctionHandle to call Test()? Has somebody worked
with this before?
I'm also curious what the difference is between the <DllImport("kernel32.dll" ... and something like Private Declare Function ABC Lib "kernel32" (...) As XYZ to interface a *.dll?
My last question is whether it is necessary to call FreeLibrary() or whether VB will handle freeing up the memory?
Thanks,
Joe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|