Reputation: 4382
I want to programmatically exit Chrome.exe via C++ on Windows OS.
Now, I use Terminate() API, however, as Chrome.exe has many instances, when kill Chrome.exe, it will popup an UI which explains that an extension crashed. I think this is because we kill extension process.
So is there any graceful method that can make Chrome.exe exit. Thanks!
Upvotes: 2
Views: 1188
Reputation: 11
As of 2024, such terminations crash Chrome, and you will see a popup with a proposition to restore tabs from the previous session.
I'd recommend sending the message "WM_CLOSE" to the chome processes using EnumWIndowsProc, so Chrome can handle the closing by itself.
Code sample:
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD windowProcessId;
GetWindowThreadProcessId(hwnd, &windowProcessId);
DWORD targetProcessId = static_cast<DWORD>(lParam);
if (windowProcessId == targetProcessId)
{
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return true;
}
void sendMsgToProc(DWORD pId)
{
EnumWindows(EnumWindowsProc, static_cast<LPARAM>(processId));
}
However, there is an issue. If the user is editing text or writing an email, the browser may show a warning popup indicating that the user may lose their data, asking for consent to close Chrome.
Upvotes: 1
Reputation: 26
int CTestDlg::KillChromeProcess()
{
HANDLE hProcessHandle;
ULONG nProcessID;
HWND TheWindow;
TheWindow = ::FindWindow( _T("Chrome_WidgetWin_1"), NULL );
::GetWindowThreadProcessId( TheWindow, &nProcessID );
hProcessHandle = ::OpenProcess( PROCESS_TERMINATE, FALSE, nProcessID );
return ::TerminateProcess( hProcessHandle, 4 );
}
"Chrome_WidgetWin_1" you can use Spy++ to get.
Upvotes: 1