Process Hacker
sessprp.c
Go to the documentation of this file.
1 /*
2  * Process Hacker -
3  * session properties
4  *
5  * Copyright (C) 2010 wj32
6  *
7  * This file is part of Process Hacker.
8  *
9  * Process Hacker is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Process Hacker is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <phapp.h>
24 #include <winsta.h>
25 #include <ws2tcpip.h>
26 
27 INT_PTR CALLBACK PhpSessionPropertiesDlgProc(
28  _In_ HWND hwndDlg,
29  _In_ UINT uMsg,
30  _In_ WPARAM wParam,
31  _In_ LPARAM lParam
32  );
33 
34 #define SIP(String, Integer) { (String), (PVOID)(Integer) }
35 
36 static PH_KEY_VALUE_PAIR PhpConnectStatePairs[] =
37 {
38  SIP(L"Active", State_Active),
39  SIP(L"Connected", State_Connected),
40  SIP(L"ConnectQuery", State_ConnectQuery),
41  SIP(L"Shadow", State_Shadow),
42  SIP(L"Disconnected", State_Disconnected),
43  SIP(L"Idle", State_Idle),
44  SIP(L"Listen", State_Listen),
45  SIP(L"Reset", State_Reset),
46  SIP(L"Down", State_Down),
47  SIP(L"Init", State_Init)
48 };
49 
51  _In_ HWND ParentWindowHandle,
52  _In_ ULONG SessionId
53  )
54 {
55  DialogBoxParam(
57  MAKEINTRESOURCE(IDD_SESSION),
58  ParentWindowHandle,
60  (LPARAM)SessionId
61  );
62 }
63 
64 INT_PTR CALLBACK PhpSessionPropertiesDlgProc(
65  _In_ HWND hwndDlg,
66  _In_ UINT uMsg,
67  _In_ WPARAM wParam,
68  _In_ LPARAM lParam
69  )
70 {
71  switch (uMsg)
72  {
73  case WM_INITDIALOG:
74  {
75  ULONG sessionId = (ULONG)lParam;
76  WINSTATIONINFORMATION winStationInfo;
77  BOOLEAN haveWinStationInfo;
78  WINSTATIONCLIENT clientInfo;
79  BOOLEAN haveClientInfo;
80  ULONG returnLength;
81  PWSTR stateString;
82 
83  SetProp(hwndDlg, L"SessionId", (HANDLE)sessionId);
84  PhCenterWindow(hwndDlg, GetParent(hwndDlg));
85 
86  // Query basic session information
87 
88  haveWinStationInfo = WinStationQueryInformationW(
89  NULL,
90  sessionId,
92  &winStationInfo,
93  sizeof(WINSTATIONINFORMATION),
94  &returnLength
95  );
96 
97  // Query client information
98 
99  haveClientInfo = WinStationQueryInformationW(
100  NULL,
101  sessionId,
103  &clientInfo,
104  sizeof(WINSTATIONCLIENT),
105  &returnLength
106  );
107 
108  if (haveWinStationInfo)
109  {
110  SetDlgItemText(hwndDlg, IDC_USERNAME,
111  PhaFormatString(L"%s\\%s", winStationInfo.Domain, winStationInfo.UserName)->Buffer);
112  }
113 
114  SetDlgItemInt(hwndDlg, IDC_SESSIONID, sessionId, FALSE);
115 
116  if (haveWinStationInfo)
117  {
119  PhpConnectStatePairs,
120  sizeof(PhpConnectStatePairs),
121  winStationInfo.ConnectState,
122  &stateString
123  ))
124  {
125  SetDlgItemText(hwndDlg, IDC_STATE, stateString);
126  }
127  }
128 
129  if (haveWinStationInfo && winStationInfo.LogonTime.QuadPart != 0)
130  {
131  SYSTEMTIME systemTime;
132  PPH_STRING time;
133 
134  PhLargeIntegerToLocalSystemTime(&systemTime, &winStationInfo.LogonTime);
135  time = PhFormatDateTime(&systemTime);
136  SetDlgItemText(hwndDlg, IDC_LOGONTIME, time->Buffer);
137  PhDereferenceObject(time);
138  }
139 
140  if (haveWinStationInfo && winStationInfo.ConnectTime.QuadPart != 0)
141  {
142  SYSTEMTIME systemTime;
143  PPH_STRING time;
144 
145  PhLargeIntegerToLocalSystemTime(&systemTime, &winStationInfo.ConnectTime);
146  time = PhFormatDateTime(&systemTime);
147  SetDlgItemText(hwndDlg, IDC_CONNECTTIME, time->Buffer);
148  PhDereferenceObject(time);
149  }
150 
151  if (haveWinStationInfo && winStationInfo.DisconnectTime.QuadPart != 0)
152  {
153  SYSTEMTIME systemTime;
154  PPH_STRING time;
155 
156  PhLargeIntegerToLocalSystemTime(&systemTime, &winStationInfo.DisconnectTime);
157  time = PhFormatDateTime(&systemTime);
158  SetDlgItemText(hwndDlg, IDC_DISCONNECTTIME, time->Buffer);
159  PhDereferenceObject(time);
160  }
161 
162  if (haveWinStationInfo && winStationInfo.LastInputTime.QuadPart != 0)
163  {
164  SYSTEMTIME systemTime;
165  PPH_STRING time;
166 
167  PhLargeIntegerToLocalSystemTime(&systemTime, &winStationInfo.LastInputTime);
168  time = PhFormatDateTime(&systemTime);
169  SetDlgItemText(hwndDlg, IDC_LASTINPUTTIME, time->Buffer);
170  PhDereferenceObject(time);
171  }
172 
173  if (haveClientInfo && clientInfo.ClientName[0] != 0)
174  {
175  WCHAR addressString[65];
176 
177  SetDlgItemText(hwndDlg, IDC_CLIENTNAME, clientInfo.ClientName);
178 
179  if (clientInfo.ClientAddressFamily == AF_INET6)
180  {
181  struct in6_addr address;
182  ULONG i;
183  PUSHORT in;
184  PUSHORT out;
185 
186  // IPv6 is special - the client address data is a reversed version of
187  // the real address.
188 
189  in = (PUSHORT)clientInfo.ClientAddress;
190  out = (PUSHORT)address.u.Word;
191 
192  for (i = 8; i != 0; i--)
193  {
194  *out = _byteswap_ushort(*in);
195  in++;
196  out++;
197  }
198 
199  RtlIpv6AddressToString(&address, addressString);
200  }
201  else
202  {
203  wcscpy_s(addressString, 65, clientInfo.ClientAddress);
204  }
205 
206  SetDlgItemText(hwndDlg, IDC_CLIENTADDRESS, addressString);
207 
208  SetDlgItemText(hwndDlg, IDC_CLIENTDISPLAY,
209  PhaFormatString(L"%ux%u@%u", clientInfo.HRes,
210  clientInfo.VRes, clientInfo.ColorDepth)->Buffer
211  );
212  }
213 
214  SendMessage(hwndDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hwndDlg, IDOK), TRUE);
215  }
216  break;
217  case WM_DESTROY:
218  {
219  RemoveProp(hwndDlg, L"SessionId");
220  }
221  break;
222  case WM_COMMAND:
223  {
224  switch (LOWORD(wParam))
225  {
226  case IDCANCEL:
227  case IDOK:
228  EndDialog(hwndDlg, IDOK);
229  break;
230  }
231  }
232  break;
233  }
234 
235  return FALSE;
236 }