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)'字符。 –