Minor code fixup:

* Explicitly call the Unicode versions of various APIs

 * Add const in a couple of places

 * Improve a few assert error messages

 * Either initialize BackgroundDesktop fully or leave all its fields NULL
This commit is contained in:
Ryan Prichard 2015-12-20 23:56:24 -06:00
parent 83b6e9281d
commit 12935a722b
4 changed files with 57 additions and 57 deletions

View File

@ -325,14 +325,14 @@ int Agent::handleStartProcessPacket(ReadBuffer &packet)
LPCWSTR cwdArg = cwd.empty() ? NULL : cwd.c_str(); LPCWSTR cwdArg = cwd.empty() ? NULL : cwd.c_str();
LPCWSTR envArg = env.empty() ? NULL : env.data(); LPCWSTR envArg = env.empty() ? NULL : env.data();
STARTUPINFO sui; STARTUPINFOW sui;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
memset(&sui, 0, sizeof(sui)); memset(&sui, 0, sizeof(sui));
memset(&pi, 0, sizeof(pi)); memset(&pi, 0, sizeof(pi));
sui.cb = sizeof(STARTUPINFO); sui.cb = sizeof(sui);
sui.lpDesktop = desktop.empty() ? NULL : (LPWSTR)desktop.c_str(); sui.lpDesktop = desktop.empty() ? NULL : (LPWSTR)desktop.c_str();
success = CreateProcess(programArg, cmdlineArg, NULL, NULL, success = CreateProcessW(programArg, cmdlineArg, NULL, NULL,
/*bInheritHandles=*/FALSE, /*bInheritHandles=*/FALSE,
/*dwCreationFlags=*/CREATE_UNICODE_ENVIRONMENT | /*dwCreationFlags=*/CREATE_UNICODE_ENVIRONMENT |
/*CREATE_NEW_PROCESS_GROUP*/0, /*CREATE_NEW_PROCESS_GROUP*/0,
@ -378,8 +378,8 @@ void Agent::updateMouseInputFlags(bool forceTrace)
{ {
DWORD mode = 0; DWORD mode = 0;
GetConsoleMode(m_console->conin(), &mode); GetConsoleMode(m_console->conin(), &mode);
bool newFlagMI = mode & ENABLE_MOUSE_INPUT; const bool newFlagMI = mode & ENABLE_MOUSE_INPUT;
bool newFlagQE = mode & ENABLE_QUICK_EDIT_MODE; const bool newFlagQE = mode & ENABLE_QUICK_EDIT_MODE;
if (forceTrace || if (forceTrace ||
newFlagMI != m_consoleMouseInputEnabled || newFlagMI != m_consoleMouseInputEnabled ||
newFlagQE != m_consoleQuickEditEnabled) { newFlagQE != m_consoleQuickEditEnabled) {

View File

@ -63,7 +63,7 @@ NamedPipe::IoWorker::IoWorker(NamedPipe *namedPipe) :
m_pending(false), m_pending(false),
m_currentIoSize(-1) m_currentIoSize(-1)
{ {
m_event = CreateEvent(NULL, TRUE, FALSE, NULL); m_event = CreateEventW(NULL, TRUE, FALSE, NULL);
ASSERT(m_event != NULL); ASSERT(m_event != NULL);
} }
@ -171,7 +171,7 @@ int NamedPipe::OutputWorker::getPendingIoSize()
bool NamedPipe::connectToServer(LPCWSTR pipeName) bool NamedPipe::connectToServer(LPCWSTR pipeName)
{ {
ASSERT(isClosed()); ASSERT(isClosed());
HANDLE handle = CreateFile(pipeName, HANDLE handle = CreateFileW(pipeName,
GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE,
0, 0,
NULL, NULL,

View File

@ -58,12 +58,12 @@ winpty_s::winpty_s() : controlPipe(NULL), dataPipe(NULL)
static HMODULE getCurrentModule() static HMODULE getCurrentModule()
{ {
HMODULE module; HMODULE module;
if (!GetModuleHandleEx( if (!GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCTSTR)getCurrentModule, reinterpret_cast<LPCWSTR>(getCurrentModule),
&module)) { &module)) {
assert(false); assert(false && "GetModuleHandleEx failed");
} }
return module; return module;
} }
@ -72,7 +72,7 @@ static std::wstring getModuleFileName(HMODULE module)
{ {
const int bufsize = 4096; const int bufsize = 4096;
wchar_t path[bufsize]; wchar_t path[bufsize];
int size = GetModuleFileName(module, path, bufsize); int size = GetModuleFileNameW(module, path, bufsize);
assert(size != 0 && size != bufsize); assert(size != 0 && size != bufsize);
return std::wstring(path); return std::wstring(path);
} }
@ -107,7 +107,7 @@ static bool connectNamedPipe(HANDLE handle, bool overlapped)
if (overlapped) { if (overlapped) {
pover = &over; pover = &over;
memset(&over, 0, sizeof(over)); memset(&over, 0, sizeof(over));
over.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); over.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
assert(over.hEvent != NULL); assert(over.hEvent != NULL);
} }
bool success = ConnectNamedPipe(handle, pover); bool success = ConnectNamedPipe(handle, pover);
@ -144,7 +144,7 @@ static int32_t readInt32(winpty_t *pc)
static HANDLE createNamedPipe(const std::wstring &name, bool overlapped) static HANDLE createNamedPipe(const std::wstring &name, bool overlapped)
{ {
return CreateNamedPipe(name.c_str(), return CreateNamedPipeW(name.c_str(),
/*dwOpenMode=*/ /*dwOpenMode=*/
PIPE_ACCESS_DUPLEX | PIPE_ACCESS_DUPLEX |
FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_FIRST_PIPE_INSTANCE |
@ -174,15 +174,15 @@ static std::wstring getObjectName(HANDLE object)
{ {
BOOL success; BOOL success;
DWORD lengthNeeded = 0; DWORD lengthNeeded = 0;
GetUserObjectInformation(object, UOI_NAME, GetUserObjectInformationW(object, UOI_NAME,
NULL, 0, NULL, 0,
&lengthNeeded); &lengthNeeded);
assert(lengthNeeded % sizeof(wchar_t) == 0); assert(lengthNeeded % sizeof(wchar_t) == 0);
wchar_t *tmp = new wchar_t[lengthNeeded / 2]; wchar_t *tmp = new wchar_t[lengthNeeded / 2];
success = GetUserObjectInformation(object, UOI_NAME, success = GetUserObjectInformationW(object, UOI_NAME,
tmp, lengthNeeded, tmp, lengthNeeded,
NULL); NULL);
assert(success); assert(success && "GetUserObjectInformationW failed");
std::wstring ret = tmp; std::wstring ret = tmp;
delete [] tmp; delete [] tmp;
return ret; return ret;
@ -201,21 +201,21 @@ static bool shouldShowConsoleWindow()
static BackgroundDesktop setupBackgroundDesktop() static BackgroundDesktop setupBackgroundDesktop()
{ {
BackgroundDesktop ret; BackgroundDesktop ret;
ret.originalStation = GetProcessWindowStation();
if (!shouldShowConsoleWindow()) { if (!shouldShowConsoleWindow()) {
ret.station = CreateWindowStation(NULL, 0, WINSTA_ALL_ACCESS, NULL); const HWINSTA originalStation = GetProcessWindowStation();
ret.station = CreateWindowStationW(NULL, 0, WINSTA_ALL_ACCESS, NULL);
if (ret.station != NULL) { if (ret.station != NULL) {
ret.originalStation = originalStation;
bool success = SetProcessWindowStation(ret.station); bool success = SetProcessWindowStation(ret.station);
assert(success); assert(success && "SetProcessWindowStation failed");
ret.desktop = CreateDesktop(L"Default", NULL, NULL, 0, GENERIC_ALL, NULL); ret.desktop = CreateDesktopW(L"Default", NULL, NULL, 0, GENERIC_ALL, NULL);
assert(ret.originalStation != NULL); assert(ret.originalStation != NULL);
assert(ret.station != NULL); assert(ret.station != NULL);
assert(ret.desktop != NULL); assert(ret.desktop != NULL);
ret.desktopName = ret.desktopName =
getObjectName(ret.station) + L"\\" + getObjectName(ret.desktop); getObjectName(ret.station) + L"\\" + getObjectName(ret.desktop);
} else { } else {
trace("CreateWindowStation failed"); trace("CreateWindowStationW failed");
} }
} }
return ret; return ret;
@ -236,8 +236,8 @@ static std::wstring getDesktopFullName()
// to be passed to CloseDesktop. // to be passed to CloseDesktop.
HWINSTA station = GetProcessWindowStation(); HWINSTA station = GetProcessWindowStation();
HDESK desktop = GetThreadDesktop(GetCurrentThreadId()); HDESK desktop = GetThreadDesktop(GetCurrentThreadId());
assert(station != NULL); assert(station != NULL && "GetProcessWindowStation returned NULL");
assert(desktop != NULL); assert(desktop != NULL && "GetThreadDesktop returned NULL");
return getObjectName(station) + L"\\" + getObjectName(desktop); return getObjectName(station) + L"\\" + getObjectName(desktop);
} }
@ -256,7 +256,7 @@ static void startAgentProcess(const BackgroundDesktop &desktop,
std::wstring agentCmdLine = agentCmdLineStream.str(); std::wstring agentCmdLine = agentCmdLineStream.str();
// Start the agent. // Start the agent.
STARTUPINFO sui; STARTUPINFOW sui;
memset(&sui, 0, sizeof(sui)); memset(&sui, 0, sizeof(sui));
sui.cb = sizeof(sui); sui.cb = sizeof(sui);
if (desktop.station != NULL) { if (desktop.station != NULL) {
@ -267,7 +267,7 @@ static void startAgentProcess(const BackgroundDesktop &desktop,
std::vector<wchar_t> cmdline(agentCmdLine.size() + 1); std::vector<wchar_t> cmdline(agentCmdLine.size() + 1);
agentCmdLine.copy(&cmdline[0], agentCmdLine.size()); agentCmdLine.copy(&cmdline[0], agentCmdLine.size());
cmdline[agentCmdLine.size()] = L'\0'; cmdline[agentCmdLine.size()] = L'\0';
success = CreateProcess(agentProgram.c_str(), success = CreateProcessW(agentProgram.c_str(),
&cmdline[0], &cmdline[0],
NULL, NULL, NULL, NULL,
/*bInheritHandles=*/FALSE, /*bInheritHandles=*/FALSE,

View File

@ -32,7 +32,7 @@
// Create a manual reset, initially unset event. // Create a manual reset, initially unset event.
static HANDLE createEvent() { static HANDLE createEvent() {
return CreateEvent(NULL, TRUE, FALSE, NULL); return CreateEventW(NULL, TRUE, FALSE, NULL);
} }
static std::vector<unsigned char> filterContent( static std::vector<unsigned char> filterContent(