+ /*
+ Then tell the window that the user is logging off and it should exit
+ without worrying about saving any data.
+ */
+ k->signalled |= PostMessage(window, WM_ENDSESSION, 1, ENDSESSION_CLOSEAPP | ENDSESSION_CRITICAL | ENDSESSION_LOGOFF);
+
+ return 1;
+}
+
+/*
+ Try to post a message to the message queues of threads associated with the
+ given process ID. Not all threads have message queues so there's no
+ guarantee of success, and we don't want to be left waiting for unsignalled
+ processes so this function returns only true if at least one thread was
+ successfully prodded.
+*/
+int kill_threads(char *service_name, kill_t *k) {
+ int ret = 0;
+
+ /* Get a snapshot of all threads in the system. */
+ HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
+ if (! snapshot) {
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATETOOLHELP32SNAPSHOT_THREAD_FAILED, service_name, error_string(GetLastError()), 0);
+ return 0;
+ }
+
+ THREADENTRY32 te;
+ ZeroMemory(&te, sizeof(te));
+ te.dwSize = sizeof(te);
+
+ if (! Thread32First(snapshot, &te)) {
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_THREAD_ENUMERATE_FAILED, service_name, error_string(GetLastError()), 0);
+ return 0;
+ }
+
+ /* This thread belongs to the doomed process so signal it. */
+ if (te.th32OwnerProcessID == k->pid) {
+ ret |= PostThreadMessage(te.th32ThreadID, WM_QUIT, k->exitcode, 0);
+ }
+
+ while (true) {
+ /* Try to get the next thread. */
+ if (! Thread32Next(snapshot, &te)) {
+ unsigned long error = GetLastError();
+ if (error == ERROR_NO_MORE_FILES) break;
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_THREAD_ENUMERATE_FAILED, service_name, error_string(GetLastError()), 0);
+ return ret;
+ }
+
+ if (te.th32OwnerProcessID == k->pid) {
+ ret |= PostThreadMessage(te.th32ThreadID, WM_QUIT, k->exitcode, 0);
+ }
+ }
+
+ return ret;
+}
+
+/* Give the process a chance to die gracefully. */
+int kill_process(char *service_name, HANDLE process_handle, unsigned long pid, unsigned long exitcode) {