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: loadlibrarya.c
8**
9** Purpose: Positive test the LoadLibrary API by calling it multiple times.
10** Call LoadLibrary to map a module into the calling
11** process address space(DLL file)
12**
13**
14**============================================================*/
15#include <palsuite.h>
16
17/* SHLEXT is defined only for Unix variants */
18
19#if defined(SHLEXT)
20#define ModuleName "librotor_pal"SHLEXT
21#else
22#define ModuleName "rotor_pal.dll"
23#endif
24
25int __cdecl main(int argc, char *argv[])
26{
27 HMODULE ModuleHandle;
28 HMODULE ReturnHandle;
29 int err;
30
31 /* Initialize the PAL environment */
32 err = PAL_Initialize(argc, argv);
33 if(0 != err)
34 {
35 ExitProcess(FAIL);
36 }
37
38 /* load a module */
39 ModuleHandle = LoadLibrary(ModuleName);
40 if(!ModuleHandle)
41 {
42 Fail("Error[%u]:Failed to call LoadLibrary API!\n", GetLastError());
43 }
44
45 /* Call LoadLibrary again, should return same handle as returned for first time */
46 ReturnHandle = LoadLibrary(ModuleName);
47 if(!ReturnHandle)
48 {
49 Fail("Error[%u]:Failed to call LoadLibrary API second time!\n", GetLastError());
50 }
51
52 if(ModuleHandle != ReturnHandle)
53 {
54 Fail("Error[%u]:Failed to return the same handle while calling LoadLibrary API twice!\n", GetLastError());
55 }
56
57 Trace("Value of handle ModuleHandle[%x], ReturnHandle[%x]\n", ModuleHandle, ReturnHandle);
58 /* decrement the reference count of the loaded dll */
59 err = FreeLibrary(ModuleHandle);
60
61 if(0 == err)
62 {
63 Fail("Error[%u]:Failed to FreeLibrary API!\n", GetLastError());
64 }
65
66 /* Try Freeing a library again, should not fail */
67 err = FreeLibrary(ReturnHandle);
68
69 if(0 == err)
70 {
71 Fail("Error[%u][%d]: Was not successful in freeing a Library twice using FreeLibrary!\n", GetLastError(), err);
72 }
73
74 /* Try Freeing a library again, should fail */
75 err = FreeLibrary(ReturnHandle);
76
77 if(1 != err)
78 {
79 Fail("Error[%u][%d]: Was successful in freeing a Library thrice using FreeLibrary!\n", GetLastError(), err);
80 }
81 PAL_Terminate();
82 return PASS;
83}
84