How to send Ctrl-C to a console-application

  • Thread starter Thread starter ThomasG
  • Start date Start date
T

ThomasG

Hi,

i've got a main (win-)application which starts a consoleapp to do some work:

Dim psi As New ProcessStartInfo
psi.UseShellExecute = False
psi.CreateNoWindow = True
psi.RedirectStandardInput = True
psi.RedirectStandardError = True
psi.RedirectStandardOutput = True
psi.FileName = "C:\SomeExe.exe"
psi.Arguments = ""

' start process
_proc = Process.Start(psi)

I'm able to send something to the standardinput of this app:

'_proc.StandardInput.WriteLine("blah")
'_proc.StandardInput.Flush()

My problem is to send Ctrl-C. This is necessary to shutdown this app.
Just killing the process etc. is not possible (dataloss...).

Maybe someone has an idea or some lines of code how to manage this?


Thomas
 
Matthias,

thank you for your fast reply.

I can't get GenerateConsoleCtrlEvent to work.

' declarations
Public Declare Auto Function GenerateConsoleCtrlEvent Lib
"kernel32" _
(ByVal dwCtrlEvent As ConsoleCtrlEvent, _
ByVal dwProcessGroupId As Integer) _
As Boolean

Public Enum ConsoleCtrlEvent As Integer
CTRL_C = 0
CTRL_BREAK = 1
CTRL_CLOSE = 2
CTRL_LOGOFF = 5
CTRL_SHUTDOWN = 6
End Enum

' call
GenerateConsoleCtrlEvent(ConsoleCtrlEvent.CTRL_C, 0)

When executing the code in debugmode, it stops at the call without any
description. Looks like a breakpoint. (yellow highlight etc.)

Is there something wrong with declaration/call?

At the moment, my main application is a windows program. The
childprocesses are consoleapplications. I think, I've to change the
whole architecture to get parent and child in the same processgroup.

Is this right?

How can this be done? (additional consoleapp just for sending ctrl-c?)

ThomasG
 
Back
Top