Skip to content

Instantly share code, notes, and snippets.

@yuyoyuppe
Created September 13, 2024 21:01
Show Gist options
  • Save yuyoyuppe/d568b14ea023422c04bec6d4cbbc0844 to your computer and use it in GitHub Desktop.
Save yuyoyuppe/d568b14ea023422c04bec6d4cbbc0844 to your computer and use it in GitHub Desktop.
C# snippets
public static bool SendMessageToAllProcessWindows(uint processId, uint message = PInvoke.WM_CLOSE) {
Process p = Process.GetProcessById((int)processId);
PInvoke.PostMessage(new HWND(p.MainWindowHandle), message, 0, 0);
bool sent = false;
for (HWND hwnd = PInvoke.GetTopWindow(HWND.Null); !hwnd.IsNull; hwnd = PInvoke.GetWindow(hwnd, GET_WINDOW_CMD.GW_HWNDNEXT)) {
unsafe {
uint windowProcessId;
_ = PInvoke.GetWindowThreadProcessId(hwnd, &windowProcessId);
if (windowProcessId == processId) {
_ = PInvoke.PostThreadMessage((uint)hwnd.Value, message, 0, 0);
sent = true;
}
}
}
return sent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment