c# 讀取IntPtr 中的資料

來源:互聯網
上載者:User

標籤:

c++的寫法是這樣的:
LRESULT CPictureQueryDlg::OnQueryPicNty(WPARAM wp, LPARAM lp)
{
EnableWindow(TRUE);

BYTE *pbyMsg = (BYTE*)lp;

// 得到當前頁數目
m_dwCurCount = *reinterpret_cast<DWORD*>(pbyMsg);
// 得到總數量
m_dwTotalCount = *reinterpret_cast<DWORD*>(pbyMsg + sizeof(DWORD));

// 得到查詢結果指標
TNVR_PIC_GRABTASK* ptResultQuery = reinterpret_cast<TNVR_PIC_GRABTASK*>(pbyMsg + sizeof(DWORD)*2);
memset(m_atGrTask, 0, sizeof(TNVR_PIC_GRABTASK) * NVR_MAXNUM_RECORDQUERY);
memcpy(m_atGrTask, ptResultQuery, m_dwCurCount * sizeof (TNVR_PIC_GRABTASK));
}

 

已經拿到IntPtr了的話可以用類型強制轉換擷取IntPtr裡的東西:

?
123 (要擷取的類型)Marshal.PtrToStructure(ptr,typeof(要擷取的類型)); //這樣就轉換到你c#可以操作的資料類型然後來讀取內容, //我不知道這個在你那裡能否適用,因為PtrToStructure並不是所有情況都適用,如果用這個方法的話具體可以看看MSDN

 主要是需要獲得類型的長度,如果長度獲得不準確,讀到的資料就會有問題。
(StructureType)Marshal.PtrToStructure((IntPtr)((uint)(pbyMsg + sizeof(uint) * 2 + i * Marshal.SizeOf(typeof(StructureType)))), typeof(StructureType));

?
123456789101112131415161718192021222324252627 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text;   namespace OpenCover.Framework.Communication {     public interface IMarshalWrapper     {         T PtrToStructure<T>(IntPtr pinnedMemory);         void StructureToPtr<T>(T structure, IntPtr pinnedMemory, bool fDeleteOld);     }       public class MarshalWrapper : IMarshalWrapper     {         public T PtrToStructure<T>(IntPtr pinnedMemory)         {             return (T)Marshal.PtrToStructure(pinnedMemory, typeof(T));         }           public void StructureToPtr<T>(T structure, IntPtr pinnedMemory, bool fDeleteOld)         {             Marshal.StructureToPtr(structure, pinnedMemory, fDeleteOld);         }     } }

c# 讀取IntPtr 中的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.