Created
September 13, 2024 21:01
-
-
Save yuyoyuppe/d568b14ea023422c04bec6d4cbbc0844 to your computer and use it in GitHub Desktop.
C# snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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