Create a minidump from VB.Net

I am a programming hack, but i do need to prototype tools for support.  This month, I am working on a little notification tray utility that will monitor the processes that have mspst32.dll loaded.  This is becuase if those processes don't exit properly, it will cause outlook to not trust your datafile, and the next time you launch you have to sit through a lengthy data file check.  So to help monitor and diagnose those problems, I am writing a tool.  As part of that tool, I wanted to be able to create a memory dump of a process that would not close.  I looked forever for a sample of using MiniDumpWriteDump API in VB.Net without success.  With the help of my incredible dev support guru Matt Stehle (https://blogs.msdn.com/mstehle/), was able to convert a C# example and get it working in Vb.Net.  Here is the snippet

 

'Imports

Imports System.Diagnostics
Imports System.IO 

' The declaration and enum for the dump types:

 Private Declare Function MiniDumpWriteDump Lib "dbghelp.dll" (ByVal hProcess As IntPtr, ByVal ProcessId As Int32, ByVal hFile As IntPtr, ByVal DumpType As mydumptypes, ByVal ExceptionParam As IntPtr, ByVal UserStreamParam As IntPtr, ByVal CallackParam As IntPtr) As Boolean

Enum mydumptypes
        normal = 1
        full = 2
End Enum

' pseduocode to get a process and create a filename. 

dumpprocess = Process.GetProcessById(<PID of the process>)
onefilename = Path.GetTempFileName.ToString
onefile = New FileStream(onefilename, FileMode.Create)

'Call the API
result = MiniDumpWriteDump(dumpprocess.Handle, dumpprocess.Id, onefile.SafeFileHandle.DangerousGetHandle, mydumptypes.normal, 0, 0, 0)

There is some way to add GetLastError support to the API declaration so thats on my list to add.  The API returns true or false, so if there was an error you have to retrieve it with Getlasterror in some way.  Also note you need the "debug" priviledge to be able to write dumps.  If you are a local machine admin, you get that priviledge for free.