Jump to content
Sign in to follow this  
bjustice

Interactive command line using .NET

Recommended Posts

I threw this together, and maybe someone will find it useful.

I needed to be able to interact with cmd.exe a bit more than the native system exec.vi primitive offers.  I used .NET to get the job done.  Some notable capabilities:

- User can see standard output and standard error in real-time
- User can write a command to standard input
- User can query if the process has completed
- User can abort the process by sending a ctrl-C command

Aborting the process was the trickiest part.  I found a solution at the following article:
http://stanislavs.org/stopping-command-line-applications-programatically-with-ctrl-c-events-from-net/#comment-2880

The ping demo illustrates this capability.  In order to abort ping.exe from the command-line, the user needs to send a ctrl-c command.  We achieve this by invoking KERNEL32 to attach a console to the process ID and then sending a ctrl-C command to the process.  This is a clean solution that safely aborts ping.exe.  The best part about this solution is that it doesn't require for any console prompts to be visible.  An alternate solution was to start the cmd.exe process with a visible window, and then to issue a MainWindowClose command, but this required for a window to be visible.

I put this code together to allow for me to better interact with HandbrakeCLI and FFMPEG.  Enjoi

image.png.3961c983fcabfe9a6ed7ce2fd527128b.png

NET_Proc.zip

  • Thanks 1

Share this post


Link to post
Share on other sites

Thx for your sharing sir! But at my current project, I got the a EXE file, and I would write some commands after i execute it, and i can't finished it with SYSTEM Exec or your tool, could you pls help me to figure out it ? the yellow part below are the commands, thx again!

D:\DESKTOP\VIG\BLE>BLE_GET_RSSI.exe
Loading...
<<READY>>
Connect FDB06556E922
<<READY>>
7C1ABEE86B33
Connectng
Connecting to FDB06556E922, Device name: VIG2_BLE_FDB06556E922
Connected to FDB06556E922
Discover Complete
SEND VIG_BYPASS
TX: VIG_BYPASS
<<READY>>
RX: MAC CHK OK
SEND soc_ver?
TX: soc_ver?
<<READY>>
RX: 0.762
quit

D:\DESKTOP\VIG\BLE>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.