CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2010
    Posts
    26

    [RESOLVED] Create A Dump File for a Running Process

    Hi

    In Windows 7 if I launch Task Manager and select a process (eg:Notepad.exe) I can right click on this and select "Create Dump File".

    This creates a dump of the file to my hard drive.

    I would like to do this programmatically.

    Any Help would be appreciated.


    (VB.Net 2008 - framework 3.5)

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Talking Re: Create A Dump File for a Running Process

    You guys are keeping me honest

    First we had the monitor Brightness Thread, now we have this interesting thread. OK, enough about me

    This is possible through the MiniDumpWriteDump API available inside the dbghelp.dll file.

    Its signature looks like this :

    Code:
        'Dump API
        <DllImport("dbghelp.dll")> _
        Private Shared Function MiniDumpWriteDump(ByVal hProcess As IntPtr, _
                                                  ByVal ProcessId As Int32, _
                                                  ByVal hFile As IntPtr, _
                                                  ByVal DumpType As DumpTypes, _
                                                  ByVal ExceptionParam As IntPtr, _
                                                  ByVal UserStreamParam As IntPtr, _
                                                  ByVal CallackParam As IntPtr) As Boolean
    I quickly slapped something together for you It takes the current process, and writes the dump information into a file called C:\dumped.txt. Here is the full code :

    Code:
    Imports System.Diagnostics
    Imports System.IO
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        'Dump API
        <DllImport("dbghelp.dll")> _
        Private Shared Function MiniDumpWriteDump(ByVal hProcess As IntPtr, _
                                                  ByVal ProcessId As Int32, _
                                                  ByVal hFile As IntPtr, _
                                                  ByVal DumpType As DumpTypes, _
                                                  ByVal ExceptionParam As IntPtr, _
                                                  ByVal UserStreamParam As IntPtr, _
                                                  ByVal CallackParam As IntPtr) As Boolean
        End Function
    
        'Various Dump Types
        Enum DumpTypes
            DumpNormal = 0
            DumpWithDataSegs = 1
            DumpWithFullMemory = 2
            DumpWithHandleData = 4
            DumpFilterMemory = 8
            DumpScanMemory = 10
            DumpWithUnloadedModules = 20
            DumpWithIndirectlyReferencedMemory = 40
            DumpFilterModulePaths = 80
            DumpWithProcessThreadData = 100
            DumpWithPrivateReadWriteMemory = 200
            DumpWithoutOptionalData = 400
            DumpWithFullMemoryInfo = 800
            DumpWithThreadInfo = 1000
            DumpWithCodeSegs = 2000
        End Enum
    
        Private Sub DumpToFile(ByVal DumpFile As String)
    
            'Create An IO Stream
            Dim ProcDumpFile As IO.FileStream = Nothing
    
            'Check Existance
            If (IO.File.Exists(DumpFile)) Then
                ProcDumpFile = IO.File.Open(DumpFile, IO.FileMode.Append)
            Else
                ProcDumpFile = IO.File.Create(DumpFile)
            End If
    
            'Get Current Process
            'Here You Could Get Another Process
            Dim ProcToDump As Process = Process.GetCurrentProcess()
    
            'Get Dump Info
            'And Write Dump Info
            MiniDumpWriteDump(ProcToDump.Handle, _
                              ProcToDump.Id, _
                              ProcDumpFile.SafeFileHandle.DangerousGetHandle(), _
                              DumpTypes.DumpNormal, _
                              IntPtr.Zero, _
                              IntPtr.Zero, _
                              IntPtr.Zero)
    
            'Close File
            ProcDumpFile.Close()
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'Call Sub To Write Dump Info
            'Into C:\Dumped.txt
            DumpToFile("C:\Dumped.txt")
    
        End Sub
    End Class
    Short and sweet I did this in VB 2010 ( TurboBob, please say which version of VB you are using in the future, it is explained here : http://www.codeguru.com/forum/showthread.php?t=403073 )

    I am attaching a full working project here for you ( as a .zip file ). This code should work in VB 2005 and 2008 as well. As I've said, you could use a different process as well, instead of the current process, but this should help you get organised

    I hope this helps

    Hannes
    Attached Files Attached Files

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Create A Dump File for a Running Process

    Oh, sorry TB, I only notice now that you have specified the VB version you are using

  4. #4
    Join Date
    Mar 2010
    Posts
    26

    Thumbs up Re: Create A Dump File for a Running Process

    That Does it !

    I was messing around with ReadProcessMemory() and I guessed I was on the wrong track!

    Thanks a million HanneSThEGreaT. I'm sure you put quite some time into "slapping this together". Appreciate It.

  5. #5
    Join Date
    Mar 2010
    Posts
    26

    Re: Create A Dump File for a Running Process

    Quote Originally Posted by HanneSThEGreaT View Post
    Oh, sorry TB, I only notice now that you have specified the VB version you are using
    No problem - I use em all. Actually VB 2008 would not open your project but VB 2010 does it fine.
    Thanks again.

    Marking this Resolved

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Create A Dump File for a Running Process

    I'm glad you got this solved - good work!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured