Calling external DLL function

vfandrea

New member
Joined
Mar 21, 2012
Messages
2
Location
Italy
Programming Experience
1-3
In my application I'm trying to invoke a function from an external DLL (this DLL is compiled in C++)

I have this code to declare the DLL function:
VB.NET:
<[COLOR=#2b91af]DllImport[/COLOR]([COLOR=#a31515]"{DLLPATH}"[/COLOR], CharSet:=[COLOR=#2b91af]CharSet[/COLOR].Auto, SetLastError:=[COLOR=blue]True[/COLOR])> _
[COLOR=blue]Public[/COLOR] [COLOR=blue]Shared[/COLOR] [COLOR=blue]Function[/COLOR] functionName(<[COLOR=#2b91af]MarshalAs[/COLOR]([COLOR=#2b91af]UnmanagedType[/COLOR].VBByRefStr)> [COLOR=blue]ByRef[/COLOR] Name1 [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], <[COLOR=#2b91af]MarshalAs[/COLOR]([COLOR=#2b91af]UnmanagedType[/COLOR].VBByRefStr)> [COLOR=blue]ByRef[/COLOR] Name2 [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]) [COLOR=blue]As[/COLOR] [COLOR=blue]Short[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
(where {DLLPATH} is the DLL file name)

then into my code I call the function with this:

VB.NET:
functionName("String1","String2")

If I compile the code with .NET 2, it work perfectly; if I compile it with .NET 4 it guves me the following error:

A call to PInvoke function 'TestVB!TestVB.Form1::functionName' 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.

Can someone help me to make the code work in .NET 4??

Thank you in advance.
 
Finally I solved it adding the callingConvention like this:
VB.NET:
[/FONT][/COLOR][LEFT][COLOR=#000000]DllImport("{DLLPATH}", CallingConvention.Cdecl, CharSet:=CharSet.Auto, SetLastError:=True)> _

Thank you.
[/LEFT]
 
Back
Top