]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - Plugson/src/main_windows.c
Fix the order issue in TreeView mode. (#3218)
[Ventoy.git] / Plugson / src / main_windows.c
index e6a0bf47a0f361751e9dca618c4114d8e9976c94..49e89c3f504fdf5a5058d8ab8fd1bfdd385cb171 100644 (file)
@@ -1,4 +1,4 @@
-#include <Windows.h>\r
+#include <Windows.h>\r
 #include <Shlobj.h>\r
 #include <tlhelp32.h>\r
 #include <Psapi.h>\r
@@ -10,8 +10,9 @@
 #include <ventoy_disk.h>\r
 #include <ventoy_http.h>\r
 \r
-char g_ventoy_dir[MAX_PATH];
+char g_ventoy_dir[MAX_PATH];\r
 \r
+static BOOL g_ChromeFirst = TRUE;\r
 static BOOL g_running = FALSE;\r
 static HWND g_refresh_button;\r
 static HWND g_start_button;\r
@@ -44,22 +45,22 @@ typedef enum MSGID
 \r
 const WCHAR *g_msg_cn[MSGID_BUTT] =\r
 {\r
-    L"´íÎó",\r
-       L"ÌáÐÑ",\r
-    L"ÇëÔÚ Ventoy Å̸ùĿ¼ÏÂÔËÐб¾³ÌÐò£¡£¨´æ·ÅISOÎļþµÄλÖã©",\r
-       L"´´½¨ ventoy Ä¿Â¼Ê§°Ü£¬ÎÞ·¨¼ÌÐø£¡",\r
-       L"ventoy Ä¿Â¼´æÔÚ£¬µ«ÊÇ´óСд²»Æ¥Å䣬ÇëÏȽ«ÆäÖØÃüÃû£¡",\r
-       L"ÄÚ²¿´íÎ󣬳ÌÐò¼´½«Í˳ö£¡",\r
-       L"Ë¢ÐÂ",\r
-       L"Æô¶¯",\r
-       L"ֹͣ",\r
-       L"Á´½Ó",\r
-       L"Í˳ö",\r
-\r
-       L"Í£Ö¹ÔËÐкóä¯ÀÀÆ÷Ò³Ãæ½«»á¹Ø±Õ£¬ÊÇ·ñ¼ÌÐø£¿",\r
-       L"µ±Ç°·þÎñÕýÔÚÔËÐУ¬ÊÇ·ñÍ˳ö£¿",\r
-       L"ÇëÏȹرÕÕýÔÚÔËÐеĠVentoyPlugson ³ÌÐò£¡",\r
-       L"ventoy\\plugson.tar.xz Îļþ²»´æÔÚ£¬ÇëÔÚÕýÈ·µÄĿ¼ÏÂÔËÐУ¡",\r
+    L"错误",\r
+       L"提醒",\r
+    L"请在 Ventoy 盘根目录下运行本程序!(存放ISO文件的位置)",\r
+       L"创建 ventoy 目录失败,无法继续!",\r
+       L"ventoy 目录存在,但是大小写不匹配,请先将其重命名!",\r
+       L"内部错误,程序即将退出!",\r
+       L"刷新",\r
+       L"启动",\r
+       L"停止",\r
+       L"链接",\r
+       L"退出",\r
+\r
+       L"停止运行后浏览器页面将会关闭,是否继续?",\r
+       L"当前服务正在运行,是否退出?",\r
+       L"请先关闭正在运行的 VentoyPlugson 程序!",\r
+       L"ventoy\\plugson.tar.xz 文件不存在,请在正确的目录下运行!",\r
 };\r
 const WCHAR *g_msg_en[MSGID_BUTT] =\r
 {\r
@@ -81,6 +82,14 @@ const WCHAR *g_msg_en[MSGID_BUTT] =
        L"ventoy\\plugson.tar.xz does not exist, please run under the correct directory!",\r
 };\r
 \r
+#define UTF8_Log(fmt, wstr) \\r
+{\\r
+    memset(TmpPathA, 0, sizeof(TmpPathA));\\r
+    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, TmpPathA, sizeof(TmpPathA), NULL, NULL);\\r
+    vlog(fmt, TmpPathA);\\r
+}\r
+\r
+\r
 const WCHAR **g_msg_lang = NULL;\r
 \r
 HINSTANCE g_hInst;\r
