A
Adam Clauss
I am building a GUI to wrap around some of the information/abilities
contained within the program netsh.
I have figured out how to use redirect the standardinput and standardoutput
so that I can read/write from my application.
The "easy" way to do this would be to spawn an instance of netsh everytime I
need to execute a command, write the command to the input (including a quit
command), and call ReadToEnd on the output. And this does work... except
that netsh is rather slow to startup and I am issuing many different
commands to it. This results in a ugly DOS box popping up everytime I want
to do something. Makes the whole program slow.
So instead, I would rather just launch ONE instance of netsh and read/write
to it and not have it actually exit in between commands. In theory this
works, but I'm finding it hard to implement in practice. First, ReadToEnd
(or any of the Read* functions for that matter) block until the application
exits. Per the documentation, I would say that they should return null (or
in some cases -1) in the event that they reached the end, but instead they
just hang.
My attempted solution was to use Peek() to see if there was anything there,
and if so, Read() it. I then repeat this process one character at a time
until Peek fails, at which point I go on and issue the next command.
This too does not seem to work quite right... running the app usually
results in only part of the output being returned, and it apparently never
finishes getting the output for the next command.
If I debug in and step through line by line, I DO get all the output, but
upon issuing my second command, Peek() never returns a positive value
indicating there is a character. (After writing the command to the output,
I have a "while (StandardOutput.Peek() == -1) Sleep()" to ensure that the
command has time to execute before I start trying to read the output. After
this is when I loop on the Peek and read it character by character.
This feels like it is just getting too complicated for something I thought
would be rather simple.
Any ideas on how to better go about this?
Thanks!
contained within the program netsh.
I have figured out how to use redirect the standardinput and standardoutput
so that I can read/write from my application.
The "easy" way to do this would be to spawn an instance of netsh everytime I
need to execute a command, write the command to the input (including a quit
command), and call ReadToEnd on the output. And this does work... except
that netsh is rather slow to startup and I am issuing many different
commands to it. This results in a ugly DOS box popping up everytime I want
to do something. Makes the whole program slow.
So instead, I would rather just launch ONE instance of netsh and read/write
to it and not have it actually exit in between commands. In theory this
works, but I'm finding it hard to implement in practice. First, ReadToEnd
(or any of the Read* functions for that matter) block until the application
exits. Per the documentation, I would say that they should return null (or
in some cases -1) in the event that they reached the end, but instead they
just hang.
My attempted solution was to use Peek() to see if there was anything there,
and if so, Read() it. I then repeat this process one character at a time
until Peek fails, at which point I go on and issue the next command.
This too does not seem to work quite right... running the app usually
results in only part of the output being returned, and it apparently never
finishes getting the output for the next command.
If I debug in and step through line by line, I DO get all the output, but
upon issuing my second command, Peek() never returns a positive value
indicating there is a character. (After writing the command to the output,
I have a "while (StandardOutput.Peek() == -1) Sleep()" to ensure that the
command has time to execute before I start trying to read the output. After
this is when I loop on the Peek and read it character by character.
This feels like it is just getting too complicated for something I thought
would be rather simple.
Any ideas on how to better go about this?
Thanks!