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