如何将IntPtr转换为流?

时间:2022-09-01 11:23:03
class Foo
{
   static bool Bar(Stream^ stream);
};

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

MemoryStream will take a byte[] but I'd like to do this without copying the data if possible.

MemoryStream将占用一个字节[],但我想在不复制数据的情况下执行此操作。

2 个解决方案

#1


6  

You can avoid the copy if you use an UnmanagedMemoryStream() instead (class exists in .NET FCL 2.0 and later). Like MemoryStream, it is a subclass of IO.Stream, and has all the usual stream operations.

如果使用UnmanagedMemoryStream()(.NET FCL 2.0及更高版本中存在类),则可以避免使用该副本。与MemoryStream一样,它是IO.Stream的子类,并具有所有常见的流操作。

Microsoft's description of the class is:

微软对该课程的描述是:

Provides access to unmanaged blocks of memory from managed code.

提供从托管代码访问非托管内存块的权限。

which pretty much tells you what you need to know. Note that UnmanagedMemoryStream() is not CLS-compliant.

这几乎告诉你你需要知道什么。请注意,UnmanagedMemoryStream()不符合CLS。

#2


1  

If I had to copy the memory, I think the following would work:

如果我必须复制内存,我认为以下内容可行:


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);
}

#1


6  

You can avoid the copy if you use an UnmanagedMemoryStream() instead (class exists in .NET FCL 2.0 and later). Like MemoryStream, it is a subclass of IO.Stream, and has all the usual stream operations.

如果使用UnmanagedMemoryStream()(.NET FCL 2.0及更高版本中存在类),则可以避免使用该副本。与MemoryStream一样,它是IO.Stream的子类,并具有所有常见的流操作。

Microsoft's description of the class is:

微软对该课程的描述是:

Provides access to unmanaged blocks of memory from managed code.

提供从托管代码访问非托管内存块的权限。

which pretty much tells you what you need to know. Note that UnmanagedMemoryStream() is not CLS-compliant.

这几乎告诉你你需要知道什么。请注意,UnmanagedMemoryStream()不符合CLS。

#2


1  

If I had to copy the memory, I think the following would work:

如果我必须复制内存,我认为以下内容可行:


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);
}