* Addendum to r58214.
[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 <thfabba@gmx.de>
6 */
7
8 #define WIN32_NO_STATUS
9 #define _INC_WINDOWS
10 #define COM_NO_WINDOWS_H
11 #define UNICODE
12 #include <stdio.h>
13 #include <wine/test.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 STARTUPINFO 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
39 FileName = wcsrchr(Path, '\\');
40 Slash = wcsrchr(Path, L'/');
41 if (Slash && (!FileName || Slash > FileName))
42 FileName = Slash;
43
44 if (FileName)
45 {
46 /* It's an absolute path. Set it as current dir and get the file name */
47 FileName++;
48 FileName[-1] = L'\0';
49
50 Success = SetCurrentDirectory(Path);
51 ok(Success == TRUE, "SetCurrentDirectory failed for path '%ls'\n", Path);
52
53 trace("Starting '%ls' in path '%ls'\n", FileName, Path);
54 }
55 else
56 {
57 FileName = Path;
58 trace("Starting '%ls', which is already relative\n", FileName);
59 }
60
61 swprintf(CommandLine, L"\"%ls\" GetModuleFileName relative", FileName);
62
63 RtlZeroMemory(&StartupInfo, sizeof(StartupInfo));
64 StartupInfo.cb = sizeof(StartupInfo);
65
66 Success = CreateProcess(FileName,
67 CommandLine,
68 NULL,
69 NULL,
70 FALSE,
71 0,
72 NULL,
73 NULL,
74 &StartupInfo,
75 &ProcessInfo);
76 if (!Success)
77 {
78 skip("CreateProcess failed with %lu\n", GetLastError());
79 return;
80 }
81 CloseHandle(ProcessInfo.hThread);
82 Ret = WaitForSingleObject(ProcessInfo.hProcess, 30 * 1000);
83 ok(Ret == WAIT_OBJECT_0, "WaitForSingleObject returns %lu\n", Ret);
84 CloseHandle(ProcessInfo.hProcess);
85 }
86
87 static
88 VOID
89 TestGetModuleFileNameA(VOID)
90 {
91 CHAR Buffer[MAX_PATH];
92 DWORD Length;
93 BOOL Relative;
94
95 Length = GetModuleFileNameA(NULL, Buffer, sizeof(Buffer));
96 ok(Length != 0, "Length = %lu\n", Length);
97 ok(Length < sizeof(Buffer), "Length = %lu\n", Length);
98 ok(Buffer[Length] == 0, "Buffer not null terminated\n");
99 Relative = PathIsRelativeA(Buffer);
100 ok(Relative == FALSE, "GetModuleFileNameA returned relative path: %s\n", Buffer);
101 }
102
103 static
104 VOID
105 TestGetModuleFileNameW(VOID)
106 {
107 WCHAR Buffer[MAX_PATH];
108 DWORD Length;
109 BOOL Relative;
110
111 Length = GetModuleFileNameW(NULL, Buffer, sizeof(Buffer) / sizeof(WCHAR));
112 ok(Length != 0, "Length = %lu\n", Length);
113 ok(Length < sizeof(Buffer) / sizeof(WCHAR), "Length = %lu\n", Length);
114 ok(Buffer[Length] == 0, "Buffer not null terminated\n");
115 Relative = PathIsRelativeW(Buffer);
116 ok(Relative == FALSE, "GetModuleFileNameA returned relative path: %ls\n", Buffer);
117 }
118
119 START_TEST(GetModuleFileName)
120 {
121 int argc;
122 char **argv;
123
124 argc = winetest_get_mainargs(&argv);
125 if (argc < 3)
126 StartChild(argv);
127 else
128 {
129 TestGetModuleFileNameA();
130 TestGetModuleFileNameW();
131 }
132 }