Reputation: 21
I am trying to use an open-source DLL to convert SQL queries between different dialects in a C# university project. I have a function in the dll which requires a void type pointer to an object. I cannot find a suitable type in c# to use for calling this function.
I've tried using a IntPtr for the required pointer to the object but i am getting the following error
A call to PInvoke function 'WindowsFormsApp1!WindowsFormsApp1.Form1::SetParserTypes' 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.
the function in c/c++ :
void SetParserTypes(void *parser, short source, short target)
{
if(parser == NULL)
return;
SqlParser *sql_parser = (SqlParser*)parser;
// Run conversion
sql_parser->SetTypes(source, target);
}
the function im trying to use in c# :
[DllImport(dllName: "DLL_LOCATION", EntryPoint = "SetParserTypes")]
public unsafe static extern void SetParserTypes(void *parser , short source, short target);
i am calling this function like this :
SetParserTypes(&parserObj, 1,2);
I am getting the following exception when calling the function in c#
Managed Debugging Assistant 'PInvokeStackImbalance' : 'A call to PInvoke function 'WindowsFormsApp1!WindowsFormsApp1.Form1::SetParserTypes' 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.'
Upvotes: 2
Views: 89
Reputation: 386
That's at least because of the type of your parameter parser
, it should be declared as a System.IntPtr
instead of void*
.
For more information about type mapping between C# and C++ have a look to: https://learn.microsoft.com/en-us/dotnet/framework/interop/marshaling-data-with-platform-invoke
Upvotes: 1