I need to execute a WinDbg command through C#. To be more clear, open the WinDbg through C# in background, execute a command in the windbg command line and close the windbg application. Does C# provide any APIs for doing this ??
-
What have you found so far? – Jim Aho Oct 14 '14 at 06:28
-
Are you trying to execute something in windbg against your *own* process or another one? – Damien_The_Unbeliever Oct 14 '14 at 06:30
-
I am creating custom commands using WinDbg. Also instead going to the WinDbg command line and executing those commands, I need to execute them when clicking a button in GUI created using C# – Avinesh Oct 14 '14 at 14:00
-
Please be more specific. Is this a live debugging question or a dump analysis question. If live debugging: is the C# application that sends the command to WinDbg also the one that you want to debug (i.e. C# application debugging itself)? This questions sounds like a XY-problem. Please describe in more detail what you actually want to achieve. Is it really the only way to start WinDbg? Or would it be ok to achieve the same result in a different way? – Thomas Weller Oct 14 '14 at 18:56
-
I am doing a live debugging, C# needs to send the command to be executed to WinDbg and get the executed command's output. – Avinesh Oct 15 '14 at 03:32
6 Answers
If you really want the GUI, just use the -c
switch to pass a command to the window. An example command line to attach to Calculator and dump the stack:
windbg.exe -pn calc.exe -c "kb"
This leaves Windbg open and attached to calculator, displaying the result of running kb
.
If you don't need the Windbg GUI and just need to execute a command to get the output of it, use CDB (the command-line debugger equivalent).
cdb.exe -pn calc.exe -c "kb; qd"
So here, the command in quotes after -c
is executed after attaching to the process named (due to -pn
) "calc.exe".
In either case, if you instead have the process ID (PID), use -p
:
cdb.exe -p 1164 -c "kb; qd"
As for running it from C#, the easiest way is to start a Process and read the console output. See this answer for a ready-to-go solution.

- 1
- 1

- 23,334
- 2
- 57
- 88
-
In the last solution provided by you i.e. creating a Process is fine but any idea of how to supply the command to be executed as input to the WinDbg – Avinesh Oct 14 '14 at 15:14
-
Yeah great answer, this seems to work but any other approach without creation of processes ?? – Avinesh Oct 15 '14 at 03:11
-
How else would you launch a process without creating a Process? What is it about creating processes that you don't want to do? – Patrick Quirk Oct 15 '14 at 12:21
-
Yeah I cannot obviously without launching a process !! But I don't want to use other debuggers except WinDbg .. – Avinesh Oct 20 '14 at 05:35
No, C# does not have API to run or control WinDbg.
You can use general purpose Process.Start to launch WinDbg and pass script to it.

- 98,904
- 14
- 127
- 179
-
Can't I make use of DLL Extension written already, and import that DLL in C# ?? – Avinesh Oct 14 '14 at 10:01
-
-
You mean what are [WinDbg command line options](http://msdn.microsoft.com/en-us/library/windows/hardware/ff561306%28v=vs.85%29.aspx)? Or [how to pass command line options to Process.Start](http://stackoverflow.com/questions/5168612/launch-program-with-parameters)? Or something else? – Alexei Levenkov Oct 14 '14 at 17:20
You can execute C# code from Windbg command line, and an approach is to write a plugin for Windbg. Not sure this is the approach you're after but if so here is a post how to do.

- 46
- 5
-
Calling C# from WinDbg, I can write a DLL WinDbg Extension but my requirement is executing a WinDbg command from C# application. – Avinesh Oct 14 '14 at 09:59
see:
https://powerdbg.codeplex.com/
it is not C#, but it is >net and may be you will find out an approach
If you are ready to change С# to python, see https://pykd.codeplex.com
And at last, you can use native dlls DbgEng/DbgHlp from your managed code
Naitive debugger engine cannot be used unless you wrap it around c++/cli checkout mdbglib https://mdbglib.codeplex.com This is a debugger written such a fashion

- 731
- 5
- 15
You can use ClrMD for C# API Here is the sample code to get started
C# code snippet on how to use it
// Create Debugger instance and call Execute for any Windbg Command
using (DbgEngine dbg = new DbgEngine(DumpFileName))
{
Console.WriteLine(dbg.Execute(".time"));
Console.WriteLine(dbg.Execute("~"));
Console.WriteLine(dbg.Execute(".sympath"));
}
Simple Debugger to run Windbg Commands and also query .NET CLR Runtime data in C# https://github.com/sukesh-ak/AutoDebug

- 639
- 5
- 7