tags:

views:

102

answers:

1

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

+10  A: 

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)

zumalifeguard
+1. Or specify the calling convention in the DllImportAttribute.
Ben Voigt
Perfect - That did the job! Thanks
Giawa