将字符串从Native C ++ DLL传递到C#App

时间:2012-06-01 19:17:03

标签: c# c++ string visual-studio-2008 dllimport

我用C ++编写了一个DLL。其中一个函数写入字符数组。

C ++函数

EXPORT int xmain(int argc, char argv[], char argv2[])
{
    char  pTypeName[4096];
    ...
    //Other pTypeName ends up populated with "Portable Network Graphics"
    //This code verifies that pTypeName is populated with what I think it is:
    char szBuff[64];
    sprintf(szBuff, pTypeName, 0);
    MessageBoxA(NULL, szBuff, szBuff, MB_OK);
    //The caption and title are "Portable Network Graphics"

    ...
    //Here, I attempt to copy the value in pTypeName to parameter 3.
    sprintf(argv2, szBuff, 0);

    return ret;
}

C#Import

    //I believe I have to use CharSet.Ansi because by the C++ code uses char[],
    [DllImport("FirstDll.dll", CharSet=CharSet.Ansi)]
    public static extern int xmain(int argc, string argv, ref string zzz);

C#功能

private void button2_Click(object sender, EventArgs e)
{
    string zzz = ""; 
    int xxx = xmain(2, @"C:\hhh.bmp", ref zzz);
    MessageBox.Show(zzz);

    //The message box displays
    //MessageBox.Show displays "IstuÈst¼ÓstÄstlÄstwÄstiÑstõÖstwÍst\
    // aÖst[ÖstÃÏst¯ÄstÐstòÄstŽÐstÅstpÅstOleMainThreadWndClass"

}

我试图通过引用从C#传递参数,并让C ++ DLL填充参数。尽管我已经验证了DLL中的值是正确的,但是乱码会传递给C#应用程序。

如何将正确的字符串值写入C#字符串?

2 个答案:

答案 0 :(得分:5)

使用StringBuilder传递本机代码可以填写的字符数组(请参阅Fixed-Length String Buffers)。

声明函数:

[DllImport("FirstDll.dll", CharSet=CharSet.Ansi)]
public static extern int xmain(int argc, string argv, StringBuilder argv2);

使用它:

// allocate a StringBuilder with enough space; if it is too small,
// the native code will corrupt memory
StringBuilder sb = new StringBuilder(4096);
xmain(2, @"C:\hhh.bmp", sb);
string argv2 = sb.ToString();

答案 1 :(得分:1)

向DLLImport调用提供一些其他信息。请看下面我自己的例子:

[DllImport("tcpipNexIbnk.dll", EntryPoint = "SendData", CallingConvention = CallingConvention.Cdecl)]
    public static extern int Send([MarshalAs(UnmanagedType.LPWStr)]string message);

注意两件事,CallingConvention参数:     CallingConvention = CallingConvention.Cdecl)

按原样使用。

然后在c#字符串类型后面,您可以使用MarshalAS指令使用不同的非托管类型,这将把您的C#字符串参数转换为您在c ++程序中的本机字符串类型:

public static extern int Send([MarshalAs(UnmanagedType.LPWStr)]string message);

希望它有所帮助。