Show exit prompt in Windows when run from console
authorJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 13 Jan 2017 10:22:57 +0000 (11:22 +0100)
committerJoseph C. Lehner <joseph.c.lehner@gmail.com>
Fri, 13 Jan 2017 10:22:57 +0000 (11:22 +0100)
main.c

diff --git a/main.c b/main.c
index 2830cf7f68c4443d2dd1cc5925e9a31c700e6c6e..d17cb3ac6b62768b63e527b4b6d4d7b21835e017 100644 (file)
--- a/main.c
+++ b/main.c
@@ -104,6 +104,19 @@ void require_admin()
 
        fprintf(stderr, "Warning: failed to check administrator privileges\n");
 }
+
+void show_exit_prompt()
+{
+       HWND win = GetConsoleWindow();
+       if (!win) {
+               return;
+       }
+
+       if (GetCurrentProcessId() == GetWindowThreadProcessId(win, NULL)) {
+               printf("\nPress any key to exit\n");
+               getch();
+       }
+}
 #else
 void require_admin()
 {
@@ -139,6 +152,8 @@ int main(int argc, char **argv)
        char *windir = NULL;
        WSADATA wsa;
 
+       atexit(&show_exit_prompt);
+
        val = WSAStartup(MAKEWORD(2, 2), &wsa);
        if (val != 0) {
                win_perror2("WSAStartup", val);