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