Skip to content

Commit

Permalink
Replace ExpandEnvironmentStrings double calling with wil helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
skyline75489 committed Oct 15, 2019
1 parent b9233c0 commit 9808131
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/host/ft_host/API_FontTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ void FontTests::TestFontScenario()

void FontTests::TestLongFontNameScenario()
{
wistd::unique_ptr<wchar_t[]> expandedLongFontPath;
VERIFY_SUCCEEDED(ExpandPathToMutable(pwszLongFontPath, expandedLongFontPath));
if (!CheckIfFileExists(expandedLongFontPath.get()))
std::wstring expandedLongFontPath = wil::ExpandEnvironmentStringsW<std::wstring>(pwszLongFontPath);

if (!CheckIfFileExists(expandedLongFontPath.c_str()))
{
Log::Comment(L"Lucida Sans Typewriter doesn't exist; skipping long font test.");
Log::Result(WEX::Logging::TestResults::Result::Skipped);
Expand Down
10 changes: 4 additions & 6 deletions src/host/ft_host/CanaryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ static PCWSTR pwszConhostV1Path = L"%WINDIR%\\system32\\conhostv1.dll";
void CanaryTests::LaunchV1Console()
{
// First ensure that this system has the v1 console to test.
wistd::unique_ptr<wchar_t[]> ConhostV1Path;
VERIFY_SUCCEEDED(ExpandPathToMutable(pwszConhostV1Path, ConhostV1Path));
std::wstring ConhostV1Path = wil::ExpandEnvironmentStringsW<std::wstring>(pwszConhostV1Path);

if (!CheckIfFileExists(ConhostV1Path.get()))
if (!CheckIfFileExists(ConhostV1Path.c_str()))
{
Log::Comment(L"This system does not have the legacy conhostv1.dll module. Skipping test.");
Log::Result(WEX::Logging::TestResults::Skipped);
Expand All @@ -39,8 +38,7 @@ void CanaryTests::LaunchV1Console()

// Attempt to launch CMD.exe in a new window
// Expand any environment variables present in the command line string.
wistd::unique_ptr<wchar_t[]> CmdLineMutable;
VERIFY_SUCCEEDED(ExpandPathToMutable(pwszCmdPath, CmdLineMutable));
std::wstring CmdLine = wil::ExpandEnvironmentStringsW<std::wstring>(pwszCmdPath);

// Create output handle for redirection. We'll read from it to make sure CMD started correctly.
// We'll let it have a default input handle to make sure it binds to the new console host window that will be created.
Expand All @@ -67,7 +65,7 @@ void CanaryTests::LaunchV1Console()

wil::unique_process_information ProcessInformation;
VERIFY_WIN32_BOOL_SUCCEEDED(CreateProcessW(NULL,
CmdLineMutable.get(),
const_cast<LPWSTR>(CmdLine.c_str()),
NULL,
NULL,
TRUE,
Expand Down
19 changes: 0 additions & 19 deletions src/host/ft_host/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,6 @@ bool CheckLastError(HANDLE handle, PCWSTR pwszFunc)
}
}

HRESULT ExpandPathToMutable(_In_ PCWSTR pwszPath, _Out_ wistd::unique_ptr<wchar_t[]>& MutablePath) noexcept
{
// Find how many characters we need.
const DWORD cchExpanded = ExpandEnvironmentStringsW(pwszPath, nullptr, 0);
RETURN_LAST_ERROR_IF(0 == cchExpanded);

// Allocate space to hold result
wistd::unique_ptr<wchar_t[]> NewMutable = wil::make_unique_nothrow<wchar_t[]>(cchExpanded);
RETURN_IF_NULL_ALLOC(NewMutable);

// Expand string into allocated space
RETURN_LAST_ERROR_IF(0 == ExpandEnvironmentStringsW(pwszPath, NewMutable.get(), cchExpanded));

// On success, give our string back out (swapping with what was given and we'll free it for the caller.)
MutablePath.swap(NewMutable);

return S_OK;
}

bool CheckIfFileExists(_In_ PCWSTR pwszPath) noexcept
{
wil::unique_hfile hFile(CreateFileW(pwszPath,
Expand Down
2 changes: 0 additions & 2 deletions src/host/ft_host/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ bool CheckLastError(HANDLE handle, PCWSTR pwszFunc);

[[nodiscard]] bool CheckIfFileExists(_In_ PCWSTR pwszPath) noexcept;

[[nodiscard]] HRESULT ExpandPathToMutable(_In_ PCWSTR pwszPath, _Out_ wistd::unique_ptr<wchar_t[]>& MutablePath) noexcept;

//http://blogs.msdn.com/b/oldnewthing/archive/2013/10/17/10457292.aspx
BOOL UnadjustWindowRectEx(
LPRECT prc,
Expand Down
14 changes: 2 additions & 12 deletions src/server/Entrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,11 @@
}

// Expand any environment variables present in the command line string.
// - Get needed size
DWORD cchCmdLineExpanded = ExpandEnvironmentStringsW(pwszCmdLine, nullptr, 0);
RETURN_LAST_ERROR_IF(0 == cchCmdLineExpanded);

// - Allocate space to hold result
wistd::unique_ptr<wchar_t[]> CmdLineMutable = wil::make_unique_nothrow<wchar_t[]>(cchCmdLineExpanded);
RETURN_IF_NULL_ALLOC(CmdLineMutable);

// - Expand string into allocated space
RETURN_LAST_ERROR_IF(0 == ExpandEnvironmentStringsW(pwszCmdLine, CmdLineMutable.get(), cchCmdLineExpanded));

std::wstring cmdLine = wil::ExpandEnvironmentStringsW<std::wstring>(pwszCmdLine);
// Call create process
wil::unique_process_information ProcessInformation;
RETURN_IF_WIN32_BOOL_FALSE(CreateProcessW(NULL,
CmdLineMutable.get(),
const_cast<LPWSTR>(cmdLine.c_str()),
NULL,
NULL,
TRUE,
Expand Down

0 comments on commit 9808131

Please sign in to comment.