[idd] helper: switch to fully Unicode to avoid encoding issues

This commit is contained in:
Quantum
2025-09-13 14:59:38 -04:00
committed by Geoffrey McRae
parent 48dc7a90f9
commit 9d48e70983
2 changed files with 25 additions and 43 deletions

View File

@@ -44,14 +44,14 @@
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<CharacterSet>NotSet</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries> <UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset> <PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>NotSet</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings"> <ImportGroup Label="ExtensionSettings">

View File

@@ -12,7 +12,8 @@ using namespace Microsoft::WRL::Wrappers::HandleTraits;
#include "VersionInfo.h" #include "VersionInfo.h"
#include "CPipeClient.h" #include "CPipeClient.h"
#define SVCNAME "Looking Glass (IDD Helper)" #define ARRAY_SIZE(x) (sizeof(x) / sizeof*(x))
#define SVCNAME L"Looking Glass (IDD Helper)"
static SERVICE_STATUS_HANDLE l_svcStatusHandle; static SERVICE_STATUS_HANDLE l_svcStatusHandle;
static SERVICE_STATUS l_svcStatus; static SERVICE_STATUS l_svcStatus;
@@ -23,10 +24,10 @@ static void WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv);
static void WINAPI SvcCtrlHandler(DWORD dwControl); static void WINAPI SvcCtrlHandler(DWORD dwControl);
static void ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint); static void ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint);
static std::string l_executable; static std::wstring l_executable;
static HandleT<HANDLENullTraits> l_process; static HandleT<HANDLENullTraits> l_process;
static HandleT<EventTraits> l_exitEvent; static HandleT<EventTraits> l_exitEvent;
static std::string l_exitEventName; static std::wstring l_exitEventName;
static void Launch(); static void Launch();
@@ -40,8 +41,8 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
g_debug.Init("looking-glass-iddhelper"); g_debug.Init("looking-glass-iddhelper");
DEBUG_INFO("Looking Glass IDD Helper (" LG_VERSION_STR ")"); DEBUG_INFO("Looking Glass IDD Helper (" LG_VERSION_STR ")");
char buffer[MAX_PATH]; wchar_t buffer[MAX_PATH];
DWORD result = GetModuleFileNameA(NULL, buffer, MAX_PATH); DWORD result = GetModuleFileName(NULL, buffer, MAX_PATH);
if (result == 0) if (result == 0)
{ {
DEBUG_ERROR("Failed to get the executable path"); DEBUG_ERROR("Failed to get the executable path");
@@ -51,24 +52,10 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
int argc = 0; int argc = 0;
LPWSTR * wargv = CommandLineToArgvW(GetCommandLineW(), &argc); LPWSTR * wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
std::vector<std::string> args; std::vector<std::wstring> args;
args.reserve(argc); args.reserve(argc);
for (int i = 0; i < argc; ++i) for (int i = 0; i < argc; ++i)
{ args.emplace_back(wargv[i]);
size_t len = wcslen(wargv[i]);
size_t bufSize = (len + 1) * 2;
std::vector<char> buffer(bufSize);
size_t converted = 0;
errno_t err = wcstombs_s(&converted, buffer.data(), bufSize, wargv[i], bufSize - 1);
if (err != 0)
{
DEBUG_ERROR("Conversion failed");
return EXIT_FAILURE;
}
args.emplace_back(buffer.data());
}
LocalFree(wargv); LocalFree(wargv);
if (argc == 1) if (argc == 1)
@@ -97,7 +84,7 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
wx.cbSize = sizeof(WNDCLASSEX); wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = DummyWndProc; wx.lpfnWndProc = DummyWndProc;
wx.hInstance = hInstance; wx.hInstance = hInstance;
wx.lpszClassName = "DUMMY_CLASS"; wx.lpszClassName = L"DUMMY_CLASS";
wx.hIcon = LoadIcon(NULL, IDI_APPLICATION); wx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wx.hCursor = LoadCursor(NULL, IDC_ARROW); wx.hCursor = LoadCursor(NULL, IDC_ARROW);
@@ -109,7 +96,7 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
return EXIT_FAILURE; return EXIT_FAILURE;
} }
HWND msgWnd = CreateWindowExA(0, MAKEINTATOM(aclass), NULL, HWND msgWnd = CreateWindowEx(0, MAKEINTATOM(aclass), NULL,
0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
bool running = g_pipe.Init(); bool running = g_pipe.Init();
@@ -149,7 +136,7 @@ bool HandleService()
{ {
SERVICE_TABLE_ENTRY DispatchTable[] = SERVICE_TABLE_ENTRY DispatchTable[] =
{ {
{ (char *)SVCNAME, SvcMain }, { (LPWSTR) SVCNAME, SvcMain },
{ NULL, NULL } { NULL, NULL }
}; };
@@ -207,7 +194,7 @@ static void WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv)
} }
UUID uuid; UUID uuid;
RPC_CSTR uuidStr; RPC_WSTR uuidStr;
RPC_STATUS status = UuidCreate(&uuid); RPC_STATUS status = UuidCreate(&uuid);
if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY && status != RPC_S_UUID_NO_ADDRESS) if (status != RPC_S_OK && status != RPC_S_UUID_LOCAL_ONLY && status != RPC_S_UUID_NO_ADDRESS)
{ {
@@ -218,8 +205,8 @@ static void WINAPI SvcMain(DWORD dwArgc, LPTSTR* lpszArgv)
if (UuidToString(&uuid, &uuidStr) == RPC_S_OK) if (UuidToString(&uuid, &uuidStr) == RPC_S_OK)
{ {
l_exitEventName = "Global\\"; l_exitEventName = L"Global\\";
l_exitEventName += (const char *)uuidStr; l_exitEventName += (wchar_t*) uuidStr;
RpcStringFree(&uuidStr); RpcStringFree(&uuidStr);
l_exitEvent.Attach(CreateEvent(NULL, FALSE, FALSE, l_exitEventName.c_str())); l_exitEvent.Attach(CreateEvent(NULL, FALSE, FALSE, l_exitEventName.c_str()));
@@ -321,7 +308,7 @@ static void ReportSvcStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD d
//static void //static void
static bool EnablePriv(const char * name) static bool EnablePriv(LPCWSTR name)
{ {
TOKEN_PRIVILEGES tp = { 0 }; TOKEN_PRIVILEGES tp = { 0 };
LUID luid; LUID luid;
@@ -334,7 +321,7 @@ static bool EnablePriv(const char * name)
return false; return false;
} }
if (!LookupPrivilegeValueA(NULL, name, &luid)) if (!LookupPrivilegeValue(NULL, name, &luid))
{ {
DEBUG_ERROR_HR(GetLastError(), "LookupPrivilegeValue %s", name); DEBUG_ERROR_HR(GetLastError(), "LookupPrivilegeValue %s", name);
return false; return false;
@@ -353,7 +340,7 @@ static bool EnablePriv(const char * name)
return true; return true;
} }
static void DisablePriv(const char * name) static void DisablePriv(LPCWSTR name)
{ {
TOKEN_PRIVILEGES tp = {0}; TOKEN_PRIVILEGES tp = {0};
LUID luid; LUID luid;
@@ -366,7 +353,7 @@ static void DisablePriv(const char * name)
return; return;
} }
if (!LookupPrivilegeValueA(NULL, name, &luid)) if (!LookupPrivilegeValue(NULL, name, &luid))
{ {
DEBUG_ERROR_HR(GetLastError(), "LookupPrivilegeValue %s", name); DEBUG_ERROR_HR(GetLastError(), "LookupPrivilegeValue %s", name);
return; return;
@@ -434,21 +421,16 @@ static void Launch()
si.cb = sizeof(si); si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW; si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW; si.wShowWindow = SW_SHOW;
si.lpDesktop = (LPSTR)"WinSta0\\Default"; si.lpDesktop = (LPWSTR) L"WinSta0\\Default";
char * cmdLine = NULL; wchar_t cmdBuf[128] = { 0 };
char cmdBuf[128];
if (l_exitEvent.IsValid()) if (l_exitEvent.IsValid())
{ _snwprintf_s(cmdBuf, ARRAY_SIZE(cmdBuf), L"LGIddHelper.exe %s", l_exitEventName.c_str());
snprintf(cmdBuf, sizeof(cmdBuf), "LGIddHelper.exe %s",
l_exitEventName.c_str());
cmdLine = cmdBuf;
}
if (!CreateProcessAsUserA( if (!CreateProcessAsUser(
token.Get(), token.Get(),
l_executable.c_str(), l_executable.c_str(),
cmdLine, cmdBuf,
NULL, NULL,
NULL, NULL,
FALSE, FALSE,