[IPHLPAPI_APITEST]
[reactos.git] / rostests / apitests / kernel32 / GetModuleFileName.c
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 <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #define _INC_WINDOWS
12 #define COM_NO_WINDOWS_H
13 #include <stdio.h>
14 #include <winreg.h>
15 #include <winnls.h>
16 #include <shlwapi.h>
17
18 static
19 VOID
20 StartChild(char **argv)
21 {
22 BOOL Success;
23 WCHAR Path[MAX_PATH];
24 PWSTR FileName;
25 PWSTR Slash;
26 WCHAR CommandLine[MAX_PATH];
27 STARTUPINFOW StartupInfo;
28 PROCESS_INFORMATION ProcessInfo;
29 DWORD Ret;
30 int Length;
31
32 Length = MultiByteToWideChar(CP_ACP,
33 0,
34 argv[0],
35 -1,
36 Path,
37 sizeof(Path) / sizeof(WCHAR));
38 ok(Length > 0, "Length = %d\n", Length);
39
40 FileName = wcsrchr(Path, '\\');
41 Slash = wcsrchr(Path, L'/');
42 if (Slash && (!FileName || Slash > FileName))
43 FileName = Slash;
44
45 if (FileName)
46 {
47 /* It's an absolute path. Set it as current dir and get the file name */
48 FileName++;
49 FileName[-1] = L'\0';
50
51 Success = SetCurrentDirectoryW(Path);
52 ok(Success == TRUE, "SetCurrentDirectory failed for path '%ls'\n", Path);
53
54 trace("Starting '%ls' in path '%ls'\n", FileName, Path);
55 }
56 else
57 {
58 FileName = Path;
59 trace("Starting '%ls', which is already relative\n", FileName);
60 }
61
62 swprintf(CommandLine, L"\"%ls\" GetModuleFileName relative", FileName);
63
64 RtlZeroMemory(&StartupInfo, sizeof(StartupInfo));
65 StartupInfo.cb = sizeof(StartupInfo);
66
67 Success = CreateProcessW(FileName,
68 CommandLine,
69 NULL,
70 NULL,
71 FALSE,
72 0,
73 NULL,
74 NULL,
75 &StartupInfo,
76 &ProcessInfo);
77 if (!Success)
78 {
79 skip("CreateProcess failed with %lu\n", GetLastError());
80 return;
81 }
82 CloseHandle(ProcessInfo.hThread);
83 Ret = WaitForSingleObject(ProcessInfo.hProcess, 30 * 1000);
84 ok(Ret == WAIT_OBJECT_0, "WaitForSingleObject returns %lu\n", Ret);
85 CloseHandle(ProcessInfo.hProcess);
86 }
87
88 static
89 VOID
90 TestGetModuleFileNameA(VOID)
91 {
92 CHAR Buffer[MAX_PATH];
93 DWORD Length;
94 BOOL Relative;
95
96 Length = GetModuleFileNameA(NULL, Buffer, sizeof(Buffer));
97 ok(Length != 0, "Length = %lu\n", Length);
98 ok(Length < sizeof(Buffer), "Length = %lu\n", Length);
99 ok(Buffer[Length] == 0, "Buffer not null terminated\n");
100 Relative = PathIsRelativeA(Buffer);
101 ok(Relative == FALSE, "GetModuleFileNameA returned relative path: %s\n", Buffer);
102 }
103
104 static
105 VOID
106 TestGetModuleFileNameW(VOID)
107 {
108 WCHAR Buffer[MAX_PATH];
109 DWORD Length;
110 BOOL Relative;
111
112 Length = GetModuleFileNameW(NULL, Buffer, sizeof(Buffer) / sizeof(WCHAR));
113 ok(Length != 0, "Length = %lu\n", Length);
114 ok(Length < sizeof(Buffer) / sizeof(WCHAR), "Length = %lu\n", Length);
115 ok(Buffer[Length] == 0, "Buffer not null terminated\n");
116 Relative = PathIsRelativeW(Buffer);
117 ok(Relative == FALSE, "GetModuleFileNameW returned relative path: %ls\n", Buffer);
118 }
119
120 START_TEST(GetModuleFileName)
121 {
122 int argc;
123 char **argv;
124
125 argc = winetest_get_mainargs(&argv);
126 if (argc < 3)
127 StartChild(argv);
128 else
129 {
130 TestGetModuleFileNameA();
131 TestGetModuleFileNameW();
132 }
133 }