vb.net pointer

Source: Internet
Author: User

In. NET, the storage functions for pointers to data are encapsulated in the Marshal class, and the main functions are: Copy, Ptrtostringuni, PtrToStructure, OffsetOf, writexxx,rreadxxx, and so on.

' Use <structlayout (layoutkind.sequential) > properties to tell the net compiler that the elements of the structure are arranged in memory in the order in which they appear
<structlayout (layoutkind.sequential) > _
Public Structure Defudt_test
Public BYTB as Byte
Public i32a as Int32
End Structure

Public Function fnGetIntptr1 () as IntPtr
' Get a 4-byte array pointer
Dim Tabyttest (3) as Byte
The following statement tells the net garbage collection process not to handle tabyttest, which means that the memory area Tabyttest occupies is fixed.
Dim Thobject as Gchandlegchandle = GCHandle.Alloc (tabyttest, gchandletype.pinned)
Dim Tpobject as IntPtr = Thobject.addrofpinnedobject () ' Gets a pointer to a byte array

' Get a pointer to the 32-bit memory data,
' Because methods that use GCHandle to fetch pointers are valid only for referenced objects,
' So for example int32 equivalent type must use the method that encapsulates it as an object to become a reference type
Dim ti32test as Object = Convert.ToInt32 (0)
The following statement tells the net garbage collection process not to handle ti32test, which means that the Ti32test memory location is fixed.
Dim ThObject1 as Gchandlegchandle = GCHandle.Alloc (ti32test, gchandletype.pinned)
Dim TpObject1 as IntPtr = Thobject1.addrofpinnedobject () ' Get Ti32test's first address

Dim TudtTest1 as Defudt_test
' Because the structure is a value type variable, to ensure that the pointer application is convenient, we apply
' Get a byte array pointer that is the same size as the structure tudtTest1, as long as the space occupies the same length and structure as possible.
' Because net inserts additional bits of data in the fabric package, it is important to use the sizeof method to get the actual size of the structure in unmanaged use
Dim tudttest (marshal.sizeof (TudtTest1)) as Byte
Dim thObject2 as Gchandlegchandle = GCHandle.Alloc (tudttest, gchandletype.pinned)
Dim tpObject2 as IntPtr = Thobject2.addrofpinnedobject () ' Get pointer to structure

' Here you can write arbitrary code for the pointer processing (supplemented in example 2) ...

' Be sure to release the memory block that the pointer points to when you're done with it so that the garbage collector can reclaim the memory block
If thobject.isallocated Then
Thobject.free ()
End If
If thobject1.isallocated Then
Thobject1.free ()
End If
If thobject2.isallocated Then
Thobject2.free ()
End If
End Function

In the above example, the vb.net pointer flow processing can be summed up as:
1. Define a reference variable with the appropriate memory length (for differences in reference variables and value variables you can visit vb.net's books)
2. Use the GCHandle.Alloc method to fix the memory area of the variable.
3. Use the Addrofpinnedobject of the GCHandle object to obtain the first address of the memory area and assign a value to the pointer variable.
4. Operate the pointer
5. Use the free method of the GCHandle object to release the memory area that the pointer points to so that the net garbage collector can reclaim this memory space
6, VB. NET pointers to data access

In. NET, the storage functions for pointers to data are encapsulated in the Marshal class, and the main functions are: Copy, Ptrtostringuni, PtrToStructure, OffsetOf, writexxx,rreadxxx, etc. Where the writexxx representation writes the XXX type of data to the address represented by the pointer, and the READXXX function is to read the data in the address of the vb.net pointer as XXX type.

vb.net pointer

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.