, , ++ . stri. stri , , .
, , . ++, # .
, .
# ++, . # StringBuilder . StringBuilder ++, - LPWSTR. # , ++ C, .
BSTR ++, ++ #.
, BSTR, . :
++
#include <comutil.h>
BSTR GetSomeText()
{
return ::SysAllocString(L"Greetings from the native world!");
}
#
[DllImport(@"test.dll", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string GetSomeText();
. , P/Invoke , , . , , ?
1 , , , . , , .
1 , , (, , ). 2, BSTR.
BSTR, , UTF-16,
char*, " - ".
, char* BSTR :
BSTR ANSItoBSTR(char* input)
{
BSTR result = NULL;
int lenA = lstrlenA(input);
int lenW = ::MultiByteToWideChar(CP_ACP, 0, input, lenA, NULL, 0);
if (lenW > 0)
{
result = ::SysAllocStringLen(0, lenW);
::MultiByteToWideChar(CP_ACP, 0, input, lenA, result, lenW);
}
return result;
}
, BSTR LPWSTR, std::string, std::wrstring ..