Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
SecureZeroMemory (kernel32)
The SecureZeroMemory function fills a block of memory with zeros.

C# Signature:

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

VB Signature:

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

VB.NET Signature:

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

C++ Signature:

WinNT header:

PVOID SecureZeroMemory(

  _In_ PVOID  ptr,
  _In_ SIZE_T cnt


WDM header:




    _Out_writes_bytes_all_(cnt) PVOID ptr,
    _In_ SIZE_T cnt

User-Defined Types:


Alternative Managed API:

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


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 SecureZeroMemory(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
    SecureZeroMemory(abytearray, absize)
    ' 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

This function declaration is InLine, meaning it cannot be called by PinVoke.


Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version