From 1d87fdc803b80b2ab6d94c28556115559cca3dcb Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Thu, 7 Feb 2019 17:41:01 +0100 Subject: [PATCH] Implemented GetModuleFileNameW --- winpr/libwinpr/library/library.c | 43 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/winpr/libwinpr/library/library.c b/winpr/libwinpr/library/library.c index 326737c..9daa099 100644 --- a/winpr/libwinpr/library/library.c +++ b/winpr/libwinpr/library/library.c @@ -232,10 +232,45 @@ HMODULE GetModuleHandleW(LPCWSTR lpModuleName) DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize) { - /* TODO: Implement */ - WLog_ERR(TAG, "%s is not implemented", __FUNCTION__); - SetLastError(ERROR_CALL_NOT_IMPLEMENTED); - return 0; + WCHAR* wname = NULL; + char* name = NULL; + int csize; + DWORD status; + { + csize = ConvertFromUnicode(CP_UTF8, 0, lpFilename, -1, &name, 0, NULL, NULL); + + if (csize < 0) + { + SetLastError(ERROR_INTERNAL_ERROR); + return 0; + } + } + memset(lpFilename, 0, nSize * sizeof(WCHAR)); + status = GetModuleFileNameA(hModule, name, (DWORD)csize); + + if (status > INT_MAX) + { + SetLastError(ERROR_INTERNAL_ERROR); + status = 0; + } + + if (status > 0) + { + int rc = ConvertToUnicode(CP_UTF8, 0, name, (int)status, &wname, 0); + + if (rc < 0) + { + free(name); + SetLastError(ERROR_INTERNAL_ERROR); + return 0; + } + + memcpy(lpFilename, wname, (size_t)rc * sizeof(WCHAR)); + } + + free(name); + free(wname); + return status; } DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize) -- 2.7.4