GlobalAlloc





VB.Net


Imports System.Runtime.InteropServices

Module Module1
    Declare Function GlobalAlloc Lib "kernel32" (dwFlags As Integer, dwBytes As Integer) As IntPtr
    Declare Function GlobalFree Lib "kernel32" (hMem As IntPtr) As Integer

    Sub GlobalAlloc_Main()
        Dim hMem As IntPtr = GlobalAlloc(0, 1024) ' Allocate 1024 bytes
        If hMem <> IntPtr.Zero Then
            Dim buffer As Byte() = New Byte(1023) {}
            Marshal.Copy(hMem, buffer, 0, buffer.Length)
            ' Use the buffer...
            GlobalFree(hMem) ' Release the allocated memory
        End If
    End Sub
End Module