Sending Ctrl+C to Apache

  • Thread starter Thread starter cartoper
  • Start date Start date
C

cartoper

I am in a pickel, I MUST start and stop the apache web server in
console mode. The only way to shut down apache cleaning is by
pressing <Ctrl>+C. I have tried everything I know and I cannot
duplicate that in C# code. This is what I have, which is NOT working:

[DllImport("Kernel32.dll")]
public static extern bool GenerateConsoleCtrlEvent(int ctrlEvent, int
processGroupId);
[DllImport("Kernel32.dll")]
public static extern int GetProcessId(IntPtr Process);

int processID = GetProcessId(process.Handle);
GenerateConsoleCtrlEvent(0, processID);

Of course, process IS the apache service the application started.
Anyone have any more thoughts?
 
Have you tried:
System.Windows.Forms.SendKeys?

eg:
IWshRuntimeLibrary.WshShell Shell;

Shell.Run("calc");
Threading.Thread.Sleep(100);
Shell.AppActivate("Calculator");
Shell.SendKeys("101")
Sleep(500);
Shell.SendKeys("*");



or you could show it some tough love:
Process[] Processes = Process.GetProcessesByName("apache");
foreach( Process myProcess in Processes)
{
myProcess.CloseMainWindow(); //not so tough...
myProcess.Kill(); //Oh dear...... You can just imagin what this does.

}

I dont think PInvokes are required, but let me know how it goes :)
 
Have you tried:
System.Windows.Forms.SendKeys?

eg:
IWshRuntimeLibrary.WshShell Shell;

Shell.Run("calc");
Threading.Thread.Sleep(100);
Shell.AppActivate("Calculator");
Shell.SendKeys("101")
Sleep(500);
Shell.SendKeys("*");

or you could show it some tough love:
Process[] Processes = Process.GetProcessesByName("apache");
foreach( Process myProcess in Processes)
{
myProcess.CloseMainWindow(); //not so tough...
myProcess.Kill(); //Oh dear...... You can just imagin what this does.

}

I dont think PInvokes are required, but let me know how it goes :)

I was doing the last thing you suggested, already, that is getting the
process by name and killing them all. The downside to that was that I
also had a develpment instance running as a service and it, too, was
killed.

What I had missed what the CloseMainWindow() call, this works like a
charm! The whole deal is that when you start one instance of apache,
it starts one or more children. When you Kill() the process you
started, the children keep running. But CloseMainWindow() sends the
right info to the parent apache and it closes the other servers
gracefully!

Thanks!
 
You were trying to kill children eh? LOL !!!

Anyhoo.....
Glad to be of some assistance.

herc said:
Have you tried:
System.Windows.Forms.SendKeys?

eg:
IWshRuntimeLibrary.WshShell Shell;

Shell.Run("calc");
Threading.Thread.Sleep(100);
Shell.AppActivate("Calculator");
Shell.SendKeys("101")
Sleep(500);
Shell.SendKeys("*");

or you could show it some tough love:
Process[] Processes = Process.GetProcessesByName("apache");
foreach( Process myProcess in Processes)
{
myProcess.CloseMainWindow(); //not so tough...
myProcess.Kill(); //Oh dear...... You can just imagin what this does.

}

I dont think PInvokes are required, but let me know how it goes :)

I was doing the last thing you suggested, already, that is getting the
process by name and killing them all. The downside to that was that I
also had a develpment instance running as a service and it, too, was
killed.

What I had missed what the CloseMainWindow() call, this works like a
charm! The whole deal is that when you start one instance of apache,
it starts one or more children. When you Kill() the process you
started, the children keep running. But CloseMainWindow() sends the
right info to the parent apache and it closes the other servers
gracefully!

Thanks!
 
Actually, I ran into another interesting issue. When I launch apache
and tell it to hide the process window, CloseMainWindow() does nothing:
( I am assuming it is because there is no main window to close. Oh
well, I will simply minimize the window for early testing and before
release I will fix apache! The docs claim that you can lanuch another
instance to kill the one running, but it doesn't work, I will change
that! hehehehe
 
Back
Top