Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PInvoke Unbalances the stack

Tags:

c++

c#

dll

pinvoke

Good afternoon,

I have been working on a dll that can use CORBA to communicate to an application that is network aware. The code works fine if I run it as a C++ console application. However, I have gotten stuck on exporting the methods as a dll. The methods seems to export fine, and if I call a method with no parameters then it works as expected. I'm hung up on passing a C# string to a C++ method.

My C++ method header looks like this:

bool __declspec(dllexport) SpiceStart(char* installPath)

My C# DLL import code is as follows:

[DllImportAttribute("SchemSipc.dll", CharSet=CharSet.Ansi)]
private static extern bool SpiceStart(string installPath);

I call the method like so:

bool success = SpiceStart(@"c:\sedatools");

The call to SpiceStart throws the exception "PInvokeStackImbalance", which "is likely because the managed PInvoke signature does not match the unmanaged target signature."

Does anyone have any suggestions? If I remove the char* and string from the parameters, then the method runs just fine. However, I'd like to be able to pass the installation path of the application to the dll from C#.

Thanks in advance,

Giawa

like image 626
Giawa Avatar asked Apr 24 '10 23:04

Giawa


1 Answers

The calling conventions don't match. In C++, declare the function with the stdcall calling convention:

extern "C" bool __declspec(dllexport) __stdcall SpiceStart(char* installPath)

like image 123
zumalifeguard Avatar answered Nov 20 '22 15:11

zumalifeguard