How to gracefully close another application?

I have a C ++ / CLI installation application that should close another application that I wrote, replace the .exe and dll application and restart the executable.

First of all, I need to close this window on the following lines:

HWND hwnd = FindWindow(NULL, windowTitle); if( hwnd != NULL ) { ::SendMessage(hwnd, (int)0x????, 0, NULL); } 

those. find the appropriate window title (which works) ... but then what message do I need to send a remote window to ask to close it?

... or is there a more .net-ish way to do this without accessing the windows API?

Do not forget that I am limited to .net 2.0

+4
source share
5 answers
+5
source

You can call WM_CLOSE using SendMessage .

 [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(int hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam); 

See http://boycook.wordpress.com/2008/07/29/c-win32-messaging-with-sendmessage-and-wm_copydata/ for sample code.

+3
source

Recommendations from MSDN

  • Send WM_QUERYENDSESSION with the lParam parameter to ENDSESSION_CLOSEAPP .
  • Then send WM_ENDSESSION with the same lParam.
  • If this does not work, send WM_CLOSE .
+2
source

In case someone wants to follow a more-networked way of donig things, you can do the following:

 using namespace System::Diagnostics; array<Process^>^ processes = Process::GetProcessesByName("YourProcessName"); for each(Process^ process in processes) { process->CloseMainWindow(); // For Gui apps process->Close(); // For Non-gui apps } 
+1
source

UPDATE

Use WM_CLOSE. I was wrong.

-1
source

All Articles