如何为PInvoke分配gt 2GB缓冲区 [英] How do I allocate gt 2GB buffer for PInvoke

查看:55
本文介绍了如何为PInvoke分配gt 2GB缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C#应用程序,如何从非托管内存中分配(并释放)一个巨大的(> 2GB)缓冲区(因为我无法分配托管缓冲区> 2GB)我可以通过PInvoke传递给C ++ dll然后在dll填充之后在C#中访问它。

C ++ dll函数的函数签名是:

 WIN32DLL_API XyzzyFunc( unsigned   short  * dataPtr, long  buffSize, / *  其他不相关的参数* / ); 



我认为我在C#中使用不安全的指针来处理这个巨大的缓冲区。



我假设在32位应用程序中它是不可能的,怎么用64位?



(Re: WIN32DLL_API 现在dll是为32位构建的,我期待成为能够让供应商提供64位版本...)

解决方案

Win32 DLL上是否有函数将此数据保存到文件中?如果是这样,那么你可以将文件保存到临时位置,然后使用.NET Reader甚至内存映射文件进行随机访问。



我写了一个日志读者一旦快速阅读并滚动大小超过4太字节的日志。我在任何时候都没有超过2兆字节的数据包含在应用程序的内存中。即使我可以将整个文件放在内存中,我发现Windows在使用该方法时会产生许多页面错误,这会使我的应用程序变慢。最好在大量数据上使用读取器或内存映射技术,首先保存到文件中,以避免分页和整个页面调度错误问题。



那个如果您的DLL将允许您首先保存数据。


From a C# application, how can I allocate (and free) a huge (>2GB) buffer from unmanaged memory (since I can't allocate a managed buffer >2GB) that I can pass via PInvoke to a C++ dll and then access it within the C# after the dll has filled it.
The function signature of the C++ dll function is:

WIN32DLL_API XyzzyFunc(unsigned short* dataPtr, long buffSize, /* other irrelevant params*/);


I think that I'd use unsafe pointers in C# to work with this huge buffer.

I assume in a 32 bit application it is not possible, so how in a 64 bit?

(Re: WIN32DLL_API right now the dll is built for 32 bit, I'm expecting to be able to get the vendor to provide a 64 bit version...)

解决方案

Is there a function on the Win32 DLL to save this data to a file? If so, then you could save the file to a temporary location, then use a .NET Reader or even memory map the file for random access.

I wrote a log reader once that quickly read and scrolled through logs greater than 4 Terabytes in size. I never had more than 2 Megabytes in data contained within the application's memory at any one time. Even though I could place the entire file within memory, I found that Windows generates many page faults when using that method which slowed my application down to a crawl. It is better to use a reader or memory map technique on large amounts of data that you save to a file first to avoid paging and the whole paging fault issue.

That is if your DLL will let you save the data first.


这篇关于如何为PInvoke分配gt 2GB缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