C # reads the data from the IntPtr

Source: Internet
Author: User
Tags type casting

The C + + notation is:
LRESULT cpicturequerydlg::onquerypicnty (WPARAM WP, LPARAM LP)
{
EnableWindow (TRUE);

BYTE *pbymsg = (byte*) LP;

Get the current page number
M_dwcurcount = *reinterpret_cast<dword*> (pbymsg);
Get the total quantity
M_dwtotalcount = *reinterpret_cast<dword*> (pbymsg + sizeof (DWORD));

Get the query result pointer
tnvr_pic_grabtask* ptresultquery = reinterpret_cast<tnvr_pic_grabtask*> (pbymsg + sizeof (DWORD));
memset (m_atgrtask, 0, sizeof (tnvr_pic_grabtask) * nvr_maxnum_recordquery);
memcpy (M_atgrtask, ptresultquery, M_dwcurcount * sizeof (tnvr_pic_grabtask));
}

You've got IntPtr. You can use type casting to get something in IntPtr:

?
123 (要获取的类型)Marshal.PtrToStructure(ptr,typeof(要获取的类型)); //这样就转换到你c#可以操作的数据类型然后来读取内容, //我不知道这个在你那里能否适用,因为PtrToStructure并不是所有情况都适用,如果用这个方法的话具体可以看看MSDN

The main thing is to get the length of the type, and if the length gets inaccurate, the data read will be problematic.
(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 # reads the data from the IntPtr

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.