3

So in my c++.dll file i got the following function:

    DLL void GetUserPass(char* &userName, char* &passWord)
{
    userName = "ceva";
    passWord = "altceva";
}

Now I want to call this from c# but it gives me an error:

[DllImport("myDLL.dll")]
private static extern void GetUserPass(ref string userName, ref string passWord);

static void f()
{
        string userName ="";
        string passWord ="";

        GetUserPass(ref userName, ref passWord);
}

And the error is:

A call to PInvoke function 'Download FTP Archive!Download_FTP_Archive.Program::GetUserPass' 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.

Should I try in C++ dll file something like:

using std::string;
 DLL void GetUserPass(string &userName, string &passWord)
{
    userName = "ceva";
    passWord = "altceva";
}
3
  • possible duplicate of Strings in c++? Commented May 21, 2012 at 8:41
  • searching on, for example, "c++ c# string interop" easily gives thousands of hits explaining how to deal with this Commented May 21, 2012 at 8:42
  • Perhaps a link to one of these thousands of hits would be more useful than a comment detailing their existence ¯_(ツ)_/¯ Commented May 3, 2016 at 17:04

2 Answers 2

3

try the following:

DLL void __stdcall GetUserPass(char* &userName, char* &passWord)
{
    userName = "ceva";
    passWord = "altceva";
}



[DllImport("myDLL.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern void GetUserPass(ref IntPtr userName, ref IntPtr passWord);
static void f()
{
        IntPtr userNamePtr = new IntPtr();
        IntPtr passWordPtr = new IntPtr();
        GetUserPass(ref userNamePtr, ref passWordPtr);
        string userName = Marshal.PtrToStringAnsi( userNamePtr );
        string passWord = Marshal.PtrToStringAnsi( passWordPtr );
}
Sign up to request clarification or add additional context in comments.

Comments

0

I am not a c++ developer, but I am a bit familiar with c++ semantics. This char* &userName makes no sense to me. Maybe I am wrong, but try char* userName and see whether it works for you.

3 Comments

no it doesn't work :( char* for me denotes a pointer to a terminated zero char (strings) and the & means to keep the value. I don't know if I remembered ok.
his C++ function is fine, your proposed change would break its semantics. However char*& may not be the best type for marshalling
OK, I see now that it could work. Mixing reference type with a pointer was a bit confusing for me. There is a problem with c++ using ansi strings and c# unicode. Maybe you should specify this in your code? Read about it here: stackoverflow.com/questions/683013/…

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.