c# - DLLImport: passing short type parameter -


i'm trying pass parameter of short type c++ unmanaged function imported dll. in c++ dll code have following function:

__declspec(dllexport)void changeicon(char *executablefile, char *iconfile,     uint16 imagecount) {     // code, doesn't matter } 

in c# managed code import function following code:

[dllimport("iconchanger.dll")] static extern void changeicon(string executablefile, string iconfile,     ushort imagecount); 

and call it:

ushort imagecount = 2; changeicon("filepath", "iconpath", imagecount); 

the application executes function fine; however, message following text pops out:

managed debugging assistant 'pinvokestackimbalance' has detected problem in 'foo.exe'. additional information: call pinvoke function 'bar.iconchanger::changeicon' has unbalanced stack. because managed pinvoke signature not match unmanaged target signature. check calling convention , parameters of pinvoke signature match target unmanaged signature.

if not pass last parameter, message doesn't pop out, must due passing short type. have tried int too, same message appears.

obviously, i'm doing wrong when passing numerical parameter. how match parameters between two?

make sure calling convention matches. if don't specify calling convention stdcall assumed. c/c++ standard calling convention cdecl. either use __stdcall on c++ function:

void __declspec(dllexport) __stdcall changeicon(char *executablefile, char *iconfile,     uint16 imagecount) {     // code, doesn't matter } 

or specify callingconvention.cdecl on dllimport:

[dllimport("iconchanger.dll", callingconvention = callingconvention.cdecl)] 

as side note, uint16 not cls compliant type want int16 if need compliance.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -