| 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: |
| 8 | ** |
| 9 | ** Purpose: Positive test the FreeLibrary API. |
| 10 | ** |
| 11 | ** |
| 12 | **============================================================*/ |
| 13 | #include <palsuite.h> |
| 14 | |
| 15 | /*char LibraryName[] = "dlltest";*/ |
| 16 | /* SHLEXT is defined only for Unix variants */ |
| 17 | |
| 18 | #if defined(SHLEXT) |
| 19 | #define LibraryName "dlltest"SHLEXT |
| 20 | #else |
| 21 | #define LibraryName "dlltest" |
| 22 | #endif |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | BOOL PALAPI TestDll(HMODULE, int); |
| 28 | |
| 29 | int __cdecl main(int argc, char *argv[]) |
| 30 | { |
| 31 | HANDLE hLib; |
| 32 | |
| 33 | /* Initialize the PAL. */ |
| 34 | if ((PAL_Initialize(argc, argv)) != 0) |
| 35 | { |
| 36 | return (FAIL); |
| 37 | } |
| 38 | |
| 39 | /*Load library (DLL). */ |
| 40 | hLib = LoadLibrary(LibraryName); |
| 41 | |
| 42 | if(hLib == NULL) |
| 43 | { |
| 44 | Fail("ERROR:%u:Unable to load library %s\n" , |
| 45 | GetLastError(), |
| 46 | LibraryName); |
| 47 | } |
| 48 | |
| 49 | /* Test access to DLL. */ |
| 50 | if(!TestDll(hLib, PASS)) |
| 51 | { |
| 52 | Trace("ERROR: TestDll function returned FALSE " |
| 53 | "expected TRUE\n." ); |
| 54 | FreeLibrary(hLib); |
| 55 | Fail("" ); |
| 56 | } |
| 57 | |
| 58 | /* Call the FreeLibrary API. */ |
| 59 | if (!FreeLibrary(hLib)) |
| 60 | { |
| 61 | Fail("ERROR:%u: Unable to free library \"%s\"\n" , |
| 62 | GetLastError(), |
| 63 | LibraryName); |
| 64 | } |
| 65 | |
| 66 | /* Test access to the free'd DLL. */ |
| 67 | if(!TestDll(hLib, FAIL)) |
| 68 | { |
| 69 | Fail("ERROR: TestDll function returned FALSE " |
| 70 | "expected TRUE\n." ); |
| 71 | } |
| 72 | |
| 73 | PAL_Terminate(); |
| 74 | return PASS; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | BOOL PALAPI TestDll(HMODULE hLib, int testResult) |
| 80 | { |
| 81 | int RetVal; |
| 82 | #if WIN32 |
| 83 | char FunctName[] = "_DllTest@0" ; |
| 84 | #else |
| 85 | char FunctName[] = "DllTest" ; |
| 86 | #endif |
| 87 | FARPROC DllAddr; |
| 88 | |
| 89 | /* Attempt to grab the proc address of the dll function. |
| 90 | * This one should succeed.*/ |
| 91 | if(testResult == PASS) |
| 92 | { |
| 93 | DllAddr = GetProcAddress(hLib, FunctName); |
| 94 | if(DllAddr == NULL) |
| 95 | { |
| 96 | Trace("ERROR: Unable to load function \"%s\" library \"%s\"\n" , |
| 97 | FunctName, |
| 98 | LibraryName); |
| 99 | return (FALSE); |
| 100 | } |
| 101 | /* Run the function in the DLL, |
| 102 | * to ensure that the DLL was loaded properly.*/ |
| 103 | RetVal = DllAddr(); |
| 104 | if (RetVal != 1) |
| 105 | { |
| 106 | Trace("ERROR: Unable to receive correct information from DLL! " |
| 107 | ":expected \"1\", returned \"%d\"\n" , |
| 108 | RetVal); |
| 109 | return (FALSE); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Attempt to grab the proc address of the dll function. |
| 114 | * This one should fail.*/ |
| 115 | if(testResult == FAIL) |
| 116 | { |
| 117 | DllAddr = GetProcAddress(hLib, FunctName); |
| 118 | if(DllAddr != NULL) |
| 119 | { |
| 120 | Trace("ERROR: Able to load function \"%s\" from free'd" |
| 121 | " library \"%s\"\n" , |
| 122 | FunctName, |
| 123 | LibraryName); |
| 124 | return (FALSE); |
| 125 | } |
| 126 | } |
| 127 | return (TRUE); |
| 128 | } |
| 129 | |