ReactOS 0.4.15-dev-7998-gdb93cb1
winstation.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Tests some aspects of the Window Station API.
5 * COPYRIGHT: Copyright 2018 Hermes Belusca-Maito
6 */
7
8#include <windef.h>
9#include <winbase.h>
10#include <wincon.h>
11#include <winuser.h>
12
13#include <strsafe.h>
14
15#include "resource.h"
16
17
18/*
19 * Logging facilities
20 */
21
22typedef struct _LOG_FILE
23{
28
29BOOL
31 OUT PLOG_FILE LogFile,
34 IN size_t cbBufferSize)
35{
37
38 ZeroMemory(LogFile, sizeof(*LogFile));
39
43 NULL,
46 NULL);
48 return FALSE;
49
50 LogFile->hLogFile = hLogFile;
51 LogFile->pBuffer = pBuffer;
52 LogFile->cbBufferSize = cbBufferSize;
53
54 return TRUE;
55}
56
57VOID
59 IN PLOG_FILE LogFile)
60{
61 CloseHandle(LogFile->hLogFile);
62 ZeroMemory(LogFile, sizeof(*LogFile));
63}
64
65BOOL
67 IN PLOG_FILE LogFile,
69 IN DWORD dwBufferSize)
70{
71 return WriteFile(LogFile->hLogFile,
72 Buffer,
73 dwBufferSize,
74 &dwBufferSize, NULL);
75}
76
77BOOL
79 IN PLOG_FILE LogFile,
81{
82 return WriteToLog(LogFile,
83 String,
84 wcslen(String) * sizeof(WCHAR));
85}
86
87BOOL
89 IN PLOG_FILE LogFile,
92{
93 StringCbVPrintfW(LogFile->pBuffer,
94 LogFile->cbBufferSize,
95 Format, args);
96
97 return WriteToLog(LogFile,
98 LogFile->pBuffer,
99 wcslen(LogFile->pBuffer) * sizeof(WCHAR));
100}
101
102BOOL
104 IN PLOG_FILE LogFile,
106 ...)
107{
108 BOOL bRet;
110
112 bRet = WriteToLogPrintfV(LogFile, Format, args);
113 va_end(args);
114
115 return bRet;
116}
117
118
119/*
120 * Window Station tests
121 */
122
123BOOL
126 IN LPWSTR lpszDesktop,
128{
129 PLOG_FILE LogFile = (PLOG_FILE)lParam;
130
131 WriteToLogPrintf(LogFile, L" :: Found desktop '%s'\r\n", lpszDesktop);
132
133 /* Continue the enumeration */
134 return TRUE;
135}
136
137/*
138 * This test inspects the same window station aspects that are used in the
139 * Cygwin fhandler_console.cc!fhandler_console::create_invisible_console()
140 * function, see:
141 * https://github.com/cygwin/cygwin/blob/7b9bfb4136f23655e243bab89fb62b04bdbacc7f/winsup/cygwin/fhandler_console.cc#L2494
142 */
144{
145 HWINSTA hWinSta;
146 LPCWSTR lpszWinSta = L"Test-WinSta";
147 BOOL bIsItOk;
148 LOG_FILE LogFile;
149 WCHAR szBuffer[2048];
150
151 bIsItOk = InitLog(&LogFile, L"test_winsta.log", szBuffer, sizeof(szBuffer));
152 if (!bIsItOk)
153 {
154 MessageBoxW(hWnd, L"Could not create the log file, stopping test now...", L"Error", MB_ICONERROR | MB_OK);
155 return;
156 }
157
158 /* Switch output to UTF-16 (little endian) */
159 WriteToLog(&LogFile, "\xFF\xFE", 2);
160
161 WriteToLogPrintf(&LogFile, L"Creating Window Station '%s'\r\n", lpszWinSta);
162 hWinSta = CreateWindowStationW(lpszWinSta, 0, WINSTA_ALL_ACCESS, NULL);
163 WriteToLogPrintf(&LogFile, L"--> Returned handle 0x%p ; last error: %lu\r\n", hWinSta, GetLastError());
164
165 if (!hWinSta)
166 {
167 WriteToLogPuts(&LogFile, L"\r\nHandle is NULL, cannot proceed further, stopping the test!\r\n\r\n");
168 return;
169 }
170
171 WriteToLogPrintf(&LogFile, L"Enumerate desktops on Window Station '%s' (0x%p) (before process attach)\r\n", lpszWinSta, hWinSta);
172 bIsItOk = EnumDesktopsW(hWinSta, EnumDesktopProc, (LPARAM)&LogFile);
173 WriteToLogPrintf(&LogFile, L"--> Returned %s ; last error: %lu\r\n",
174 (bIsItOk ? L"success" : L"failure"), GetLastError());
175
176 WriteToLogPrintf(&LogFile, L"Setting current process to Window Station '%s' (0x%p)\r\n", lpszWinSta, hWinSta);
177 bIsItOk = SetProcessWindowStation(hWinSta);
178 WriteToLogPrintf(&LogFile, L"--> Returned %s ; last error: %lu\r\n",
179 (bIsItOk ? L"success" : L"failure"), GetLastError());
180
181 WriteToLogPrintf(&LogFile, L"Enumerate desktops on Window Station '%s' (0x%p) (after process attach, before allocating console)\r\n", lpszWinSta, hWinSta);
182 bIsItOk = EnumDesktopsW(hWinSta, EnumDesktopProc, (LPARAM)&LogFile);
183 WriteToLogPrintf(&LogFile, L"--> Returned %s ; last error: %lu\r\n",
184 (bIsItOk ? L"success" : L"failure"), GetLastError());
185
186 WriteToLogPrintf(&LogFile, L"Allocating a new console on Window Station '%s' (0x%p)\r\n", lpszWinSta, hWinSta);
187 bIsItOk = AllocConsole();
188 WriteToLogPrintf(&LogFile, L"--> Returned %s ; last error: %lu\r\n",
189 (bIsItOk ? L"success" : L"failure"), GetLastError());
190
191 WriteToLogPrintf(&LogFile, L"Enumerate desktops on Window Station '%s' (0x%p) (after allocating console)\r\n", lpszWinSta, hWinSta);
192 bIsItOk = EnumDesktopsW(hWinSta, EnumDesktopProc, (LPARAM)&LogFile);
193 WriteToLogPrintf(&LogFile, L"--> Returned %s ; last error: %lu\r\n",
194 (bIsItOk ? L"success" : L"failure"), GetLastError());
195
196 WriteToLogPrintf(&LogFile, L"Now closing Window Station '%s' (0x%p)\r\n", lpszWinSta, hWinSta);
197 bIsItOk = CloseWindowStation(hWinSta);
198 WriteToLogPrintf(&LogFile, L"--> Returned %s ; last error: %lu\r\n\r\n",
199 (bIsItOk ? L"success" : L"failure"), GetLastError());
200
201 CloseLog(&LogFile);
202}
203
205{
207 switch (message)
208 {
209 case WM_INITDIALOG:
210 return (INT_PTR)TRUE;
211
212 case WM_COMMAND:
213 switch (LOWORD(wParam))
214 {
215 case IDOK:
216 DoTest(hDlg);
217 EndDialog(hDlg, LOWORD(wParam));
218 break;
219
220 case IDCANCEL:
221 default:
222 EndDialog(hDlg, LOWORD(wParam));
223 break;
224 }
225 return (INT_PTR)TRUE;
226 }
227 return (INT_PTR)FALSE;
228}
229
231 HINSTANCE hPrevInstance,
232 LPWSTR lpCmdLine,
233 int nCmdShow)
234{
235 UNREFERENCED_PARAMETER(hPrevInstance);
236 UNREFERENCED_PARAMETER(lpCmdLine);
237
239}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define IDD_ABOUTBOX
Definition: resource.h:8
HWND hWnd
Definition: settings.c:17
static HANDLE hLogFile
Definition: log.c:16
BOOL WINAPI AllocConsole(VOID)
Definition: console.c:74
HINSTANCE hInstance
Definition: charmap.c:19
Definition: bufpool.h:45
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define APIENTRY
Definition: api.h:79
#define CloseHandle
Definition: compat.h:739
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define CALLBACK
Definition: compat.h:35
#define FILE_SHARE_READ
Definition: compat.h:136
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
void InitLog(void)
Definition: log.c:20
#define OPEN_ALWAYS
Definition: disk.h:70
unsigned int UINT
Definition: ndis.h:50
#define GENERIC_WRITE
Definition: nt_native.h:90
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
_Must_inspect_result_ _In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PUNICODE_STRING LogFileName
Definition: nttmapi.h:324
#define L(x)
Definition: ntvdm.h:50
#define LOWORD(l)
Definition: pedump.c:82
PVOID pBuffer
#define args
Definition: format.c:66
STRSAFEAPI StringCbVPrintfW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszFormat, va_list argList)
Definition: strsafe.h:507
PWCHAR pBuffer
Definition: winstation.c:25
HANDLE hLogFile
Definition: winstation.c:24
size_t cbBufferSize
Definition: winstation.c:26
Definition: match.c:390
Definition: tftpd.h:60
int32_t INT_PTR
Definition: typedefs.h:64
#define IN
Definition: typedefs.h:39
uint16_t * PWCHAR
Definition: typedefs.h:56
#define OUT
Definition: typedefs.h:40
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
Definition: wdfdevice.h:2433
BOOL WriteToLogPrintfV(IN PLOG_FILE LogFile, IN LPCWSTR Format, IN va_list args)
Definition: winstation.c:88
VOID DoTest(HWND hWnd)
Definition: winstation.c:143
BOOL CALLBACK EnumDesktopProc(IN LPWSTR lpszDesktop, IN LPARAM lParam)
Definition: winstation.c:125
BOOL WriteToLogPrintf(IN PLOG_FILE LogFile, IN LPCWSTR Format,...)
Definition: winstation.c:103
BOOL WriteToLogPuts(IN PLOG_FILE LogFile, IN LPCWSTR String)
Definition: winstation.c:78
struct _LOG_FILE * PLOG_FILE
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
Definition: winstation.c:230
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
Definition: winstation.c:204
BOOL WriteToLog(IN PLOG_FILE LogFile, IN LPCVOID Buffer, IN DWORD dwBufferSize)
Definition: winstation.c:66
VOID CloseLog(IN PLOG_FILE LogFile)
Definition: winstation.c:58
struct _LOG_FILE LOG_FILE
#define ZeroMemory
Definition: winbase.h:1712
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
LONG_PTR LPARAM
Definition: windef.h:208
UINT_PTR WPARAM
Definition: windef.h:207
CONST void * LPCVOID
Definition: windef.h:191
#define IDCANCEL
Definition: winuser.h:831
HWINSTA WINAPI CreateWindowStationW(_In_opt_ LPCWSTR lpwinsta, _In_ DWORD dwFlags, _In_ ACCESS_MASK dwDesiredAccess, _In_opt_ LPSECURITY_ATTRIBUTES lpsa)
#define DialogBoxW(i, t, p, f)
Definition: winuser.h:4399
#define WM_COMMAND
Definition: winuser.h:1740
BOOL WINAPI CloseWindowStation(_In_ HWINSTA)
#define WM_INITDIALOG
Definition: winuser.h:1739
BOOL WINAPI SetProcessWindowStation(_In_ HWINSTA)
int WINAPI MessageBoxW(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
#define IDOK
Definition: winuser.h:830
#define MB_ICONERROR
Definition: winuser.h:787
BOOL WINAPI EnumDesktopsW(_In_opt_ HWINSTA, _In_ DESKTOPENUMPROCW, _In_ LPARAM)
#define WINSTA_ALL_ACCESS
Definition: winuser.h:417
#define MB_OK
Definition: winuser.h:790
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185