0
extern "C"
__declspec(dllexport)
bool FillString(LPWSTR OutName)
{
LPWSTR out = L"TheName\0";
int len = wcslen(out);
memcpy(
OutName,
out,
len * sizeof(wchar_t));
return true;
}
即在我的C/C++ DLL功能,下面是从C#我的呼叫...stringbuilder的作为输出参数
[DllImport(@"My.dll", EntryPoint = "FillString", CallingConvention = CallingConvention.Cdecl)]
public static extern bool MyFunction([MarshalAsAttribute(UnmanagedType.LPWStr)] StringBuilder Name);
var fromdll = new StringBuilder(64);
// I do not know length of out string (Name), but it is null terminated
bool IsFilled = MyFunction(fromdll);
Console.WriteLine(fromdll);
输出是
TheName ???
任何人都可以帮助我得到输出......?
TheName
是否有原因,您不使用例如['wcscpy'](http://en.cppreference.com/w/cpp/string/wide/wcscpy)? –
StringBuilder是可变的,但是这里被转换为C字符串,然后没有任何积极的事情发生。 –
似乎你正在复制wcslen字符,没有\ 0。尝试复制'(len + 1)* sizeof(wchar_t)'字符。 –