| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/file.h> |
| 5 |
#include <winpr/path.h> |
| 6 |
#include <winpr/tchar.h> |
| 7 |
#include <winpr/windows.h> |
| 8 |
#include <winpr/library.h> |
| 9 |
#include <winpr/nt.h> |
| 10 |
|
| 11 |
int TestLibraryLoadLibrary(int argc, char* argv[]) |
| 12 |
{ |
| 13 |
HINSTANCE library = nullptr; |
| 14 |
LPCSTR SharedLibraryExtension = nullptr; |
| 15 |
CHAR LibraryPath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 16 |
PCHAR p = nullptr; |
| 17 |
WINPR_UNUSED(argc); |
| 18 |
WINPR_UNUSED(argv); |
| 19 |
if (!GetModuleFileNameA(nullptr, LibraryPath, ARRAYSIZE(LibraryPath))) |
| 20 |
{ |
| 21 |
const UINT32 err = GetLastError(); |
| 22 |
const HRESULT herr = HRESULT_FROM_WIN32(err); |
| 23 |
printf("%s: GetModuleFilenameA failed: %s - %s [0x%08" PRIX32 "]\n", __func__, |
| 24 |
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err); |
| 25 |
return -1; |
| 26 |
} |
| 27 |
|
| 28 |
/* PathCchRemoveFileSpec is not implemented in WinPR */ |
| 29 |
|
| 30 |
if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE)))) |
| 31 |
{ |
| 32 |
printf("%s: Error identifying module directory path\n", __func__); |
| 33 |
return -1; |
| 34 |
} |
| 35 |
*p = 0; |
| 36 |
|
| 37 |
if (FAILED(NativePathCchAppendA(LibraryPath, ARRAYSIZE(LibraryPath), "TestLibraryA"))) |
| 38 |
return -1; |
| 39 |
SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT); |
| 40 |
if (FAILED(NativePathCchAddExtensionA(LibraryPath, ARRAYSIZE(LibraryPath), |
| 41 |
SharedLibraryExtension))) |
| 42 |
return -1; |
| 43 |
|
| 44 |
printf("%s: Loading Library: '%s'\n", __func__, LibraryPath); |
| 45 |
|
| 46 |
if (!(library = LoadLibraryA(LibraryPath))) |
| 47 |
{ |
| 48 |
const UINT32 err = GetLastError(); |
| 49 |
const HRESULT herr = HRESULT_FROM_WIN32(err); |
| 50 |
printf("%s: LoadLibraryA failure: %s - %s [0x%08" PRIX32 "]\n", __func__, |
| 51 |
NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err); |
| 52 |
return -1; |
| 53 |
} |
| 54 |
|
| 55 |
if (!FreeLibrary(library)) |
| 56 |
{ |
| 57 |
const UINT32 err = GetLastError(); |
| 58 |
const HRESULT herr = HRESULT_FROM_WIN32(err); |
| 59 |
printf("%s: FreeLibrary failure: %s - %s [0x%08" PRIX32 "]\n", __func__, NtStatus2Tag(herr), |
| 60 |
Win32ErrorCode2Tag(err), err); |
| 61 |
return -1; |
| 62 |
} |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |