I'm calling a function in a library ("wow64ext.dll") with this declaration
Declaration in C++ library:
extern "C" __declspec(dllexport) DWORD64 __cdecl GetModuleHandle64(wchar_t* lpModuleName)
My declaration in the VB.net program is:
Public Declare Function GetModuleHandle64 Lib "wow64ext.dll" (ByRef lpModuleName As String) As ULong
Now when I call this function via
Dim ntqipHandle as Ulong = GetModuleHandle64("ntdll.dll")
it throws a exception: "Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'MyCode.vshost.exe'."
What am I doing wrong here?
(I'm fully aware that this might be a silly question. Nevertheless so many people over at xsimulator.net will be happy when you help me to resolve it :) )
You have a few problems. One is the calling convention. The unmanaged code uses cdecl. Your code uses stdcall. The other problem is the strings. The unmanaged code uses UTF-16 text, your code uses ANSI encoded text. And passing ByRef
is wrong too. Using Declare
is not advisable these days. That was how you did things in the old VB6 days. Now that we have VB.net you should use p/invoke. It is much more flexible and capable.
Fix it all up like this:
<DllImport("wow64ext.dll", CallingConvention := CallingConvention.Cdecl, _
CharSet := CharSet.Unicode)> _
Public Function GetModuleHandle64(ByVal lpModuleName As String) As ULong
End Function