Problem calling DLL function with C++ HANDLE in VB.NET

voltzart

Member
Joined
Dec 28, 2011
Messages
11
Programming Experience
1-3
I'm having trouble accessing a DLL function that uses a HANDLE as a return type.

This is the signature of the function within the DLL:

VB.NET:
HANDLE netagentsearch(int nPort, int *nError);

I am able to access the function fine with C++.

VB.NET:
typedef HANDLE (*funcnetagentsearch)(int nPort,int *nError);
funcnetagentsearch Search;
Search = (funcnetagentsearch)GetProcAddress(hGetProcIDDLL, "netagentsearch");
HANDLE test = Search(7, &error);

I understand that vb.net doesn't have a HANDLE type, but as far as I knew, you could use IntPtr instead. This is the code in vb.net:

VB.NET:
<DllImport("netclient.dll", EntryPoint:="netagentsearch")> _
Public Shared Function GetDeviceList(ByVal nPort As Integer, ByRef nError As Integer) As IntPtr
End Function

Dim test As IntPtr = WirelessLedCtrl.GetDeviceList(7, error)

The error I receive when trying to run the VB.NET code is:

DLLImport_Wireless.WirelessLedCtrl::GetDeviceList' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Is there something I am doing wrong? Thanks in advance for your help. I just solved a major problem that I've been working on for 4 months, so if I end up having to use C++, I wouldn't be too disappointed. Still, I would rather use vb.net. Thanks.
 
Are you sure calling convention is default StdCall? CallingConvention field can be set for DllImport.
 
Back
Top