1 /*
2  * PROJECT:         ReactOS API Tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for CRT command-line handling - Utility GUI program.
5  * PROGRAMMER:      Hermès BÉLUSCA - MAÏTO <hermes.belusca@sfr.fr>
6  */
7 
8 #define WIN32_NO_STATUS
9 #include <stdio.h>
10 #include <windef.h>
11 #include <winbase.h>
12 #include <ndk/rtlfuncs.h>
13 
14 #include "CmdLineUtil.h"
15 
16 int APIENTRY wWinMain(HINSTANCE hInstance,
17                       HINSTANCE hPrevInstance,
18                       LPWSTR    lpCmdLine,
19                       int       nCmdShow)
20 {
21     /*
22      * Get the unparsed command line as seen in Win32 mode,
23      * and the NT-native mode one.
24      */
25     LPWSTR CmdLine = GetCommandLineW();
26     UNICODE_STRING CmdLine_U = NtCurrentPeb()->ProcessParameters->CommandLine;
27 
28     /* Write the results into a file. */
29     HANDLE hFile = CreateFileW(DATAFILE,
30                                GENERIC_WRITE,
31                                0, NULL,
32                                CREATE_ALWAYS,
33                                FILE_ATTRIBUTE_NORMAL,
34                                NULL);
35     if (hFile != INVALID_HANDLE_VALUE)
36     {
37         DWORD dwSize, dwStringSize;
38 
39         /*
40          * Format of the data file :
41          *
42          * [size_of_string 4 bytes][null_terminated_C_string]
43          * [size_of_string 4 bytes][null_terminated_C_string]
44          * [UNICODE_STRING_structure][string_buffer_of_UNICODE_STRING]
45          */
46 
47         /* 1- Write the WinMain's command line. */
48         dwStringSize = (lstrlenW(lpCmdLine) + 1) * sizeof(WCHAR);
49 
50         WriteFile(hFile,
51                   &dwStringSize,
52                   sizeof(dwStringSize),
53                   &dwSize,
54                   NULL);
55 
56         WriteFile(hFile,
57                   lpCmdLine,
58                   dwStringSize,
59                   &dwSize,
60                   NULL);
61 
62         /* 2- Write the Win32 mode command line. */
63         dwStringSize = (lstrlenW(CmdLine) + 1) * sizeof(WCHAR);
64 
65         WriteFile(hFile,
66                   &dwStringSize,
67                   sizeof(dwStringSize),
68                   &dwSize,
69                   NULL);
70 
71         WriteFile(hFile,
72                   CmdLine,
73                   dwStringSize,
74                   &dwSize,
75                   NULL);
76 
77         /* 3- Finally, write the UNICODE_STRING command line. */
78         WriteFile(hFile,
79                   &CmdLine_U,
80                   sizeof(CmdLine_U),
81                   &dwSize,
82                   NULL);
83 
84         WriteFile(hFile,
85                   CmdLine_U.Buffer,
86                   CmdLine_U.Length,
87                   &dwSize,
88                   NULL);
89 
90         /* Now close the file. */
91         CloseHandle(hFile);
92     }
93 
94     return 0;
95 }
96 
97 /* EOF */
98