I’m trying to pass parameter of short type to C++ unmanaged function imported from DLL. In my C++ DLL code I have a following function:
__declspec(dllexport)void ChangeIcon(char *executableFile, char *iconFile,
UINT16 imageCount)
{
// Some code, doesn't matter
}
In C# managed code I import this function with the following code:
[DllImport("IconChanger.dll")]
static extern void ChangeIcon(string executableFile, string iconFile,
ushort imageCount);
And then I call it:
ushort imageCount = 2;
ChangeIcon("filePath", "iconPath", imageCount);
The application executes the function just fine; however, the message with the following text pops out:
Managed Debugging Assistant ‘PInvokeStackImbalance’ has detected a problem in ‘foo.exe’.
Additional Information: A call to PInvoke function ‘bar.IconChanger::ChangeIcon’ 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.
If I do not pass the last parameter, the message doesn’t pop out, so it must be due to passing short type. I have tried with int too, the same message appears.
Obviously, I’m doing something wrong when passing this numerical parameter. How to match parameters between the two?
Make sure the calling convention matches. If you don’t specify a calling convention
StdCallis assumed. However for C/C++ the standard calling convention isCdecl. So either use__stdcallon your C++ function:or specify
CallingConvention.Cdeclon theDllImport:As a side note, UInt16 is not CLS a compliant type so you probably want Int16 if you need this compliance.