0
我在写一个C#应用程序,它将大小为30的空字符串数组传递给C++ DLL。该字符串数组需要在DLL中填充并返回给C#应用程序。将C#中的字符串数组传递给C++ DLL函数并将其填充到DLL中并返回
我的代码我观察DLL函数调用结束时的内存损坏。
我的C++ DLL代码如下。请帮助我。提前致谢。
SAMPLEDLL_API BOOL InitExecution (wchar_t **paszStrings, int count)
{
for (int i = 0 ; i < count; i++)
{
mbstowcs(*(paszStrings + i), "Good",4);
//*(paszStrings + i) = "Good";
}
return TRUE;
}
我的C#代码是
string[] names = new[] { "Britto", "Regis" };
if (Wrapper1.InitExecution(ref names, names.Length) == 1)
MessageBox.Show("Passed");
[DllImport("MFCLibrary1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern UInt32 InitExecution(ref string[] Names, int count);
谢谢。
C#字符串是不可变的。尝试传递一个'StringBuilder'。 – Romoku
或者使用BSTR,而不必决定要分配多少内存 –