C # pointer Operation Marshal Instance

Source: Internet
Author: User

        Static voidMain (string[] args) {            byte[] A =New byte[]{0,0,0,0}; byte[] B =New byte[] {1,2,3,4}; INTPTR PT=Marshal.allochglobal (a.length); //Copy the length object to ptr starting with the startindex subscript from the source array;Marshal.Copy (b,0, pt+0, b.length); //copies a length object from PTR to the target array, starting with the startindex of the target array. Marshal.Copy ((pt+1), A,1,2); unsafe            {                byte* PB = (byte*) pt;  for(inti =0; i < a.length; i++) {Console.WriteLine ("/b:"+ (*pb++) +"/A:"+A[i]); }            }            //releasing unmanaged memory;Marshal.freehglobal (PT); byte[] ARBT =New byte[]{ $,0, -, -, +, -, -, -, the, -, One, A, -, -,0x0f,199}; IntPtr ptr=Marshal.allochglobal (arbt.length); //write data;Marshal.Copy (ARBT,0, PTR, arbt.length);  Short[] Arst =New  Short[Arbt.length/sizeof( Short)]; int[] Arint =New int[Arbt.length/sizeof(int)]; //copy to short data;Marshal.Copy (PTR, Arst,0, arst.length); //adjust the data at this time Arst unchanged under the arint change;Marshal.writebyte (PTR,1,9); Marshal.writebyte (PTR,Ten, (byte) (Marshal.readbyte (PTR,Ten)*9)); //copy to int data;Marshal.Copy (PTR, Arint,0, arint.length);  for(inti =0; i < arbt.length; i++) {Console.Write (Arbt[i]+"-");            } Console.WriteLine ();  for(inti =0; i < arbt.length; i++) {Console.Write (Marshal.readbyte (ptr,i)+"-");            } Console.WriteLine (); unsafe{//Gets the address of the element at the specified index in the specified array Short* PS = ( Short*) Marshal.unsafeaddrofpinnedarrayelement (Arst,0); byte[] tmp0 = bitconverter.getbytes (*PS); Console.WriteLine (*ps+"/"+ (ushort) *ps+", byte>>>&0="+ tmp0[0] +", &1="+ tmp0[1]); //Gets the address of the element at the specified index in the specified array                 int* pi = (int*) Marshal.unsafeaddrofpinnedarrayelement (Arint,0); byte[] Tmp1 = bitconverter.getbytes (*pi); Console.WriteLine (*pi +"/"+ (UINT) *pi +", byte>>>&0="+ tmp1[0] +", &1="+ tmp1[1] +", &2="+ tmp1[2] +", &3="+ tmp1[3]); Console.WriteLine ("-----Short 2 byte-----");  for(inti =0; i < arst.length; i++)                {                    byte[] tmp =bitconverter.getbytes (Arst[i]); Console.WriteLine (Arst[i]+"/"+ (ushort) Arst[i] +", byte>>>&0="+ tmp[0] +", &1="+ tmp[1]); } Console.WriteLine ("-----int 2 byte-----");  for(inti =0; i < arint.length; i++)                {                    byte[] tmp =bitconverter.getbytes (Arint[i]); Console.WriteLine (Arint[i]+"/"+ (UINT) Arint[i] +", byte>>>&0="+ tmp[0] +", &1="+ tmp[1] +", &2="+ tmp[2] +", &3="+ tmp[3]);       }} marshal.freehglobal (PTR); unsafe{Test TT=NewTest (); Tt.t1= -; Tt.t2=true; Tt.t3=6666; Tt.t4=666666; Tt.t6=false; strings ="ABCD Master 0x00"; Char[] CHS =S.tochararray (); Char* block =stackalloc Char[ -]; fixed(Char* CPT =CHS) {                     for(inti =0; I < CHS. Length; i++) {Tt.t5[i]= * (CPT +i); Block[i]= * (CPT +i); } Console.WriteLine (New string(TT.T5)); }            }        }
[StructLayout (layoutkind.sequential, Pack =4)]         Public unsafe structTest { Public byteT1;  Public BOOLT2;  Public ushortT3;  Public intT4; //fixed-size buffers             Public fixed Chart5[ -];  Public BOOLT6; }

//----------------------------------

From the above example, we can see that using the C # pointer to manipulate memory is very convenient. With Marshal we can get pointer IntPtr for unmanaged memory. The pointer we can cast to

A type pointer of SByte, BYTE, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool. After that we can copy,read, write and so on to manipulate memory.

As with C + +, the pointers we get can be *,->,&,++,--the data and displacement operations of the specified memory through the pointer operator. You can also use Marshalto convert our byte type data into a type conversion operation.

The Marshal class provides a powerful function of the conversion function.

Refer to Https://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.marshal_methods%28v=vs.80%29.aspx;

C # pointer Operation Marshal Instance

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.