2

I want to send my C# string to a C++ DLL function. I have succeeded, both with StringBuilder:

[C#]
public static extern int installHook(StringBuilder directory);
StringBuilder myText = new StringBuilder(512);
myfunc(myText);

[C++]
int OPENGLHOOK_API myfunc(char* directory)
{
    ::MessageBoxA(NULL,directory,"test123",0);
}

and with a simple string & wchar:

[C#]
public static extern int installHook(string directory);
myfunc("myText");

[C++]
int OPENGLHOOK_API installHook(wchar* directory)
{
    wstring s = directory;
    const wchar_t* wstr = s.c_str();
    size_t wlen = wcslen(wstr) + 1;
    char newchar[100];
    size_t convertedChars = 0;
    wcstombs_s(&convertedChars, newchar, wlen, wstr, _TRUNCATE);

    ::MessageBoxA(NULL,newchar,"test123",0);
}

as it was mentioned in other thread on StackOverflow. The problem is that everytime I do this, I get an error because the function signatures are not the same:

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\Dave\Documents\Visual Studio 2010\Projects\OpenGLInjector\Loader\bin\Release\Loader.vshost.exe'. Additional Information: A call to PInvoke function 'Loader!Loader.Form1::myfunc' 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.

Any idea how I can fix my problem/what to do from here?

6
  • What does OPENGLHOOK_API expand to? Commented Jun 7, 2011 at 19:29
  • simply the __declspec(dllexport) definition Commented Jun 7, 2011 at 19:30
  • possible duplicate of pinvokestackimbalance -- how can I fix this or turn it off? Commented Jun 7, 2011 at 19:33
  • i'd like it to be.. I could turn off the MDA, but that doesn't rly fix my problem does it? Commented Jun 7, 2011 at 19:34
  • 1
    @David : Then that implies that your functions are being exported as __cdecl, but .NET P/Invoke expects __stdcall by default. You'll need to reconcile these. Commented Jun 7, 2011 at 19:38

2 Answers 2

3

I believe the problem is the default calling convention between the two languages. C# is __stdcall and c++ is __cdecl, I believe. Try explicitly stating __stdcall on your C++ method signatures and see if that doesn't resolve the error.

C++:

int OPENGLHOOK_API __stdcall installHook(wchar* directory)

C#:

[DllImport( "yourdll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode )]
static extern int installHook(string directory);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, where exactly do I do this? do i replace the __declspec(dllexport)? - NVM got it, that works great, thanks
Shouldn't the C++ be int __stdcall installHook(wchar* directory), i.e. without the OPENGLHOOK_API because OPENGLHOOK_API expands to __declspec(dllexport) ?
-1

You need to explicitly describe the unmanaged calling convention for 32bit, and in addition, you will need to explicitly describe the unmanaged string type- ASCII, UTF16, etc.

4 Comments

Isn't the default calling convention for C# __stdcall independent of of the platform?
@Brandon Moretz: x64 doesn't have different calling conventions.
@Downvoter : Doing what this answer suggests would have completely solved the OP's problem; why the downvote?
Sorry to be so thick, but where do you explicitly describe the unmanaged string type?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.