Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
7.9 KiB

  1. #include "common.h"
  2. #include <windowsx.h>
  3. #include <stdio.h>
  4. #include "w3scon.h"
  5. CW3SpoofUI* g_pw3sui = NULL;
  6. LPCWSTR g_wszShowUI = L"ShowUI";
  7. LPCWSTR g_wszNIFText1 = L"W3Spoof is running.";
  8. //-----------------------------------------------------------------------------
  9. // Program entry
  10. //-----------------------------------------------------------------------------
  11. int
  12. WINAPI
  13. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  14. {
  15. HRESULT hr = S_OK;
  16. IW3SpoofClientSupport* pcs = NULL;
  17. CoInitialize(NULL);
  18. hr = CoCreateInstance(
  19. CLSID_W3Spoof,
  20. NULL,
  21. CLSCTX_LOCAL_SERVER,
  22. IID_IW3SpoofClientSupport,
  23. (void**) &pcs
  24. );
  25. if( SUCCEEDED(hr) )
  26. {
  27. hr = CW3SpoofUI::Create(&g_pw3sui, hInstance, pcs);
  28. if( SUCCEEDED(hr) )
  29. {
  30. g_pw3sui->Run();
  31. }
  32. g_pw3sui->Terminate();
  33. }
  34. if( pcs )
  35. pcs->Release();
  36. CoUninitialize();
  37. return 0L;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // CW3SpoofUI methods
  41. //-----------------------------------------------------------------------------
  42. CW3SpoofUI::CW3SpoofUI()
  43. {
  44. m_hInst = NULL;
  45. m_hWnd = NULL;
  46. m_cRefs = 1L;
  47. m_hIcon = NULL;
  48. }
  49. CW3SpoofUI::~CW3SpoofUI()
  50. {
  51. }
  52. HRESULT
  53. CW3SpoofUI::Create(CW3SpoofUI** ppw3sui, HINSTANCE hInst, IW3SpoofClientSupport* pcs)
  54. {
  55. HRESULT hr = S_OK;
  56. if( !ppw3sui )
  57. {
  58. hr = E_INVALIDARG;
  59. }
  60. else
  61. {
  62. *ppw3sui = new CW3SpoofUI;
  63. if( !(*ppw3sui) )
  64. {
  65. hr = E_OUTOFMEMORY;
  66. }
  67. else
  68. {
  69. hr = (*ppw3sui)->Initialize(hInst, pcs);
  70. }
  71. }
  72. return hr;
  73. }
  74. HRESULT
  75. CW3SpoofUI::Initialize(HINSTANCE hInst, IW3SpoofClientSupport* pcs)
  76. {
  77. LPDWORD pdw = NULL;
  78. HRESULT hr = S_OK;
  79. IConnectionPointContainer* pCPC = NULL;
  80. if( !hInst )
  81. {
  82. hr = E_INVALIDARG;
  83. goto quit;
  84. }
  85. else
  86. {
  87. m_hInst = hInst;
  88. }
  89. //
  90. // load our friendly icon and if successful create the ui
  91. //
  92. m_hIcon = (HICON) LoadImage(m_hInst, L"IDI_W3SPOOF", IMAGE_ICON, 16, 16, 0);
  93. if( m_hIcon )
  94. {
  95. hr = _CreateUI();
  96. }
  97. else
  98. {
  99. hr = E_FAIL;
  100. goto quit;
  101. }
  102. //
  103. // sink the IW3SpoofEvents interface.
  104. //
  105. hr = pcs->QueryInterface(IID_IConnectionPointContainer, (void**) &pCPC);
  106. if( SUCCEEDED(hr) )
  107. {
  108. hr = pCPC->FindConnectionPoint(IID_IW3SpoofEvents, &m_pCP);
  109. if( SUCCEEDED(hr) )
  110. {
  111. pCPC->Release();
  112. }
  113. else
  114. {
  115. goto quit;
  116. }
  117. hr = m_pCP->Advise(static_cast<IUnknown*>(this), &m_dwCookie);
  118. }
  119. quit:
  120. return hr;
  121. }
  122. HRESULT
  123. CW3SpoofUI::Terminate(void)
  124. {
  125. HRESULT hr = S_OK;
  126. if( m_pCP )
  127. m_pCP->Unadvise(m_dwCookie);
  128. SAFERELEASE(m_pCP);
  129. _DestroyTrayIcon();
  130. //
  131. // the ui object is created with a refcount of 1. this release will
  132. // drop the refcount to 0 and cause the object to be deleted.
  133. //
  134. Release();
  135. return hr;
  136. }
  137. HRESULT
  138. CW3SpoofUI::Run()
  139. {
  140. HRESULT hr = S_OK;
  141. MSG msg;
  142. while( GetMessage(&msg, NULL, 0, 0) )
  143. {
  144. TranslateMessage(&msg);
  145. DispatchMessage(&msg);
  146. }
  147. return hr;
  148. }
  149. HRESULT
  150. CW3SpoofUI::_CreateUI(void)
  151. {
  152. HRESULT hr = S_OK;
  153. DWORD dwRet = ERROR_SUCCESS;
  154. WNDCLASSEX wc = {0};
  155. wc.cbSize = sizeof(wc);
  156. wc.cbClsExtra = 0;
  157. wc.cbWndExtra = 0;
  158. wc.hInstance = m_hInst;
  159. wc.lpfnWndProc = WndProc;
  160. wc.lpszMenuName = NULL;
  161. wc.lpszClassName = L"w3spoof_main";
  162. wc.style = CS_HREDRAW | CS_VREDRAW;
  163. wc.hIcon = m_hIcon;
  164. wc.hIconSm = NULL;
  165. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  166. wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  167. RegisterClassEx(&wc);
  168. m_hWnd = CreateWindow(
  169. wc.lpszClassName,
  170. L"w3spoof",
  171. WS_OVERLAPPEDWINDOW,
  172. CW_USEDEFAULT,
  173. CW_USEDEFAULT,
  174. 600,
  175. 600,
  176. NULL,
  177. NULL,
  178. wc.hInstance,
  179. NULL
  180. );
  181. if( m_hWnd )
  182. {
  183. hr = _CreateTrayIcon();
  184. }
  185. else
  186. {
  187. hr = E_FAIL;
  188. goto quit;
  189. }
  190. ShowWindow(m_hWnd, SW_SHOWNORMAL);
  191. UpdateWindow(m_hWnd);
  192. quit:
  193. return hr;
  194. }
  195. HRESULT
  196. CW3SpoofUI::_CreateTrayIcon(void)
  197. {
  198. HRESULT hr = S_OK;
  199. NOTIFYICONDATA nid = {0};
  200. nid.cbSize = sizeof(NOTIFYICONDATA);
  201. nid.hWnd = m_hWnd;
  202. nid.uID = SHELLMESSAGE_W3SICON;
  203. nid.uCallbackMessage = nid.uID;
  204. nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  205. nid.hIcon = m_hIcon;
  206. wsprintf(nid.szTip, L"%s", g_wszNIFText1);
  207. if( !Shell_NotifyIcon(NIM_ADD, &nid) )
  208. {
  209. hr = E_FAIL;
  210. }
  211. return hr;
  212. }
  213. HRESULT
  214. CW3SpoofUI::_UpdateTrayIcon(void)
  215. {
  216. //
  217. // TODO: implementation
  218. //
  219. return S_OK;
  220. }
  221. HRESULT
  222. CW3SpoofUI::_DestroyTrayIcon(void)
  223. {
  224. HRESULT hr = S_OK;
  225. NOTIFYICONDATA nid = {0};
  226. nid.cbSize = sizeof(NOTIFYICONDATA);
  227. nid.hWnd = m_hWnd;
  228. nid.uID = SHELLMESSAGE_W3SICON;
  229. nid.uCallbackMessage = nid.uID;
  230. if( !Shell_NotifyIcon(NIM_DELETE, &nid) )
  231. {
  232. hr = E_FAIL;
  233. }
  234. DestroyIcon(m_hIcon);
  235. return hr;
  236. }
  237. void
  238. CW3SpoofUI::_WriteText(const WCHAR* format, ...)
  239. {
  240. int ret = 0;
  241. int offset = 0;
  242. WCHAR* timestamp = new WCHAR[256];
  243. WCHAR* buffer = new WCHAR[1024];
  244. va_list arg_list;
  245. SYSTEMTIME st;
  246. GetLocalTime(&st);
  247. wsprintf(
  248. timestamp,
  249. L"%0.2d:%0.2d:%0.2d.%0.3d",
  250. st.wHour,
  251. st.wMinute,
  252. st.wSecond,
  253. st.wMilliseconds
  254. );
  255. offset = wsprintf(buffer, L"[%s] ", timestamp);
  256. va_start(arg_list, format);
  257. _vsnwprintf(
  258. (buffer + offset),
  259. (1024 - wcslen(timestamp)),
  260. format,
  261. arg_list
  262. );
  263. va_end(arg_list);
  264. do
  265. {
  266. if( (ret = ListBox_AddString(m_listbox, buffer)) == LB_ERR )
  267. {
  268. ListBox_DeleteString(m_listbox, 0);
  269. }
  270. else
  271. {
  272. ListBox_SetCurSel(m_listbox, ret);
  273. }
  274. }
  275. while( ret == LB_ERR );
  276. SAFEDELETEBUF(timestamp);
  277. SAFEDELETEBUF(buffer);
  278. }
  279. //-----------------------------------------------------------------------------
  280. // IUnknown
  281. //-----------------------------------------------------------------------------
  282. HRESULT
  283. __stdcall
  284. CW3SpoofUI::QueryInterface(REFIID riid, void** ppv)
  285. {
  286. HRESULT hr = S_OK;
  287. if( !ppv )
  288. {
  289. hr = E_POINTER;
  290. }
  291. else
  292. {
  293. if( IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IW3SpoofEvents))
  294. {
  295. *ppv = static_cast<IW3SpoofEvents*>(this);
  296. }
  297. else
  298. {
  299. *ppv = NULL;
  300. hr = E_NOINTERFACE;
  301. }
  302. if( SUCCEEDED(hr) )
  303. reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  304. }
  305. return hr;
  306. }
  307. ULONG
  308. __stdcall
  309. CW3SpoofUI::AddRef(void)
  310. {
  311. InterlockedIncrement(&m_cRefs);
  312. return m_cRefs;
  313. }
  314. ULONG
  315. __stdcall
  316. CW3SpoofUI::Release(void)
  317. {
  318. InterlockedDecrement(&m_cRefs);
  319. if( m_cRefs == 0 )
  320. {
  321. delete this;
  322. return 0;
  323. }
  324. return m_cRefs;
  325. }
  326. //-----------------------------------------------------------------------------
  327. // IW3SpoofEvents methods
  328. //-----------------------------------------------------------------------------
  329. HRESULT
  330. __stdcall
  331. CW3SpoofUI::OnSessionOpen(LPWSTR clientid)
  332. {
  333. _WriteText(
  334. L"client %s session is open",
  335. clientid
  336. );
  337. return S_OK;
  338. }
  339. HRESULT
  340. __stdcall
  341. CW3SpoofUI::OnSessionStateChange(LPWSTR clientid, STATE state)
  342. {
  343. return S_OK;
  344. }
  345. HRESULT
  346. __stdcall
  347. CW3SpoofUI::OnSessionClose(LPWSTR clientid)
  348. {
  349. _WriteText(
  350. L"client %s session is closed",
  351. clientid
  352. );
  353. return S_OK;
  354. }