zeromemory (kernel32)
Last changed: pinvoke@winsteps.com-58.166.178.197

.
Summary
The ZeroMemory function fills a block of memory with zeros.

C# Signature:

[DllImport("Kernel32.dll", EntryPoint="RtlZeroMemory", SetLastError=false)]
static extern void ZeroMemory(IntPtr dest, IntPtr size);

VB Signature:

Declare Auto Sub ZeroMemory Lib "Kernel32.dll" _
    Alias "RtlZeroMemory" (ByVal dest As IntPtr, ByVal size As IntPtr)

VB.NET Signature:

<DllImport("kernel32.dll")> _
   Public Shared Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As IntPtr)
   End Sub

User-Defined Types:

None.

Alternative Managed API:

Use Marshal.Copy() to copy an zero-filled managed array onto the unmanaged heap.

Notes:

VB developers might want to check the VB declaration thoroughly...

Tips & Tricks:

Please add some!

Sample Code:

    Imports System.Runtime.InteropServices
    Public Class Form1
    <DllImport("kernel32.dll")> _
    Public Shared Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As IntPtr)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Const elements As Integer = 50
    Dim bytearray(elements) As Byte ' has elements+1 bytes
    bytearray(0) = 1
    bytearray(elements - 1) = 2
    bytearray(elements) = 3
    Debug.Print(CStr(bytearray(0)) + " " + CStr(bytearray(elements - 1)) + " " + CStr(bytearray(elements)))
    Dim gch As GCHandle = GCHandle.Alloc(bytearray, GCHandleType.Pinned)
    Dim abytearray As IntPtr = gch.AddrOfPinnedObject()
    Dim absize As IntPtr = CType(elements, IntPtr) ' deliberately one less than size of bytearray
    ZeroMemory(abytearray, absize)
    gch.Free()
    ' show that bytearray has been filled with 0s except for the last element
    Debug.Print(CStr(bytearray(0)) + " " + CStr(bytearray(elements - 1)) + " " + CStr(bytearray(elements)))
    End Sub

End Class

Documentation
ZeroMemory on MSDN