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