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.

342 lines
7.5 KiB

  1. // WmiCtrsDlg.cpp : implementation file
  2. //
  3. #include "precomp.h"
  4. #include "WmiCtrsDlg.h"
  5. #include "resource.h"
  6. #include <process.h>
  7. #include <util.h>
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. WCHAR g_szCounters[][40] = { L"Connections",
  14. L"DeliveryBackupBytes",
  15. L"InternalObjects",
  16. L"InternalSinks",
  17. L"TasksInProgress",
  18. L"TasksWaiting",
  19. L"TotalAPIcalls",
  20. L"Users"
  21. };
  22. UINT g_uCtrIds[] = { IDC_CONNECTIONS,
  23. IDC_DELBACKUP,
  24. IDC_INTERNALOBJS,
  25. IDC_INTERNALSINKS,
  26. IDC_TASKSINPROG,
  27. IDC_TASKSWAITING,
  28. IDC_TOTALAPICALLS,
  29. IDC_USERS
  30. };
  31. /////////////////////////////////////////////////////////////////////////////
  32. // WmiCtrsDlg dialog
  33. CWmiCtrsDlg::CWmiCtrsDlg()
  34. {
  35. m_pCredentials = NULL;
  36. _tcscpy(m_szMachineName,_T(""));
  37. m_pDlg = NULL;
  38. m_hWndBusy = NULL;
  39. }
  40. CWmiCtrsDlg::CWmiCtrsDlg(LPCTSTR szMachineName, LOGIN_CREDENTIALS *pCredentials)
  41. {
  42. m_pCredentials = pCredentials;
  43. _tcsncpy(m_szMachineName,szMachineName,ARRAYSIZE(m_szMachineName));
  44. m_pDlg = NULL;
  45. m_hWndBusy = NULL;
  46. }
  47. CWmiCtrsDlg::~CWmiCtrsDlg()
  48. {
  49. if(m_pDlg != NULL)
  50. delete m_pDlg;
  51. if(m_hWndBusy != NULL)
  52. {
  53. //Now close the busy Dialog
  54. SendMessage(*(m_hWndBusy),WM_CLOSE_BUSY_DLG,0,0);
  55. delete m_hWndBusy;
  56. }
  57. }
  58. INT_PTR CWmiCtrsDlg::DoModal(HWND hWnd)
  59. {
  60. m_bRun = true;
  61. INT_PTR retVal = DialogBoxParam(_Module.GetModuleInstance(),
  62. MAKEINTRESOURCE(IDD_WMICTR),
  63. hWnd, CtrDlgProc,
  64. (LPARAM)this);
  65. m_bRun = false;
  66. return retVal;
  67. }
  68. INT_PTR CALLBACK CtrDlgProc(HWND hwndDlg,
  69. UINT uMsg,
  70. WPARAM wParam,
  71. LPARAM lParam)
  72. {
  73. INT_PTR retVal = TRUE;
  74. CWmiCtrsDlg *pDlg = (CWmiCtrsDlg *)lParam;
  75. switch(uMsg)
  76. {
  77. case WM_INITDIALOG:
  78. {
  79. pDlg->InitDlg(hwndDlg);
  80. break;
  81. }
  82. case WM_COMMAND :
  83. {
  84. switch(LOWORD(wParam))
  85. {
  86. case IDOK:
  87. {
  88. EndDialog(hwndDlg, IDOK);
  89. break;
  90. }
  91. case IDCANCEL:
  92. {
  93. EndDialog(hwndDlg, IDCANCEL);
  94. break;
  95. }
  96. default:
  97. retVal = FALSE;
  98. }
  99. break;
  100. }
  101. default:
  102. {
  103. retVal = FALSE;
  104. break;
  105. }
  106. }
  107. return retVal;
  108. }
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CWmiCtrsDlg message handlers
  111. DWORD WINAPI CountersThread(LPVOID lpParameter)
  112. {
  113. CoInitialize(NULL);
  114. CWmiCtrsDlg *pDlg = (CWmiCtrsDlg *)lpParameter;
  115. CWbemServices Services;
  116. TCHAR nameSpace[1024];
  117. // if machine is whacked already...
  118. if(_tcsncmp(pDlg->m_szMachineName, _T("\\"), 1) == 0)
  119. {
  120. // use it.
  121. _tcscpy(nameSpace,pDlg->m_szMachineName);
  122. _tcscat(nameSpace,_T("\\root\\CimV2"));
  123. }
  124. else if(_tcslen(pDlg->m_szMachineName) > 0)
  125. {
  126. // whack it myself.
  127. _tcscpy(nameSpace,_T("\\\\"));
  128. _tcscat(nameSpace,pDlg->m_szMachineName);
  129. _tcscat(nameSpace,_T("\\root\\CimV2"));
  130. }
  131. else
  132. {
  133. _tcscpy(nameSpace,_T("root\\CimV2"));
  134. }
  135. HRESULT hr = Services.ConnectServer(nameSpace,pDlg->m_pCredentials);
  136. if(FAILED(hr))
  137. {
  138. //Display a MessageBox to the user that the connection has been failed
  139. pDlg->DisplayErrorMessage(IDC_ERROR_CONNECT);
  140. return 0;
  141. }
  142. //Now try to Get the instances of the class Win32_PerfRawData_WinMgmt_WINMGMTCounters
  143. IEnumWbemClassObject *Enum = NULL;
  144. ULONG uReturned;
  145. IWbemClassObject *pInst = NULL;
  146. hr = Services.CreateInstanceEnum(L"Win32_PerfRawData_WinMgmt_WINMGMTCounters",
  147. WBEM_FLAG_SHALLOW,
  148. &Enum);
  149. if(SUCCEEDED(hr))
  150. {
  151. // get the first and only instance.
  152. Enum->Next(-1, 1, &pInst, &uReturned);
  153. Enum->Release();
  154. }
  155. else
  156. {
  157. pDlg->DisplayErrorMessage(IDC_ERROR_ENUM);
  158. return 0;
  159. }
  160. if(pDlg->m_hWndBusy != NULL)
  161. {
  162. //Now close the nusy Dialog
  163. SendMessage(*(pDlg->m_hWndBusy),WM_CLOSE_BUSY_DLG,0,0);
  164. }
  165. int i;
  166. VARIANT vt;
  167. TCHAR strVal[1024];
  168. VariantInit(&vt);
  169. while(pDlg->m_bRun == true)
  170. {
  171. for(i=0; i < NUM_COUNTERS; i++)
  172. {
  173. VariantClear(&vt);
  174. hr = pInst->Get(g_szCounters[i],0,&vt,NULL,NULL);
  175. /* if(FAILED(hr))
  176. {
  177. ::MessageBox(NULL,_T("error"),_T("Unable to get"),MB_OK);
  178. }
  179. */ _itot(vt.lVal,strVal,10);
  180. SetWindowText(pDlg->m_hWndCounters[i],strVal);
  181. }
  182. Sleep(900);
  183. }
  184. Services.DisconnectServer();
  185. return 1;
  186. }
  187. void CWmiCtrsDlg::InitDlg(HWND hDlg)
  188. {
  189. DWORD dwThreadId;
  190. //Do the member Initializations
  191. m_pDlg = new HWND;
  192. if (!m_pDlg)
  193. return;
  194. *m_pDlg = hDlg;
  195. m_hWndBusy = new HWND;
  196. ::LoadString(_Module.GetModuleInstance(), IDC_ERROR_CAPTION, m_szError, 100);
  197. for(int i=0;i < NUM_COUNTERS; i++)
  198. {
  199. m_hWndCounters[i] = GetDlgItem(hDlg,g_uCtrIds[i]);
  200. }
  201. m_hThread = CreateThread(NULL,0,CountersThread,(LPVOID)this,0,&dwThreadId);
  202. //Now Display the "Connecting to WMI" Dialog
  203. DisplayBusyDialog(hDlg);
  204. }
  205. void CWmiCtrsDlg::DisplayErrorMessage(UINT ErrorId)
  206. {
  207. TCHAR szErrorText[1024];
  208. if(m_hWndBusy != NULL)
  209. {
  210. //Now close the busy Dialog
  211. SendMessage(*(m_hWndBusy),WM_CLOSE_BUSY_DLG,0,0);
  212. }
  213. ::LoadString(_Module.GetModuleInstance(), ErrorId, szErrorText, 1024);
  214. EndDialog(*m_pDlg,IDCANCEL);
  215. ::MessageBox(NULL,szErrorText,m_szError,MB_OK);
  216. }
  217. INT_PTR CALLBACK BusyDlgProc(HWND hwndDlg,
  218. UINT uMsg,
  219. WPARAM wParam,
  220. LPARAM lParam)
  221. {
  222. BOOL retval = FALSE;
  223. switch(uMsg)
  224. {
  225. case WM_INITDIALOG:
  226. {//BEGIN
  227. //lParam = ANIMCONFIG *
  228. CWmiCtrsDlg *pDlg = (CWmiCtrsDlg *)lParam;
  229. SetWindowLongPtr(hwndDlg, DWLP_USER, (LPARAM)pDlg);
  230. *(pDlg->m_hWndBusy) = hwndDlg;
  231. HWND hAnim = GetDlgItem(hwndDlg, IDC_ANIMATE);
  232. HWND hMsg = GetDlgItem(hwndDlg, IDC_MSG);
  233. Animate_Open(hAnim, MAKEINTRESOURCE(IDR_AVIWAIT));
  234. TCHAR caption[100] = {0}, msg[256] = {0};
  235. ::LoadString(_Module.GetModuleInstance(), IDS_DISPLAY_NAME, caption, 100);
  236. ::LoadString(_Module.GetModuleInstance(), IDS_CONNECTING, msg, 256);
  237. SetWindowText(hwndDlg, caption);
  238. SetWindowText(hMsg, msg);
  239. retval = TRUE;
  240. break;
  241. }
  242. case WM_CLOSE_BUSY_DLG:
  243. {
  244. HWND *me = (HWND *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  245. *me = 0;
  246. EndDialog(hwndDlg, IDCANCEL);
  247. break;
  248. }
  249. case WM_COMMAND:
  250. {
  251. // they're only one button.
  252. if(HIWORD(wParam) == BN_CLICKED)
  253. {
  254. // I'm going away now so anybody that has a ptr to my
  255. // hwnd (which I gave out in my WM_INITDIALOG) shouldn't
  256. // use it anymore.
  257. HWND *me = (HWND *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  258. *me = 0;
  259. EndDialog(hwndDlg, IDCANCEL);
  260. }
  261. retval = TRUE; // I processed it.
  262. break;
  263. }
  264. case WM_DESTROY:
  265. {// BEGIN
  266. // I'm going away now so anybody that has a ptr to my
  267. // hwnd (which I gave out in my WM_INITDIALOG) shouldn't
  268. // use it anymore.
  269. HWND *me = (HWND *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  270. *me = 0;
  271. retval = TRUE; // I processed it.
  272. break;
  273. } //END
  274. default:
  275. {
  276. retval = FALSE; // I did NOT process this msg.
  277. break;
  278. }
  279. } //endswitch uMsg
  280. return retval;
  281. }
  282. INT_PTR CWmiCtrsDlg::DisplayBusyDialog(HWND hWnd)
  283. {
  284. return DialogBoxParam(_Module.GetModuleInstance(),
  285. MAKEINTRESOURCE(IDD_ANIMATE),
  286. NULL, BusyDlgProc,
  287. (LPARAM)this);
  288. }
  289. void CWmiCtrsDlg::CloseBusyDialog()
  290. {
  291. if(m_hWndBusy != NULL)
  292. {
  293. //Now close the nusy Dialog
  294. SendMessage(*(m_hWndBusy),WM_CLOSE_BUSY_DLG,0,0);
  295. }
  296. }