, 나는 다음과 같은 작업 것이라고 생각 : 당신이 (클래스 나중에 .NET FCL 2.0에 존재) 대신 UnmanagedMemoryStream()
를 사용하는 경우
static Stream^ UnicodeStringToStream(LPCWSTR szUnicodeString)
{
//validate the input parameter
if (szUnicodeString == NULL)
{
return nullptr;
}
//get the length of the string
size_t lengthInWChars = wcslen(szUnicodeString);
size_t lengthInBytes = lengthInWChars * sizeof(wchar_t);
//allocate the .Net byte array
array^ byteArray = gcnew array(lengthInBytes);
//copy the unmanaged memory into the byte array
Marshal::Copy((IntPtr)(void*)szUnicodeString, byteArray, 0, lengthInBytes);
//create a memory stream from the byte array
return gcnew MemoryStream(byteArray);
}
참고 :이 대답은 안전하지 않은 코드에서만 작동합니다. 안전하지 않은 플래그로 컴파일하지 않으면 데이터를 바이트 배열로 정렬 한 다음 해당 바이트 배열을 스트림에 래핑하면 더 효율적일 수 있습니다. 참조 : http://stackoverflow.com/a/11660831/684852 데이터의 길이 (포인터에서 원래 유니 코드 문자열의 바이트 수)를 알아야합니다. 예 : byte [] dataArray = 새 바이트 [dataLength]; Marshal.Copy (szUnicodeString, dataArray, 0, dataLength); MemoryStream stream = 새 MemoryStream (데이터 배열); –