C # reads the data in IntPtr Z

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:

(the type to get) Marshal.PtrToStructure (ptr,typeof (type to get));//This translates to the type of data you can manipulate in C # and then reads the content,//I don't know if it works for you, Because PtrToStructure does not apply in all cases, if you use this method, you can see the 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));

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 in IntPtr Z

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.