OSDN Git Service

Some improvements to connectivity check: Start with small timeout and increase the...
[mutilities/MUtilities.git] / src / Terminal_Win32.cpp
index 773f022..03a76f3 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
-// LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
+// MuldeR's Utilities for Qt
+// Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -36,7 +36,6 @@
 #include <QFile>
 #include <QStringList>
 #include <QIcon>
-#include <QLibrary>
 
 //CRT
 #include <iostream>
@@ -73,6 +72,9 @@ static QScopedPointer<std::filebuf> g_fileBuf_stderr;
 //The log file
 static QScopedPointer<QFile> g_terminal_log_file;
 
+//Terminal icon
+static HICON g_terminal_icon = NULL;
+
 ///////////////////////////////////////////////////////////////////////////////
 // HELPER FUNCTIONS
 ///////////////////////////////////////////////////////////////////////////////
@@ -146,6 +148,15 @@ static inline size_t clean_string(char *const str)
        return out;
 }
 
+static inline void set_hicon(HICON *const ptr, const HICON val)
+{
+       if (*ptr)
+       {
+               DestroyIcon(*ptr);
+       }
+       *ptr = val;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // TERMINAL SETUP
 ///////////////////////////////////////////////////////////////////////////////
@@ -173,6 +184,7 @@ static void terminal_shutdown(void)
                if(stdout) freopen_s(&temp[0], "NUL", "wb", stdout);
                if(stderr) freopen_s(&temp[1], "NUL", "wb", stderr);
                FreeConsole();
+               set_hicon(&g_terminal_icon, NULL);
                g_terminal_attached = false;
        }
 }
@@ -407,18 +419,36 @@ void MUtils::Terminal::set_icon(const QIcon &icon)
 
        if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine())))
        {
-               QLibrary kernel32("kernel32.dll");
-               if(kernel32.load())
+               if(const HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(&icon, 16, 16))
                {
-                       typedef DWORD (__stdcall *SetConsoleIconFun)(HICON);
-                       if(SetConsoleIconFun SetConsoleIconPtr = (SetConsoleIconFun) kernel32.resolve("SetConsoleIcon"))
+                       typedef BOOL(__stdcall *SetConsoleIconFun)(HICON);
+                       bool success = false;
+                       if (const SetConsoleIconFun pSetConsoleIconFun = MUtils::Win32Utils::resolve<SetConsoleIconFun>(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon")))
+                       {
+                               const DWORD before = GetLastError();
+                               if (pSetConsoleIconFun(hIcon))
+                               {
+                                       success = true;
+                               }
+                               else
+                               {
+                                       const DWORD error = GetLastError();
+                                       qWarning("SetConsoleIcon() has failed! [Error: 0x%08X]", error);
+                               }
+                       }
+                       if (!success)
                        {
-                               if(HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
+                               const HWND hwndConsole = GetConsoleWindow();
+                               if ((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
                                {
-                                       SetConsoleIconPtr(hIcon);
-                                       DestroyIcon(hIcon);
+                                       SendMessage(hwndConsole, WM_SETICON, ICON_SMALL, LPARAM(hIcon));
+                                       success = true;
                                }
                        }
+                       if (success)
+                       {
+                               set_hicon(&g_terminal_icon, hIcon);
+                       }
                }
        }
 }