2008-09-03 8 views

답변

6

당신은 복사를 방지 할 수 있습니다. MemoryStream처럼 IO.Stream의 하위 클래스이며 모든 일반적인 스트림 작업이 있습니다. 클래스의

마이크로 소프트의 설명입니다

관리 코드에서 메모리 관리되지 않는 블록에 대한 액세스를 제공합니다.

거의 알려주는 내용을 알려줍니다. UnmanagedMemoryStream()은 CLS와 호환되지 않습니다.

+0

참고 :이 대답은 안전하지 않은 코드에서만 작동합니다. 안전하지 않은 플래그로 컴파일하지 않으면 데이터를 바이트 배열로 정렬 한 다음 해당 바이트 배열을 스트림에 래핑하면 더 효율적일 수 있습니다. 참조 : http://stackoverflow.com/a/11660831/684852 데이터의 길이 (포인터에서 원래 유니 코드 문자열의 바이트 수)를 알아야합니다. 예 : byte [] dataArray = 새 바이트 [dataLength]; Marshal.Copy (szUnicodeString, dataArray, 0, dataLength); MemoryStream stream = 새 MemoryStream (데이터 배열); –

1

, 나는 다음과 같은 작업 것이라고 생각 : 당신이 (클래스 나중에 .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); 
}
관련 문제