문제

class Foo
{
   static bool Bar(Stream^ stream);
};

class FooWrapper
{
   bool Bar(LPCWSTR szUnicodeString)
   {
       return Foo::Bar(??);
   }
};

MemoryStream 걸릴 것이다 byte[] 하지만 난 그럴 거야 좋다 가능하면 데이터를 복사하지 않고 이 작업을 수행합니다.

도움이 되었습니까?

해결책

다음을 사용하면 복사를 피할 수 있습니다. UnmanagedMemoryStream() 대신(클래스는 .NET FCL 2.0 이상에 존재함)좋다 MemoryStream, 이는 의 하위 클래스입니다. IO.Stream, 일반적인 스트림 작업이 모두 포함되어 있습니다.

Microsoft의 클래스 설명은 다음과 같습니다.

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

이는 당신이 알아야 할 사항을 거의 알려줍니다.참고하세요 UnmanagedMemoryStream() CLS 규격이 아닙니다.

다른 팁

메모리를 복사해야 한다면 다음과 같이 하면 될 것 같습니다.


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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top