X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/Ventoy.git/blobdiff_plain/4bf43ab9d4e6b724836724c1d514760d164df79a..c7dcaa57342a3c5b556d8572dfdba720bb15eba3:/Plugson/src/main_windows.c diff --git a/Plugson/src/main_windows.c b/Plugson/src/main_windows.c index 910dfe5..6f74d2b 100644 --- a/Plugson/src/main_windows.c +++ b/Plugson/src/main_windows.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -10,6 +10,8 @@ #include #include +char g_ventoy_dir[MAX_PATH]; + static BOOL g_running = FALSE; static HWND g_refresh_button; static HWND g_start_button; @@ -34,6 +36,7 @@ typedef enum MSGID MSGID_BTN_STOP_TIP, MSGID_BTN_EXIT_TIP, MSGID_RUNNING_TIP, + MSGID_NO_TARXZ_TIP, MSGID_BUTT }MSGID; @@ -41,21 +44,22 @@ typedef enum MSGID const WCHAR *g_msg_cn[MSGID_BUTT] = { - L"´íÎó", - L"ÌáÐÑ", - L"ÇëÔÚ Ventoy Å̸ùĿ¼ÏÂÔËÐб¾³ÌÐò£¡£¨´æ·ÅISOÎļþµÄλÖã©", - L"´´½¨ ventoy Ŀ¼ʧ°Ü£¬ÎÞ·¨¼ÌÐø£¡", - L"ventoy Ŀ¼´æÔÚ£¬µ«ÊÇ´óСд²»Æ¥Å䣬ÇëÏȽ«ÆäÖØÃüÃû£¡", - L"ÄÚ²¿´íÎ󣬳ÌÐò¼´½«Í˳ö£¡", - L"Ë¢ÐÂ", - L"Æô¶¯", - L"Í£Ö¹", - L"Á´½Ó", - L"Í˳ö", - - L"Í£Ö¹ÔËÐкóä¯ÀÀÆ÷Ò³Ãæ½«»á¹Ø±Õ£¬ÊÇ·ñ¼ÌÐø£¿", - L"µ±Ç°·þÎñÕýÔÚÔËÐУ¬ÊÇ·ñÍ˳ö£¿", - L"ÇëÏȹرÕÕýÔÚÔËÐÐµÄ VentoyPlugson ³ÌÐò£¡", + L"错误", + L"提醒", + L"请在 Ventoy 盘根目录下运行本程序!(存放ISO文件的位置)", + L"创建 ventoy 目录失败,无法继续!", + L"ventoy 目录存在,但是大小写不匹配,请先将其重命名!", + L"内部错误,程序即将退出!", + L"刷新", + L"启动", + L"停止", + L"链接", + L"退出", + + L"停止运行后浏览器页面将会关闭,是否继续?", + L"当前服务正在运行,是否退出?", + L"请先关闭正在运行的 VentoyPlugson 程序!", + L"ventoy\\plugson.tar.xz 文件不存在,请在正确的目录下运行!", }; const WCHAR *g_msg_en[MSGID_BUTT] = { @@ -74,6 +78,7 @@ const WCHAR *g_msg_en[MSGID_BUTT] = L"The browser page will close after stop, continue?", L"Service is running, continue?", L"Please close another running VentoyPlugson instance!", + L"ventoy\\plugson.tar.xz does not exist, please run under the correct directory!", }; const WCHAR **g_msg_lang = NULL; @@ -88,7 +93,7 @@ void ventoy_log_exit(void); static BOOL OnDestroyDialog() { - ventoy_http_exit(); + ventoy_http_exit(); ventoy_disk_exit(); #ifndef VENTOY_SIM ventoy_www_exit(); @@ -459,13 +464,71 @@ static int ParseCmdLine(LPSTR lpCmdLine, char *ip, char *port) return 0; } + + +// +//copy from Rufus +//Copyright © 2011-2021 Pete Batard +// +#include +// For delay-loaded DLLs, use LOAD_LIBRARY_SEARCH_SYSTEM32 to avoid DLL search order hijacking. +FARPROC WINAPI dllDelayLoadHook(unsigned dliNotify, PDelayLoadInfo pdli) +{ + if (dliNotify == dliNotePreLoadLibrary) { + // Windows 7 without KB2533623 does not support the LOAD_LIBRARY_SEARCH_SYSTEM32 flag. + // That is is OK, because the delay load handler will interrupt the NULL return value + // to mean that it should perform a normal LoadLibrary. + return (FARPROC)LoadLibraryExA(pdli->szDll, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32); + } + return NULL; +} + +#if defined(_MSC_VER) +// By default the Windows SDK headers have a `const` while MinGW does not. +const +#endif +PfnDliHook __pfnDliNotifyHook2 = dllDelayLoadHook; + +typedef BOOL(WINAPI* SetDefaultDllDirectories_t)(DWORD); +static void DllProtect(void) +{ + SetDefaultDllDirectories_t pfSetDefaultDllDirectories = NULL; + + // Disable loading system DLLs from the current directory (sideloading mitigation) + // PS: You know that official MSDN documentation for SetDllDirectory() that explicitly + // indicates that "If the parameter is an empty string (""), the call removes the current + // directory from the default DLL search order"? Yeah, that doesn't work. At all. + // Still, we invoke it, for platforms where the following call might actually work... + SetDllDirectoryA(""); + + // For libraries on the KnownDLLs list, the system will always load them from System32. + // For other DLLs we link directly to, we can delay load the DLL and use a delay load + // hook to load them from System32. Note that, for this to work, something like: + // 'somelib.dll;%(DelayLoadDLLs)' must be added to the 'Delay Loaded Dlls' option of + // the linker properties in Visual Studio (which means this won't work with MinGW). + // For all other DLLs, use SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32). + // Finally, we need to perform the whole gymkhana below, where we can't call on + // SetDefaultDllDirectories() directly, because Windows 7 doesn't have the API exposed. + // Also, no, Coverity, we never need to care about freeing kernel32 as a library. + // coverity[leaked_storage] + + pfSetDefaultDllDirectories = (SetDefaultDllDirectories_t) + GetProcAddress(LoadLibraryW(L"kernel32.dll"), "SetDefaultDllDirectories"); + if (pfSetDefaultDllDirectories != NULL) + pfSetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32); +} + + int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow) { int rc; HANDLE hMutex; + WCHAR CurDir[MAX_PATH]; UNREFERENCED_PARAMETER(hPrevInstance); + DllProtect(); + if (GetUserDefaultUILanguage() == 0x0804) { g_sysinfo.language = LANGUAGE_CN; @@ -484,10 +547,18 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi return 1; } - GetCurrentDirectoryA(MAX_PATH, g_cur_dir); + GetCurrentDirectoryW(MAX_PATH, CurDir); + WideCharToMultiByte(CP_UTF8, 0, CurDir, -1, g_cur_dir, MAX_PATH, NULL, 0); + + sprintf_s(g_ventoy_dir, sizeof(g_ventoy_dir), "%s", g_cur_dir); sprintf_s(g_log_file, sizeof(g_log_file), "%s\\%s", g_cur_dir, LOG_FILE); ventoy_log_init(); + if (!ventoy_is_file_exist("%s\\ventoy\\%s", g_ventoy_dir, PLUGSON_TXZ)) + { + MessageBoxW(NULL, g_msg_lang[MSGID_NO_TARXZ_TIP], g_msg_lang[MSGID_ERROR], MB_OK | MB_ICONERROR); + return 1; + } ParseCmdLine(lpCmdLine, g_sysinfo.ip, g_sysinfo.port); if (g_sysinfo.ip[0] == 0)