Skip to content

Commit d2de3a0

Browse files
committed
IO (Windows): prefers native functions
1 parent c9b7c65 commit d2de3a0

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

src/common/impl/io_windows.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "common/io.h"
33
#include "common/stringUtils.h"
44
#include "common/windows/nt.h"
5+
#include "common/windows/unicode.h"
56

67
#include <windows.h>
78

@@ -314,13 +315,17 @@ bool ffPathExpandEnv(const char* in, FFstrbuf* out)
314315
}
315316
}
316317

317-
DWORD length = ExpandEnvironmentStringsA(in, NULL, 0);
318-
if (length <= 1) return false;
318+
wchar_t pathInW[MAX_PATH], pathOutW[MAX_PATH];
319+
ULONG len = (ULONG) strlen(in);
320+
if (!NT_SUCCESS(RtlUTF8ToUnicodeN(pathInW, (ULONG) sizeof(pathInW), &len, in, len)))
321+
return false;
322+
len /= sizeof(wchar_t); // convert from bytes to characters
323+
324+
size_t outLen; // in characters, including null terminator
325+
if (!NT_SUCCESS(RtlExpandEnvironmentStrings(NULL, pathInW, len, pathOutW, ARRAY_SIZE(pathOutW), &outLen)))
326+
return false;
319327

320-
ffStrbufClear(out);
321-
ffStrbufEnsureFree(out, (uint32_t)length);
322-
ExpandEnvironmentStringsA(in, out->chars, length);
323-
out->length = (uint32_t)length - 1;
328+
ffStrbufSetNWS(out, (uint32_t) outLen - 1, pathOutW);
324329
return true;
325330
}
326331

src/common/windows/nt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,3 +996,12 @@ static inline RTL_USER_PROCESS_PARAMETERS_FULL* ffGetProcessParams()
996996
{
997997
return (RTL_USER_PROCESS_PARAMETERS_FULL*) NtCurrentTeb()->ProcessEnvironmentBlock->ProcessParameters;
998998
}
999+
1000+
NTSYSAPI NTSTATUS NTAPI RtlExpandEnvironmentStrings(
1001+
_In_opt_ PVOID Environment,
1002+
_In_reads_(SourceLength) PCWSTR Source,
1003+
_In_ SIZE_T SourceLength,
1004+
_Out_writes_(DestinationLength) PWSTR Destination,
1005+
_In_ SIZE_T DestinationLength,
1006+
_Out_opt_ PSIZE_T ReturnLength
1007+
);

0 commit comments

Comments
 (0)