@@ -93,7 +102,7 @@ void ventoy_log_exit(void);
 \r
 static BOOL OnDestroyDialog()\r
 {    \r
-    ventoy_http_exit();
+    ventoy_http_exit();\r
     ventoy_disk_exit();\r
 #ifndef VENTOY_SIM        \r
     ventoy_www_exit();\r
@@ -118,14 +127,17 @@ static void OpenURL(void)
 \r
        sprintf_s(url, sizeof(url), "http://%s:%s/index.html", g_sysinfo.ip, g_sysinfo.port);\r
 \r
-    for (i = 0; Browsers[i] != NULL; i++)\r
-    {\r
-        if (ventoy_is_file_exist("%s", Browsers[i]))\r
-        {\r
-            ShellExecuteA(NULL, "open", Browsers[i], url, NULL, SW_SHOW);\r
-            return;\r
-        }\r
-    }\r
+       if (g_ChromeFirst)\r
+       {\r
+               for (i = 0; Browsers[i] != NULL; i++)\r
+               {\r
+                       if (ventoy_is_file_exist("%s", Browsers[i]))\r
+                       {\r
+                               ShellExecuteA(NULL, "open", Browsers[i], url, NULL, SW_SHOW);\r
+                               return;\r
+                       }\r
+               }\r
+       }\r
 \r
     ShellExecuteA(NULL, "open", url, NULL, NULL, SW_SHOW);\r
 }\r
@@ -395,7 +407,7 @@ INT_PTR CALLBACK DialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lPara
         {\r
             InitDialog(hWnd, wParam, lParam);\r
             break;\r
-        }        \r
+        }\r
         case WM_CLOSE:\r
         {\r
                        if (g_running)\r
@@ -410,6 +422,7 @@ INT_PTR CALLBACK DialogProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lPara
 \r
             OnDestroyDialog();\r
             EndDialog(hWnd, 0);\r
+                       break;\r
         }\r
     }\r
 \r
@@ -464,14 +477,87 @@ static int ParseCmdLine(LPSTR lpCmdLine, char *ip, char *port)
        return 0;\r
 }\r
 \r
