Non-Terminating Process Git Bash (C#) - 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!

Related

Command line command won't run

I am trying to make a UI for some of the new Windows 10 Commands that have become useful to us. I am trying to make a UI that has three text boxes, one for the computer name, the username, and the message in order to utilize the msg command. When I run the command on my normal CMD, I would type msg /server:matthewl mlynch "hello",this will display a little message on my screen saying hello. However, when I try to run it from my c# ui, I get this
'msg' is not recognized as an internal or external command,
operable program or batch file.
As you can probably tell, this is not correct as it works when I launch command line and manually input but not when the UI runs the command.
Nevermind, coworker helped me, it was a setting in Project>Project Properties>Build>Platform Target and changed that to x64. Apparently it wasn't opening C:\Windows\System32, it was actually opening C:\Windows\Syswow64 but labeling itself as system32.
To run these commands, normally you'd use the System.Diagnostics.Process class to start the cmd.exe process, and then pass the command to the window:
Process.Start("cmd.exe",
$"/C msg /server:{txtServer.Text} {txtUser.Text} \"{txtMessage.Text}\"");

Automate commands in git cmd?

Is there a way to automate input in git-cmd without actually typing in the input command? Let's say write the input to a script to run automatically?
start git.cmd
connect to git server.
clone git repository.
input password.
I have this exact same situation and I automated it using expect
#!/usr/bin/expect
set timeout 600
log_user 0
spawn git clone http://myuser#myserver/group/repo.git tmp/repo
expect "Password for 'http://myuser#myserver':"
send "mypassword\r"
expect eof
Key was setting timeout appropriately.
I see that expect for Windows is available.
Assuming "git-cmd" is the Windows command shell setup for Git then all you need to do is write a command script (or batch file). You can find general instructions here. It is worth noting that the Git bin folder will need to be in your path. The Git command to clone a repository is described here. As was mentioned in another answer you could write a program to spawn a command shell and run the command script, but that seems the long way around.
Hope that helps.

Execute a WinDbg command through 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

c# program works from cmd prompt but not run separately?

I would post a snippet, but I honestly have no idea what part of my code could possibly be doing this. The program is sizable, I don't want to make you all wade through it. What kinds of things could possibly be the cause of this? Everything works perfectly when called from the command prompt: "readoo.exe". But when I click the exe in its file. . . "readoo.exe has encountered a problem and needs to close. . ."
this is intended to eventually be a scheduled task -> i'm worried, will it work?
i've never debugged, all i've ever used is notepad. I am learning, and feel that this strengthens my understanding of a project.
it crashes nearly immediately. there are no shortcuts, though the file paths are relative.
trying this method: shortcut -> properties -> shortcut -> Start In. I don't have a "shortcut" option
my program reads log files, parses, and creates 4 new files based on the found content
Microsoft Error Report says file not found. But how can this be? the files are there, albeit relative.
Take a copy of your project, and then start hacking bits out of it. When it no longer crashes, you've removed the bit causing the problem.
At what point does it fail when you double-click on it? Immediately, or only when you take a certain action?
You could also add a lot of logging to it, which could indicate where the problem is too.
This is probably looking for a dll that it can't find or is finding a different version from what it wants.
You could try Process Monitor or Process Explorer from sysinternals to see what dlls it loads when it does work and where it finds them.
Try putting a System.Diagnostics.Debugger.Break()call as the first thing in Main() and you'll be asked to attach a debugger - this should definitely show you what is different betweent the 2 invocations.
I would start with identifying what is different in the two methods of execution. Is there a shortcut modifying anything?
The starting directory?
The execution account?
command line arguments?
There are 2 things that it could be:
The current directory could be different when you click on the program or run from the command prompt.
The settings and path could be different when you click on the programe you are using the standard command prompt, are you opening the visual studio command prompt when you run the program from the prompt.
If your application relies on some file that should be on the same path of that exe, that can occurr.
You will have to change the properties of the exe (or shortcut to the exe) to "Start In" the directory where your exe is. For a shortcut, right click on the shortcut -> properties -> shortcut -> Start In.
I guess that is what I think could be the cause.
EDIT: Add a Console.ReadLine towards the end of your code to make it pause for you to see any exception thrown. That should help when you run it using windows explorer.
Put a try/catch around your code and output the exception message to the console in the catch block. That should give you some clues.

Unlock a file with unlocker from a WinForms App?

I am trying to unlock a file from a C# program, using unlocker.
In my UI, I put a button to unlock the file the app couldn't delete. When the user pushes the button, I want unlocker (the famous app) to be opened.
I have read about in the Unlocker web, and there is some explanations about the commandline to use but nothing works.
I write the following code but nothing happens:
"c:\Program Files\unlocker\unlocker.exe" -L "PATHFORTHEFILE.doc"
Nothing happens. I have tried without parameters and with -LU.
Any idea?
Something more efficient than unlocker to integrate it with software?
If unlocker comes with parameters -L and -U, I don't think L would be the one you want to unlock with. Probably U is for unlocking ;)
If you have any control of the application that is locking the file, it would be a better solution to have that program free the file rather than a third party app rip it away like this.
Look at the documentation for the System.Diagnostics.Process class and the related ProcessStartInfo class.
unnlocker.exe c:\song.mp3 -s -d
-s unlock
-d delete

Categories