1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================================
6**
7** Source: test1.c (filemapping_memmgt\getprocaddress\test1)
8**
9** Purpose: Positive test the GetProcAddress API.
10** The first test calls GetProcAddress to retrieve the
11** address of SimpleFunction inside testlib by its name,
12** then calls the function and checks that it worked.
13**
14**
15**===========================================================================*/
16#include <palsuite.h>
17
18typedef int (PALAPI *SIMPLEFUNCTION)(int);
19
20/* SHLEXT is defined only for Unix variants */
21#if defined(SHLEXT)
22#define lpModuleName "testlib"SHLEXT
23#else
24#define lpModuleName "testlib.dll"
25#endif
26
27int __cdecl main(int argc, char *argv[])
28{
29 int err;
30 HMODULE hModule;
31 SIMPLEFUNCTION procAddressByName;
32
33#if WIN32
34 const char *FunctionName = "_SimpleFunction@4";
35#else
36 const char *FunctionName = "SimpleFunction";
37#endif
38
39 /* Initialize the PAL environment. */
40 if(0 != PAL_Initialize(argc, argv))
41 {
42 return FAIL;
43 }
44
45
46 /* load a module */
47 hModule = LoadLibrary(lpModuleName);
48 if(!hModule)
49 {
50 Fail("Unexpected error: "
51 "LoadLibrary(%s) failed.\n",
52 lpModuleName);
53 }
54
55 /*
56 * Test 1
57 *
58 * Get the address of a function
59 */
60 procAddressByName = (SIMPLEFUNCTION) GetProcAddress(hModule,FunctionName);
61 if(!procAddressByName)
62 {
63 Trace("ERROR: Unable to get address of SimpleFunction by its name. "
64 "GetProcAddress returned NULL with error %d\n",
65 GetLastError());
66
67 /* Cleanup */
68 err = FreeLibrary(hModule);
69 if(0 == err)
70 {
71 Fail("Unexpected error: Failed to FreeLibrary %s\n",
72 lpModuleName);
73 }
74 Fail("");
75 }
76
77 /* Call the function to see that it really worked */
78 /* Simple function adds 1 to the argument passed */
79 if( 2 != ((procAddressByName)(1)))
80 {
81 Trace("ERROR: Problem calling the function by its address.\n");
82
83 /* Cleanup */
84 err = FreeLibrary(hModule);
85 if(0 == err)
86 {
87 Fail("Unexpected error: Failed to FreeLibrary %s\n",
88 lpModuleName);
89 }
90 Fail("");
91 }
92
93 /* Cleanup */
94 err = FreeLibrary(hModule);
95 if(0 == err)
96 {
97 Fail("Unexpected error: Failed to FreeLibrary %s\n",
98 lpModuleName);
99 }
100
101 PAL_Terminate();
102 return PASS;
103}
104
105
106
107
108
109
110
111
112