Execute a WinDbg command through C# - c#

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 ??

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.

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.

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.

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

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

Related

Non-Terminating Process Git Bash (C#)

As a fun little project, I am trying to use C# to operate the bash.exe provided by Git. I want the process to behave just as if I ran it in the Git Bash Application. By this, I mean I want to be able to execute command and get the output of said commands (i.e. if I enter the command "curl --version", I want to get the same output as the image here and be able to store it in a variable)
I have come very close to accomplishing this with the code here. However, with some commands, I find that the Process in C# never terminates. For example, if I try to execute the command "curl --help", I find the the Process never exits where "curl --version" did. As a quick hack, I figured out that I could fix this by changing the command to
curl --help >> output.txt
and then reading the .txt file. This does cause the command to exit and to write the correct output to the file, however, I don't like having to do this and I am sure there is a better solution to make commands of this sort exit properly. Thanks for the help!

How to launch a JVM from C#

I would like to launch a java application from C# but am unsure of the proper way to do it. I can do it from a bat file:
java -cp ".;other_classes_location" classname
How does this get turned into C#?
What you have posted is a command line (likely executed in a command shell)
you cannot turn this into C# code
I guess you are looking for a way to execute that command using C#. In this case the answer is the below code
System.Diagnostics.Process.Start("yourPath\java.exe", "Command Line Arguments");
Use Process.Start.
Examples here.

How to run exe in mono in the background

Okay, so I have a .net exe, and it runs when I type mono myexe.exe. However, if I want to use another command or close the terminal window the app stops executing.
I have tried using mono myexe.exe & and it runs and showing [8] 20078 etc., but once I type something else it shows [8]+ Stopped, and then executes the command I typed in.
Any ideas?
how about nohup mono myexe.exe &
If you want to use & then look into outputting the result to a file.
nohup mono program.exe > program.out &
The right way of doing this would be to create a daemon with your linux distro.
You can also run it as an service using this line of code:
mono-service -l:/tmp/myservice.lock ./ServiceDaemon.exe
where -l:LOCKFILE specifies the file to use for locking. When you look into your lock file, you will see the process id. The process id you can use to kill the service, whenever you need to terminate it.
Source: blog.chudinov.net

Run an executable present in Windows Path using C#

I'm trying to run some commands, like rails test, using a C# command line. I tried using How To: Execute command line in C#, get STD OUT results but I'll need full path to the rails executable for that to work. Is there any alternative that will find work just like the windows command line does?
If you can P/Invoke, you could locate the executable with PathFindOnPath. A quick google doesn't show a C# equivalent.
without P/Invoke, Environment.GetEnvironmentVariable("Path").Split(";") should give you a list of paths to probe.
However, this is not the entire resolution used by ShellExecute or even the console.
I believe if you have UseShellExecute set to true in the ProcessStartInfo used to start the process, it'll use the path. Haven't checked it yet - will do so when I get a chance.

C# Application Calling Powershell Script Issues

I have a C# Winforms application which is calling a simple powershell script using the following method:
Process process = new Process();
process.StartInfo.FileName = #"powershell.exe";
process.StartInfo.Arguments = String.Format("-noexit \"C:\\Develop\\{1}\"", scriptName);
process.Start();
The powershell script simply reads a registry key and outputs the subkeys.
$items = get-childitem -literalPath hklm:\software
foreach($item in $items)
{
Write-Host $item
}
The problem I have is that when I run the script from the C# application I get one set of results, but when I run the script standalone (from the powershell command line) I get a different set of results entirely.
The results from running from the c# app are:
HKEY_LOCAL_MACHINE\software\Adobe
HKEY_LOCAL_MACHINE\software\Business Objects
HKEY_LOCAL_MACHINE\software\Helios
HKEY_LOCAL_MACHINE\software\InstallShield
HKEY_LOCAL_MACHINE\software\Macrovision
HKEY_LOCAL_MACHINE\software\Microsoft
HKEY_LOCAL_MACHINE\software\MozillaPlugins
HKEY_LOCAL_MACHINE\software\ODBC
HKEY_LOCAL_MACHINE\software\Classes
HKEY_LOCAL_MACHINE\software\Clients
HKEY_LOCAL_MACHINE\software\Policies
HKEY_LOCAL_MACHINE\software\RegisteredApplications
PS C:\Develop\RnD\SiriusPatcher\Sirius.Patcher.UI\bin\Debug>
When run from the powershell command line I get:
PS M:\> C:\Develop\RegistryAccess.ps1
HKEY_LOCAL_MACHINE\software\ATI Technologies
HKEY_LOCAL_MACHINE\software\Classes
HKEY_LOCAL_MACHINE\software\Clients
HKEY_LOCAL_MACHINE\software\Equiniti
HKEY_LOCAL_MACHINE\software\Microsoft
HKEY_LOCAL_MACHINE\software\ODBC
HKEY_LOCAL_MACHINE\software\Policies
HKEY_LOCAL_MACHINE\software\RegisteredApplications
HKEY_LOCAL_MACHINE\software\Wow6432Node
PS M:\>
The second set of results match what I have in the registry, but the first set of results (which came from the c# app) don't.
Any help or pointers would be greatly apreciated :)
Ben
This is actually not a particularly good way to embed PowerShell within a C# api. There are APIs for that.
You can find an example of them on MSDN, but in your case the could would look something like
PowerShell.Create().AddScript("get-childitem -literalPath hklm:\software").Invoke()
You can also check out this blog post, which will show you how to dot source inside of the API and how to use this API to get at the other data streams in PowerShell.
Hope this helps
Are you running a 64bit version of Windows by chance? It could be a difference in how the two "hives" are being shown. Try forcing your C# app to compile to x86/x64 instead of "Any" in the Project properties. See if that makes any difference.
Also, your command line syntax is a little strange, see the following thread for better details, but you may want to adjust your syntax:
String cmd = "-Command "& { . \"" + scriptName + "\" }";
Process process = new Process();
process.StartInfo.FileName = #"powershell.exe";
process.StartInfo.Arguments = cmd;
process.Start();
Calling a specific PowerShell function from the command line
I did look at alternative methods for calling Powershell and came across that API.
Am I right in thinking though that they rely on a Microsoft SDK??
I'm not really a fan of dependencies on external SDKs. I work in a rather large company and ensuring that the SDK is installed on all of the developers machines would be a nightmare.
If I'm wrong in my thinking, I am open to a better way of calling Powershell. I didn't particularly like calling the script as a separate process and would like the ability to have values returned from the script.
Ben

Categories