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 = NULL;
18 TEST_AB_FN pFunctionA = NULL;
19 TEST_AB_FN pFunctionB = NULL;
20 LPCSTR SharedLibraryExtension = NULL;
21 CHAR LibraryPath[PATHCCH_MAX_CCH] = { 0 };
22 PCHAR p = NULL;
23 WINPR_UNUSED(argc);
24 WINPR_UNUSED(argv);
25 if (!GetModuleFileNameA(NULL, 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 NativePathCchAppendA(LibraryPath, PATHCCH_MAX_CCH, "TestLibraryA");
44 SharedLibraryExtension = PathGetSharedLibraryExtensionA(PATH_SHARED_LIB_EXT_WITH_DOT);
45 NativePathCchAddExtensionA(LibraryPath, PATHCCH_MAX_CCH, SharedLibraryExtension);
46 printf("%s: Loading Library: '%s'\n", __func__, LibraryPath);
47
48 if (!(library = LoadLibraryA(LibraryPath)))
49 {
50 const UINT32 err = GetLastError();
51 const HRESULT herr = HRESULT_FROM_WIN32(err);
52 printf("%s: LoadLibraryA failure: %s - %s [0x%08" PRIX32 "]\n", __func__,
53 NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
54 return -1;
55 }
56
57 if (!(pFunctionA = GetProcAddressAs(library, "FunctionA", TEST_AB_FN)))
58 {
59 const UINT32 err = GetLastError();
60 const HRESULT herr = HRESULT_FROM_WIN32(err);
61 printf("%s: GetProcAddress failure (FunctionA) %s - %s [0x%08" PRIX32 "]\n", __func__,
62 NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
63 return -1;
64 }
65
66 if (!(pFunctionB = GetProcAddressAs(library, "FunctionB", TEST_AB_FN)))
67 {
68 const UINT32 err = GetLastError();
69 const HRESULT herr = HRESULT_FROM_WIN32(err);
70 printf("%s: GetProcAddress failure (FunctionB) %s - %s [0x%08" PRIX32 "]\n", __func__,
71 NtStatus2Tag(herr), Win32ErrorCode2Tag(err), err);
72 return -1;
73 }
74
75 a = 2;
76 b = 3;
77 c = pFunctionA(a, b); /* LibraryA / FunctionA multiplies a and b */
78
79 if (c != (a * b))
80 {
81 printf("%s: pFunctionA call failed\n", __func__);
82 return -1;
83 }
84
85 a = 10;
86 b = 5;
87 c = pFunctionB(a, b); /* LibraryA / FunctionB divides a by b */
88
89 if (c != (a / b))
90 {
91 printf("%s: pFunctionB call failed\n", __func__);
92 return -1;
93 }
94
95 if (!FreeLibrary(library))
96 {
97 const UINT32 err = GetLastError();
98 const HRESULT herr = HRESULT_FROM_WIN32(err);
99 printf("%s: FreeLibrary failure: %s - %s [0x%08" PRIX32 "]\n", __func__, NtStatus2Tag(herr),
100 Win32ErrorCode2Tag(err), err);
101 return -1;
102 }
103
104 return 0;
105}