diff --git a/Sources/FoundationEssentials/Platform.swift b/Sources/FoundationEssentials/Platform.swift index 1e39051b8..7e9800ab7 100644 --- a/Sources/FoundationEssentials/Platform.swift +++ b/Sources/FoundationEssentials/Platform.swift @@ -355,17 +355,14 @@ extension Platform { return try? FileManager.default.destinationOfSymbolicLink( atPath: "/proc/self/exe").standardizingPath #elseif os(Windows) - let hFile = GetModuleHandleW(nil) - let dwLength: DWORD = GetFinalPathNameByHandleW(hFile, nil, 0, FILE_NAME_NORMALIZED) - guard dwLength > 0 else { return nil } - return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) { lpBuffer in - guard GetFinalPathNameByHandleW(hFile, lpBuffer.baseAddress, dwLength, FILE_NAME_NORMALIZED) == dwLength - 1 else { + return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(MAX_PATH)) { lpBuffer in + let actualLength = GetModuleFileNameW(nil, lpBuffer.baseAddress!, DWORD(lpBuffer.count)) + // Windows Documentation: + // If the function fails, the return value is 0 (zero). + // To get extended error information, call GetLastError. + guard actualLength > 0 else { return nil } - - // The `GetFinalPathNameByHandleW` function will normalise the path - // for us as part of the query. This allows us to avoid having to - // standardize the path ourselves. return String(decodingCString: lpBuffer.baseAddress!, as: UTF16.self) } #else diff --git a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift index 1caa04bfb..acb94408e 100644 --- a/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift +++ b/Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift @@ -597,10 +597,7 @@ extension _ProcessInfo { } private static func _getProcessName() -> String { - guard let processPath = CommandLine.arguments.first else { - return "" - } - return processPath.lastPathComponent + return Platform.getFullExecutablePath()?.lastPathComponent ?? "" } #if os(macOS)