+\r
+\r
+//\r
+//copy from Rufus\r
+//Copyright © 2011-2021 Pete Batard <pete@akeo.ie>\r
+//\r
+#include <delayimp.h>\r
+// For delay-loaded DLLs, use LOAD_LIBRARY_SEARCH_SYSTEM32 to avoid DLL search order hijacking.\r
+FARPROC WINAPI dllDelayLoadHook(unsigned dliNotify, PDelayLoadInfo pdli)\r
+{\r
+       if (dliNotify == dliNotePreLoadLibrary) {\r
+               // Windows 7 without KB2533623 does not support the LOAD_LIBRARY_SEARCH_SYSTEM32 flag.\r
+               // That is is OK, because the delay load handler will interrupt the NULL return value\r
+               // to mean that it should perform a normal LoadLibrary.\r
+               return (FARPROC)LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);\r
+       }\r
+       return NULL;\r
+}\r
+\r
+#if defined(_MSC_VER)\r
+// By default the Windows SDK headers have a `const` while MinGW does not.\r
+const\r
+#endif\r
+PfnDliHook __pfnDliNotifyHook2 = dllDelayLoadHook;\r
+\r
+typedef BOOL(WINAPI* SetDefaultDllDirectories_t)(DWORD);\r
+static void DllProtect(void)\r
+{\r
+       SetDefaultDllDirectories_t pfSetDefaultDllDirectories = NULL;\r
+\r
+       // Disable loading system DLLs from the current directory (sideloading mitigation)\r
+       // PS: You know that official MSDN documentation for SetDllDirectory() that explicitly\r
+       // indicates that "If the parameter is an empty string (""), the call removes the current\r
+       // directory from the default DLL search order"? Yeah, that doesn't work. At all.\r
+       // Still, we invoke it, for platforms where the following call might actually work...\r
+       SetDllDirectoryA("");\r
+\r
+       // For libraries on the KnownDLLs list, the system will always load them from System32.\r
+       // For other DLLs we link directly to, we can delay load the DLL and use a delay load\r
+       // hook to load them from System32. Note that, for this to work, something like:\r
+       // 'somelib.dll;%(DelayLoadDLLs)' must be added to the 'Delay Loaded Dlls' option of\r
+       // the linker properties in Visual Studio (which means this won't work with MinGW).\r
+       // For all other DLLs, use SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32).\r
+       // Finally, we need to perform the whole gymkhana below, where we can't call on\r
+       // SetDefaultDllDirectories() directly, because Windows 7 doesn't have the API exposed.\r
+       // Also, no, Coverity, we never need to care about freeing kernel32 as a library.\r
+       // coverity[leaked_storage]\r
+\r
+       pfSetDefaultDllDirectories = (SetDefaultDllDirectories_t)\r
+               GetProcAddress(LoadLibraryW(L"kernel32.dll"), "SetDefaultDllDirectories");\r
+       if (pfSetDefaultDllDirectories != NULL)\r
+               pfSetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);\r
+}\r
+\r
+\r
 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)\r
 {\r
+       int i;\r
     int rc;\r
+       int status = 0;\r
        HANDLE hMutex;\r
+       WCHAR* Pos = NULL;\r
        WCHAR CurDir[MAX_PATH];\r
+       WCHAR ExePath[MAX_PATH];\r
+       WCHAR CurDirBk[MAX_PATH];\r
+       WCHAR ExePathBk[MAX_PATH];\r
+       CHAR TmpPathA[MAX_PATH];\r
 \r
     UNREFERENCED_PARAMETER(hPrevInstance);\r
 \r
+       for (i = 0; i < __argc; i++)\r
+       {\r
+               if (__argv[i] && _stricmp(__argv[i], "/F") == 0)\r
+               {\r
+                       g_ChromeFirst = FALSE;\r
+                       break;\r
+               }\r
+       }\r
+\r
+       DllProtect();\r
+\r
     if (GetUserDefaultUILanguage() == 0x0804)\r
     {\r
         g_sysinfo.language = LANGUAGE_CN;\r
@@ -491,12 +577,67 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
        }\r
 \r
        GetCurrentDirectoryW(MAX_PATH, CurDir);\r
+       GetCurrentDirectoryW(MAX_PATH, CurDirBk);\r
+       GetModuleFileNameW(NULL, ExePath, MAX_PATH);\r
+       GetModuleFileNameW(NULL, ExePathBk, MAX_PATH);\r
+\r
+       for (Pos = NULL, i = 0; i < MAX_PATH && ExePath[i]; i++)\r
+       {\r
+               if (ExePath[i] == '\\' || ExePath[i] == '/')\r
+               {\r
+                       Pos = ExePath + i;\r
+               }\r
+       }\r
+\r
+       if (Pos)\r
+       {\r
+               *Pos = 0;\r
+               if (wcscmp(CurDir, ExePath))\r
+               {\r
+                       status |= 1;\r
+                       SetCurrentDirectoryW(ExePath);\r
+                       GetCurrentDirectoryW(MAX_PATH, CurDir);\r
+               }\r
+               else\r
+               {\r
+                       status |= 2;\r
+               }\r
+       }\r
+\r
+       Pos = wcsstr(CurDir, L"\\altexe");\r
+       if (Pos)\r
+       {\r
+               *Pos = 0;\r
+               status |= 4;\r
+               SetCurrentDirectoryW(CurDir);\r
+       }\r
+\r
+\r
        WideCharToMultiByte(CP_UTF8, 0, CurDir, -1, g_cur_dir, MAX_PATH, NULL, 0);\r
 \r
        sprintf_s(g_ventoy_dir, sizeof(g_ventoy_dir), "%s", g_cur_dir);\r
-       sprintf_s(g_log_file, sizeof(g_log_file), "%s\\%s", g_cur_dir, LOG_FILE);\r
+       sprintf_s(g_log_file, sizeof(g_log_file), "%s", LOG_FILE);\r
        ventoy_log_init();\r
 \r
+       vlog("====================== Ventoy Plugson =========================\n");\r
+\r
+       UTF8_Log("Current Directory <%s>\n", CurDirBk);\r
+       UTF8_Log("Exe file path <%s>\n", ExePathBk);\r
+       \r
+       if (status & 1)\r
+       {\r
+               UTF8_Log("Change current dir to exe <%s>\n", ExePath);\r
+       }\r
+       if (status & 2)\r
+       {\r
+               vlog("Current directory check OK.\n");\r
+       }\r
+       if (status & 4)\r
+       {\r
+               UTF8_Log("altexe detected, change current dir to <%s>\n", CurDir);\r
+       }\r
+\r
+\r
     if (!ventoy_is_file_exist("%s\\ventoy\\%s", g_ventoy_dir, PLUGSON_TXZ))\r
     {        \r
                MessageBoxW(NULL, g_msg_lang[MSGID_NO_TARXZ_TIP], g_msg_lang[MSGID_ERROR], MB_OK | MB_ICONERROR);\r
@@ -514,7 +655,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
        }\r
 \r
        vlog("===============================================\n");\r
-       vlog("===== Ventoy Plugson %s:%s =====\n", g_sysinfo.ip, g_sysinfo.port);\r
+       vlog("========= Ventoy Plugson %s:%s =========\n", g_sysinfo.ip, g_sysinfo.port);\r
        vlog("===============================================\n");\r
 \r
 \r
@@ -534,7 +675,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
     ventoy_http_init();\r
 \r
     g_hInst = hInstance;\r
-    DialogBoxA(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc);\r
+    DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DialogProc);\r
 \r
     return 0;\r
 }\r