471,122 Members | 1,122 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,122 software developers and data experts.

Sending Ctrl+C to Apache

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?

Feb 2 '07 #1
4 6093
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 :)

"ca******@gmail.com" wrote:
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?

Feb 2 '07 #2
On Feb 1, 10:12 pm, TheMadHatter
<TheMadHat...@discussions.microsoft.comwrote:
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!

Feb 3 '07 #3

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

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

"herc" wrote:
On Feb 1, 10:12 pm, TheMadHatter
<TheMadHat...@discussions.microsoft.comwrote:
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!

Feb 5 '07 #4
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

On Feb 5, 3:08 pm, TheMadHatter
<TheMadHat...@discussions.microsoft.comwrote:
You were trying to kill children eh? LOL !!!

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

"herc" wrote:
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!

Feb 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Per Magnus L?vold | last post: by
4 posts views Thread by Scott | last post: by
3 posts views Thread by s99999999s2003 | last post: by
2 posts views Thread by s99999999s2003 | last post: by
3 posts views Thread by Daniel Clark | last post: by
31 posts views Thread by happyse27 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.