FreeRDP
Loading...
Searching...
No Matches
TestLibraryGetProcAddress.c
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
11typedef int (*TEST_AB_FN)(int a, int b);
12
13int TestLibraryGetProcAddress(int argc, char* argv[])
14{
15 int a = 0;
16 int b = 0;
17 int c = 0;
18 HINSTANCE library = nullptr;
19 TEST_AB_FN pFunctionA = nullptr;
20 TEST_AB_FN pFunctionB = nullptr;
21 LPCSTR SharedLibraryExtension = nullptr;
22 CHAR LibraryPath[MAX_PATH] = WINPR_C_ARRAY_INIT;
23 PCHAR p = nullptr;
24 WINPR_UNUSED(argc);
25 WINPR_UNUSED(argv);
26 if (!GetModuleFileNameA(nullptr, LibraryPath, ARRAYSIZE(LibraryPath)))
27 {
28 const UINT32 err = GetLastError();
29 const HRESULT herr = HRESULT_FROM_WIN32(err);
30 printf("%s: GetModuleFilenameA failed: %s - %s [0x%08" PRIX32 "]\n", __func__,
31 NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
32 return -1;
33 }
34
35 /* PathCchRemoveFileSpec is not implemented in WinPR */
36
37 if (!(p = strrchr(LibraryPath, PathGetSeparatorA(PATH_STYLE_NATIVE))))
38 {
39 printf("%s: Error identifying module directory path\n", __func__);
40 return -1;
41 }
42
43 *p = 0;
44 if (FAILED(NativePathCchAppendA(LibraryPath, ARRAYSIZE(LibraryPath), "TestLibraryA")))
45 return -1;
46 SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
47 if (FAILED(NativePathCchAddExtensionA(LibraryPath, ARRAYSIZE(LibraryPath),
48 SharedLibraryExtension)))
49 return -1;
50 printf("%s: Loading Library: '%s'\n", __func__, LibraryPath);
51
52 if (!(library = LoadLibraryA(LibraryPath)))
53 {
54 const UINT32 err = GetLastError();
55 const HRESULT herr = HRESULT_FROM_WIN32(err);
56 printf("%s: LoadLibraryA failure: %s - %s [0x%08" PRIX32 "]\n", __func__,
57 NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
58 return -1;
59 }
60
61 if (!(pFunctionA = GetProcAddressAs(library, "FunctionA", TEST_AB_FN)))
62 {
63 const UINT32 err = GetLastError();
64 const HRESULT herr = HRESULT_FROM_WIN32(err);
65 printf("%s: GetProcAddress failure (FunctionA) %s - %s [0x%08" PRIX32 "]\n", __func__,
66 NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
67 return -1;
68 }
69
70 if (!(pFunctionB = GetProcAddressAs(library, "FunctionB", TEST_AB_FN)))
71 {
72 const UINT32 err = GetLastError();
73 const HRESULT herr = HRESULT_FROM_WIN32(err);
74 printf("%s: GetProcAddress failure (FunctionB) %s - %s [0x%08" PRIX32 "]\n", __func__,
75 NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
76 return -1;
77 }
78
79 a = 2;
80 b = 3;
81 c = pFunctionA(a, b); /* LibraryA / FunctionA multiplies a and b */
82
83 if (c != (a * b))
84 {
85 printf("%s: pFunctionA call failed\n", __func__);
86 return -1;
87 }
88
89 a = 10;
90 b = 5;
91 c = pFunctionB(a, b); /* LibraryA / FunctionB divides a by b */
92
93 if (c != (a / b))
94 {
95 printf("%s: pFunctionB call failed\n", __func__);
96 return -1;
97 }
98
99 if (!FreeLibrary(library))
100 {
101 const UINT32 err = GetLastError();
102 const HRESULT herr = HRESULT_FROM_WIN32(err);
103 printf("%s: FreeLibrary failure: %s - %s [0x%08" PRIX32 "]\n", __func__, NtStatus2Tag(herr),
104 Win32ErrorCode2Tag(err), err);
105 return -1;
106 }
107
108 return 0;
109}