[英]Reading from an unmanaged stream - unsafe code, IntPtr
Firefox(Gecko)3.5代碼中公開了以下內容:
[Guid("fa9c7f6c-61b3-11d4-9877-00c04fa0cf4a"), ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface nsIInputStream
{
void Close();
int Available();
int Read(IntPtr aBuf, uint aCount);
int ReadSegments(IntPtr aWriter, IntPtr aClosure, uint aCount);
bool IsNonBlocking();
}
所以在這里,我在我的小型.Net / C#應用程序中,想要利用我從代碼中其他地方返回的實例,但是我在弄清楚如何處理int Read(IntPtr aBuf, uint aCount)
方法。
我想用從Read
方法接收的內容填充本地byte[]
數組,但是我不確定如何處理IntPtr或如何將其轉換回托管字節數組。
任何提示/猜測/指針? (雙關語無意)
基於這篇引人入勝的文章,您應該可以將方法簽名更改為:
int Read([Out] byte[] aBuf, uint aCount);
使用現有的定義,您將需要使用Marshal.AllocHGlobal來分配非托管內存塊並將其指針傳遞給該方法。 返回時,使用Marshal.Copy將內存復制到托管字節[],並使用Marshal.FreeHGlobal釋放分配的非托管內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.