Restart explorer.exe shell in C/C++

The easiest way to restart Windows Explorer shell is… sending a TerminateProcess with “2” as exit code.

HANDLE h_explorer;
h_explorer = OpenProcess(PROCESS_TERMINATE, false, pid);
TerminateProcess(h_explorer, 2);
CloseHandle(h_explorer);

“pid” is your Process ID. Since explorer.exe may have multiple processes, you’ll need the one who has “Shell_TrayWnd” Window Class name.

Small example:

HWND hwnd = FindWindow(L"Shell_TrayWnd", NULL);
DWORD pid = {};
GetWindowThreadProcessId(hwnd, &pid);

Easy, right?

3 thoughts on “Restart explorer.exe shell in C/C++”

    • This just for explorer.exe shell.
      Terminate process with 2 tells to the OS (not sure exactly how, but this is how it works when you register explorer.exe as shell) to restart the process

      Reply

Leave a Comment

Enable Notifications    Ok No thanks