Introduction
Visual Basic as a language never had provision of pointers or raw memory management as in C or C++. However, in VB.NET you can do so using certain .NET framework structures and classes. They include IntPtr, Marshal and GCHandle. These structures and classes allow you to communicate with managed and unmanaged environments. In this article I (Bipin Joshi) will show you how to use these structures and classes to perform pointer and unmanaged memory operations. Though the examples I give are in VB.NET, converting them to C# should not be a problem. Also note that, for the sake of simplicity I have not included any unmanaged code samples here. My aim is to illustrate use of these structures and classes so that one can use them as per individual requirements.
About IntPtr structure
IntPtr is a structure that acts as an integer pointer that is designed to be platform specific. This structure can be used in languages that do or do not support pointers. .NET file IO classes use this structure extensively for holding file handles.
About Marshal class
The IntPtr structure simply acts as a pointer to platform specific integer. It does not have any ability to write or read data at that memory location. You need to use Marshal class residing in System.Runtime.InteropServices namespace. This class provides collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types. Make sure you import System.Runtime.InteropServices in your code.
About GCHandle structure
The GCHandle structure provides a means for accessing a managed object from unmanaged memory. This can be used to control garbage collection of your object when unmanaged code is using it.
Writing and reading an integer value
In our first example, we will store an integer value in memory using Marshal class. Then we will get pointer to its memory location and store it in IntPtr structure. Finally, we will read value back using Marshal class again.
Dim ptr As IntPtr
Dim n as Integer=123
ptr = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(ptr, n)
Here, we declared a variable of type IntPtr. We then used AllocHGlobal shared method of Marshal class that allocates a memory from global heap of 4 bytes and returns its address. We collect the address in our IntPtr variable. We then called the WriteInt32 method of Marshal class and passed memory address for writing operation followed by integer to be written.
You can read value from a memory location as follows:
Dim myint as Integer=Marshal.ReadInt32(ptr)
Writing and reading strings
In this example we will write a managed string to heap and then read it back.
Dim str as String = "hello world"
Dim ptr As IntPtr = Marshal.StringToHGlobalAuto(str)
Dim mystring As String = Marshal.PtrToStringAuto(ptr)
Here, we used StringToHGlobalAuto() method of marshal class that copies the contents of a managed string into unmanaged memory. We collect the memory address in IntPtr structure. To read the string back we used PtrToStringAuto() method of Marshal class that returns a string at specified memory location.
Writing and reading structures
In the next example we will see how to write and read structures using Marshal class and IntPtr structure.
Public Structure Point
Dim x As Integer
Dim y As Integer
End Structure
Dim mystruct1,mystruct2 As Point
mystructure.x=100
mystructure.y=200
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(mystruct))
Marshal.StructureToPtr(mystruct, ptr, True)
mystruct2 = Marshal.PtrToStructure(ptr, New Point().GetType)
Here, we will be using a structure called Point. This code is similar to example 1 but note how we used SizeOf method of Marshal that returns size of our structure variable. The StructureToPtr method accepts the structure variable to be written, the IntPtr instance that will be returned and flag indicating whether Marshal class should call DestroyStructure method. It is recommended that you set this flag to True as setting it to false can cause memory leaks. Finally, we used PtrToStructure method of Marshal class to read the structure back.
Writing and reading objects
Up till now we saw how to read and write value types using Marshal and IntPtr class. The managed objects work a bit differently. You need to use GCHandle to read and write these objects. Following code shows how:
Public Class Employee
Public Name As String
Public Salary As Decimal
End Class
Dim gh As GCHandle
Dim emp As New Employee
emp.Name = "John"
emp.Salary = 12345.67
gh = GCHandle.Alloc(emp)
Dim emp2 As Employee = gh.Target
gh.Free()
Here, we used GCHandle (Garbage Collector Handle) class to allocate and de allocate memory to our instance of employee class. The shared (static) Alloc method accepts the object to store in the memory and returns an instance of GCHandle class. We can point to the reference of our object via Target property of this object. Once we are done with the object we can release memory consumed by it using Free method of GCHandle instance.
Summary
Pointer like operations and unmanaged operations were never easy in VB.NET. However, IntPtr and GCHandle structures and Marshal class allow you to achieve similar effect.