ReactOS  0.4.14-dev-614-gbfd8a84
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 
22 typedef struct _LOG_FILE
23 {
26  size_t cbBufferSize;
28 
29 BOOL
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 
57 VOID
59  IN PLOG_FILE LogFile)
60 {
61  CloseHandle(LogFile->hLogFile);
62  ZeroMemory(LogFile, sizeof(*LogFile));
63 }
64 
65 BOOL
67  IN PLOG_FILE LogFile,
69  IN DWORD dwBufferSize)
70 {
71  return WriteFile(LogFile->hLogFile,
72  Buffer,
73  dwBufferSize,
74  &dwBufferSize, NULL);
75 }
76 
77 BOOL
79  IN PLOG_FILE LogFile,
81 {
82  return WriteToLog(LogFile,
83  String,
84  wcslen(String) * sizeof(WCHAR));
85 }
86 
87 BOOL
89  IN PLOG_FILE LogFile,
91  IN va_list args)
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 
102 BOOL
104  IN PLOG_FILE LogFile,
105  IN LPCWSTR Format,
106  ...)
107 {
108  BOOL bRet;
109  va_list args;
110 
111  va_start(args, Format);
112  bRet = WriteToLogPrintfV(LogFile, Format, args);
113  va_end(args);
114 
115  return bRet;
116 }
117 
118 
119 /*
120  * Window Station tests
121  */
122 
123 BOOL
124 CALLBACK
126  IN LPWSTR lpszDesktop,
127  IN LPARAM lParam)
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 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define IN
Definition: typedefs.h:38
Definition: tftpd.h:59
#define TRUE
Definition: types.h:120
BOOL WINAPI AllocConsole(VOID)
Definition: console.c:48
#define IDOK
Definition: winuser.h:824
#define CloseHandle
Definition: compat.h:406
struct _LOG_FILE LOG_FILE
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
BOOL WriteToLogPuts(IN PLOG_FILE LogFile, IN LPCWSTR String)
Definition: winstation.c:78
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:323
#define CALLBACK
Definition: compat.h:27
HWND hWnd
Definition: settings.c:17
#define INVALID_HANDLE_VALUE
Definition: compat.h:399
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define ZeroMemory
Definition: winbase.h:1642
BOOL WriteToLogPrintfV(IN PLOG_FILE LogFile, IN LPCWSTR Format, IN va_list args)
Definition: winstation.c:88
BOOL WINAPI SetProcessWindowStation(_In_ HWINSTA)
static WCHAR String[]
Definition: stringtable.c:55
UINT_PTR WPARAM
Definition: windef.h:207
uint16_t * PWCHAR
Definition: typedefs.h:54
int32_t INT_PTR
Definition: typedefs.h:62
Definition: match.c:390
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
WPARAM wParam
Definition: combotst.c:138
STRSAFEAPI StringCbVPrintfW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszFormat, va_list argList)
Definition: strsafe.h:507
#define FILE_SHARE_READ
Definition: compat.h:125
HANDLE hLogFile
Definition: winstation.c:24
HINSTANCE hInstance
Definition: charmap.c:20
struct _LOG_FILE * PLOG_FILE
BOOL WriteToLog(IN PLOG_FILE LogFile, IN LPCVOID Buffer, IN DWORD dwBufferSize)
Definition: winstation.c:66
#define va_end(ap)
Definition: acmsvcex.h:90
unsigned int BOOL
Definition: ntddk_ex.h:94
#define IDD_ABOUTBOX
Definition: resource.h:8
#define GENERIC_WRITE
Definition: nt_native.h:90
PWCHAR pBuffer
Definition: winstation.c:25
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
LONG_PTR LPARAM
Definition: windef.h:208
char * va_list
Definition: acmsvcex.h:78
size_t cbBufferSize
Definition: winstation.c:26
Definition: bufpool.h:45
VOID DoTest(HWND hWnd)
Definition: winstation.c:143
#define DialogBoxW(i, t, p, f)
Definition: winuser.h:4374
int WINAPI MessageBoxW(_In_opt_ HWND, _In_opt_ LPCWSTR, _In_opt_ LPCWSTR, _In_ UINT)
__wchar_t WCHAR
Definition: xmlstorage.h:180
BOOL WINAPI EnumDesktopsW(_In_opt_ HWINSTA, _In_ DESKTOPENUMPROCW, _In_ LPARAM)
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
Definition: winstation.c:230
unsigned long DWORD
Definition: ntddk_ex.h:95
HANDLE hLogFile
Definition: logfile.c:34
#define OPEN_ALWAYS
Definition: disk.h:70
_Must_inspect_result_ _In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PUNICODE_STRING LogFileName
Definition: nttmapi.h:322
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
static const WCHAR L[]
Definition: oid.c:1250
#define MB_ICONERROR
Definition: winuser.h:781
#define WM_COMMAND
Definition: winuser.h:1722
BOOL WriteToLogPrintf(IN PLOG_FILE LogFile, IN LPCWSTR Format,...)
Definition: winstation.c:103
#define WINSTA_ALL_ACCESS
Definition: winuser.h:417
BOOL CALLBACK EnumDesktopProc(IN LPWSTR lpszDesktop, IN LPARAM lParam)
Definition: winstation.c:125
#define va_start(ap, A)
Definition: acmsvcex.h:91
unsigned int UINT
Definition: ndis.h:50
VOID CloseLog(IN PLOG_FILE LogFile)
Definition: winstation.c:58
#define MB_OK
Definition: winuser.h:784
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
Definition: winstation.c:204
#define CreateFileW
Definition: compat.h:408
CONST void * LPCVOID
Definition: windef.h:191
#define OUT
Definition: typedefs.h:39
HWINSTA WINAPI CreateWindowStationW(_In_opt_ LPCWSTR lpwinsta, _In_ DWORD dwFlags, _In_ ACCESS_MASK dwDesiredAccess, _In_opt_ LPSECURITY_ATTRIBUTES lpsa)
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define IDCANCEL
Definition: winuser.h:825
#define args
Definition: format.c:66
#define WM_INITDIALOG
Definition: winuser.h:1721
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define APIENTRY
Definition: api.h:79
BOOL WINAPI CloseWindowStation(_In_ HWINSTA)
BOOL InitLog(OUT PLOG_FILE LogFile, IN LPCWSTR LogFileName, IN PWCHAR pBuffer, IN size_t cbBufferSize)
Definition: winstation.c:30