ReactOS  0.4.13-dev-464-g6b95727
GetModuleFileName.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Test for GetModuleFileName
5  * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include "precomp.h"
9 
10 #include <shlwapi.h>
11 
12 static
13 VOID
15 {
16  BOOL Success;
19  PWSTR Slash;
20  WCHAR CommandLine[MAX_PATH];
21  STARTUPINFOW StartupInfo;
22  PROCESS_INFORMATION ProcessInfo;
23  DWORD Ret;
24  int Length;
25 
27  0,
28  argv[0],
29  -1,
30  Path,
31  sizeof(Path) / sizeof(WCHAR));
32  ok(Length > 0, "Length = %d\n", Length);
33 
34  FileName = wcsrchr(Path, '\\');
35  Slash = wcsrchr(Path, L'/');
36  if (Slash && (!FileName || Slash > FileName))
37  FileName = Slash;
38 
39  if (FileName)
40  {
41  /* It's an absolute path. Set it as current dir and get the file name */
42  FileName++;
43  FileName[-1] = L'\0';
44 
46  ok(Success == TRUE, "SetCurrentDirectory failed for path '%ls'\n", Path);
47 
48  trace("Starting '%ls' in path '%ls'\n", FileName, Path);
49  }
50  else
51  {
52  FileName = Path;
53  trace("Starting '%ls', which is already relative\n", FileName);
54  }
55 
56  swprintf(CommandLine, L"\"%ls\" GetModuleFileName relative", FileName);
57 
58  RtlZeroMemory(&StartupInfo, sizeof(StartupInfo));
59  StartupInfo.cb = sizeof(StartupInfo);
60 
62  CommandLine,
63  NULL,
64  NULL,
65  FALSE,
66  0,
67  NULL,
68  NULL,
69  &StartupInfo,
70  &ProcessInfo);
71  if (!Success)
72  {
73  skip("CreateProcess failed with %lu\n", GetLastError());
74  return;
75  }
76  CloseHandle(ProcessInfo.hThread);
77  Ret = WaitForSingleObject(ProcessInfo.hProcess, 30 * 1000);
78  ok(Ret == WAIT_OBJECT_0, "WaitForSingleObject returns %lu\n", Ret);
79  CloseHandle(ProcessInfo.hProcess);
80 }
81 
82 static
83 VOID
85 {
87  DWORD Length;
88  BOOL Relative;
89 
91  ok(Length != 0, "Length = %lu\n", Length);
92  ok(Length < sizeof(Buffer), "Length = %lu\n", Length);
93  ok(Buffer[Length] == 0, "Buffer not null terminated\n");
94  Relative = PathIsRelativeA(Buffer);
95  ok(Relative == FALSE, "GetModuleFileNameA returned relative path: %s\n", Buffer);
96 }
97 
98 static
99 VOID
101 {
103  DWORD Length;
104  BOOL Relative;
105 
106  Length = GetModuleFileNameW(NULL, Buffer, sizeof(Buffer) / sizeof(WCHAR));
107  ok(Length != 0, "Length = %lu\n", Length);
108  ok(Length < sizeof(Buffer) / sizeof(WCHAR), "Length = %lu\n", Length);
109  ok(Buffer[Length] == 0, "Buffer not null terminated\n");
110  Relative = PathIsRelativeW(Buffer);
111  ok(Relative == FALSE, "GetModuleFileNameW returned relative path: %ls\n", Buffer);
112 }
113 
115 {
116  int argc;
117  char **argv;
118 
120  if (argc < 3)
121  StartChild(argv);
122  else
123  {
126  }
127 }
START_TEST(GetModuleFileName)
DWORD WINAPI GetModuleFileNameW(HINSTANCE hModule, LPWSTR lpFilename, DWORD nSize)
Definition: loader.c:607
static VOID StartChild(char **argv)
static int argc
Definition: ServiceArgs.c:12
#define trace(...)
Definition: kmt_test.h:217
#define TRUE
Definition: types.h:120
BOOL WINAPI PathIsRelativeW(LPCWSTR lpszPath)
Definition: path.c:1558
#define CloseHandle
Definition: compat.h:398
static VOID TestGetModuleFileNameA(VOID)
uint16_t * PWSTR
Definition: typedefs.h:54
#define CP_ACP
Definition: compat.h:99
char CHAR
Definition: xmlstorage.h:175
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define argv
Definition: mplay32.c:18
DWORD WINAPI GetModuleFileNameA(HINSTANCE hModule, LPSTR lpFilename, DWORD nSize)
Definition: loader.c:546
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ok(value,...)
smooth NULL
Definition: ftsmooth.c:416
_Check_return_ _CRTIMP _CONST_RETURN wchar_t *__cdecl wcsrchr(_In_z_ const wchar_t *_Str, _In_ wchar_t _Ch)
Definition: bufpool.h:45
BOOL WINAPI DECLSPEC_HOTPATCH CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory, LPSTARTUPINFOW lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation)
Definition: proc.c:4623
#define WAIT_OBJECT_0
Definition: winbase.h:387
DWORD cb
Definition: winbase.h:817
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define MAX_PATH
Definition: compat.h:26
#define swprintf(buf, format,...)
Definition: sprintf.c:56
unsigned long DWORD
Definition: ntddk_ex.h:95
#define GetModuleFileName
Definition: winbase.h:3645
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
int winetest_get_mainargs(char ***pargv)
static VOID TestGetModuleFileNameW(VOID)
static const WCHAR L[]
Definition: oid.c:1250
BOOL WINAPI PathIsRelativeA(LPCSTR lpszPath)
Definition: path.c:1542
struct _FileName FileName
Definition: fatprocs.h:884
PRTL_UNICODE_STRING_BUFFER Path
BOOL WINAPI SetCurrentDirectoryW(IN LPCWSTR lpPathName)
Definition: path.c:2248
#define MultiByteToWideChar
Definition: compat.h:100
#define skip(...)
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261