20 #include <winpr/config.h>
22 #include <winpr/crt.h>
23 #include <winpr/platform.h>
25 #include <winpr/library.h>
28 #define TAG WINPR_TAG("library")
65 #if !defined(_WIN32) || defined(_UWP)
73 #include <sys/types.h>
77 #include <mach-o/dyld.h>
80 #if defined(__FreeBSD__)
81 #include <sys/sysctl.h>
86 DLL_DIRECTORY_COOKIE AddDllDirectory(PCWSTR NewDirectory)
89 WLog_ERR(TAG,
"not implemented");
90 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
94 BOOL RemoveDllDirectory(DLL_DIRECTORY_COOKIE Cookie)
97 WLog_ERR(TAG,
"not implemented");
98 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
102 BOOL SetDefaultDllDirectories(DWORD DirectoryFlags)
105 WLog_ERR(TAG,
"not implemented");
106 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
110 HMODULE LoadLibraryA(LPCSTR lpLibFileName)
114 HMODULE hModule = NULL;
115 WCHAR* filenameW = NULL;
120 filenameW = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
124 hModule = LoadLibraryW(filenameW);
128 HMODULE library = NULL;
129 library = dlopen(lpLibFileName, RTLD_LOCAL | RTLD_LAZY);
133 const char* err = dlerror();
134 WLog_ERR(TAG,
"failed with %s", err);
142 HMODULE LoadLibraryW(LPCWSTR lpLibFileName)
147 return LoadPackagedLibrary(lpLibFileName, 0);
149 HMODULE module = NULL;
150 char* name = ConvertWCharToUtf8Alloc(lpLibFileName, NULL);
154 module = LoadLibraryA(name);
160 HMODULE LoadLibraryExA(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
163 WLog_WARN(TAG,
"does not support dwFlags 0x%08" PRIx32, dwFlags);
166 WLog_WARN(TAG,
"does not support hFile != NULL");
168 return LoadLibraryA(lpLibFileName);
171 HMODULE LoadLibraryExW(LPCWSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
174 WLog_WARN(TAG,
"does not support dwFlags 0x%08" PRIx32, dwFlags);
177 WLog_WARN(TAG,
"does not support hFile != NULL");
179 return LoadLibraryW(lpLibFileName);
184 #if !defined(_WIN32) && !defined(__CYGWIN__)
186 FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
189 proc = dlsym(hModule, lpProcName);
193 WLog_ERR(TAG,
"GetProcAddress: could not find procedure %s: %s", lpProcName, dlerror());
194 return (FARPROC)NULL;
200 BOOL FreeLibrary(HMODULE hLibModule)
203 status = dlclose(hLibModule);
211 HMODULE GetModuleHandleA(LPCSTR lpModuleName)
214 WLog_ERR(TAG,
"not implemented");
215 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
219 HMODULE GetModuleHandleW(LPCWSTR lpModuleName)
222 WLog_ERR(TAG,
"not implemented");
223 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
235 DWORD GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize)
240 SetLastError(ERROR_INTERNAL_ERROR);
244 char* name = calloc(nSize,
sizeof(
char));
247 SetLastError(ERROR_INTERNAL_ERROR);
250 status = GetModuleFileNameA(hModule, name, nSize);
252 if ((status > INT_MAX) || (nSize > INT_MAX))
254 SetLastError(ERROR_INTERNAL_ERROR);
260 if (ConvertUtf8NToWChar(name, status, lpFilename, nSize) < 0)
263 SetLastError(ERROR_INTERNAL_ERROR);
272 #if defined(__linux__) || defined(__NetBSD__) || defined(__DragonFly__)
273 static DWORD module_from_proc(
const char* proc, LPSTR lpFilename, DWORD nSize)
275 char buffer[8192] = { 0 };
276 ssize_t status = readlink(proc, buffer, ARRAYSIZE(buffer) - 1);
278 if ((status < 0) || ((
size_t)status >= ARRAYSIZE(buffer)))
280 SetLastError(ERROR_INTERNAL_ERROR);
284 const size_t length = strnlen(buffer, ARRAYSIZE(buffer));
288 CopyMemory(lpFilename, buffer, length);
289 lpFilename[length] =
'\0';
290 return (DWORD)length;
293 CopyMemory(lpFilename, buffer, nSize - 1);
294 lpFilename[nSize - 1] =
'\0';
295 SetLastError(ERROR_INSUFFICIENT_BUFFER);
300 DWORD GetModuleFileNameA(HMODULE hModule, LPSTR lpFilename, DWORD nSize)
302 #if defined(__linux__)
304 return module_from_proc(
"/proc/self/exe", lpFilename, nSize);
305 #elif defined(__FreeBSD__)
306 int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
309 const int rc = sysctl(mib, ARRAYSIZE(mib), lpFilename, &cb, NULL, 0);
310 if ((rc != 0) || (cb > nSize))
312 SetLastError(ERROR_INTERNAL_ERROR);
317 #elif defined(__NetBSD__)
319 return module_from_proc(
"/proc/curproc/exe", lpFilename, nSize);
320 #elif defined(__DragonFly__)
322 return module_from_proc(
"/proc/curproc/file", lpFilename, nSize);
323 #elif defined(__MACOSX__)
331 uint32_t size =
sizeof(path);
332 status = _NSGetExecutablePath(path, &size);
337 SetLastError(ERROR_INTERNAL_ERROR);
345 realpath(path, buffer);
346 length = strnlen(buffer,
sizeof(buffer));
350 CopyMemory(lpFilename, buffer, length);
351 lpFilename[length] =
'\0';
352 return (DWORD)length;
355 CopyMemory(lpFilename, buffer, nSize - 1);
356 lpFilename[nSize - 1] =
'\0';
357 SetLastError(ERROR_INSUFFICIENT_BUFFER);
362 WLog_ERR(TAG,
"is not implemented");
363 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
369 HMODULE LoadLibraryX(LPCSTR lpLibFileName)
376 WCHAR* wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
379 hm = LoadLibraryW(wstr);
383 return LoadLibraryA(lpLibFileName);
387 HMODULE LoadLibraryExX(LPCSTR lpLibFileName, HANDLE hFile, DWORD dwFlags)
393 WCHAR* wstr = ConvertUtf8ToWCharAlloc(lpLibFileName, NULL);
395 hm = LoadLibraryExW(wstr, hFile, dwFlags);
399 return LoadLibraryExA(lpLibFileName, hFile, dwFlags);