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: getmodulefilenamea.c
8**
9** Purpose: Positive test the GetModuleFileName API.
10** Call GetModuleFileName to retrieve the specified
11** module full path and file name
12**
13**
14**============================================================*/
15
16#include <palsuite.h>
17
18#define MODULENAMEBUFFERSIZE 1024
19
20/* SHLEXT is defined only for Unix variants */
21
22#if defined(SHLEXT)
23#define ModuleName "librotor_pal"SHLEXT
24#define Delimiter "/"
25#else
26#define ModuleName "rotor_pal.dll"
27#define Delimiter "\\"
28#endif
29
30int __cdecl main(int argc, char *argv[])
31{
32 HMODULE ModuleHandle;
33 int err;
34 DWORD ModuleNameLength;
35 char ModuleFileNameBuf[MODULENAMEBUFFERSIZE]="";
36 char* TempBuf = NULL;
37 char* LastBuf = NULL;
38
39 //Initialize the PAL environment
40 err = PAL_Initialize(argc, argv);
41 if(0 != err)
42 {
43 ExitProcess(FAIL);
44 }
45
46
47 //load a module
48 ModuleHandle = LoadLibrary(ModuleName);
49 if(!ModuleHandle)
50 {
51 Fail("Failed to call LoadLibrary API!\n");
52 }
53
54
55 //retrieve the specified module full path and file name
56 ModuleNameLength = GetModuleFileName(
57 ModuleHandle,//specified module handle
58 ModuleFileNameBuf,//buffer for module file name
59 MODULENAMEBUFFERSIZE);
60
61 //strip out all full path
62 TempBuf = strtok(ModuleFileNameBuf,Delimiter);
63 LastBuf = TempBuf;
64 while(NULL != TempBuf)
65 {
66 LastBuf = TempBuf;
67 TempBuf = strtok(NULL,Delimiter);
68 }
69
70
71 if(0 == ModuleNameLength || strcmp(ModuleName,LastBuf))
72 {
73 Trace("\nFailed to all GetModuleFileName API!\n");
74 err = FreeLibrary(ModuleHandle);
75 if(0 == err)
76 {
77 Fail("\nFailed to all FreeLibrary API!\n");
78 }
79 Fail("");
80 }
81
82 //decrement the reference count of the loaded dll
83 err = FreeLibrary(ModuleHandle);
84 if(0 == err)
85 {
86 Fail("\nFailed to all FreeLibrary API!\n");
87 }
88
89 PAL_Terminate();
90 return PASS;
91}
92
93