Source code of Windows XP (NT5)
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.

568 lines
13 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. --*/
  4. // EnumGuidDlg.cpp : implementation file
  5. //
  6. #include "stdafx.h"
  7. #include "EnumGuid.h"
  8. #include "EnumGDlg.h"
  9. #pragma warning (once : 4200)
  10. #include <wmium.h>
  11. #include "DspDataDlg.h"
  12. #include "SelName.h"
  13. #include "SelData.h"
  14. #include "SelDataM.h"
  15. #include "wmihlp.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CAboutDlg dialog used for App About
  23. #define MAX_DATA 200
  24. class CAboutDlg : public CDialog
  25. {
  26. public:
  27. CAboutDlg();
  28. // Dialog Data
  29. //{{AFX_DATA(CAboutDlg)
  30. enum { IDD = IDD_ABOUTBOX };
  31. //}}AFX_DATA
  32. // ClassWizard generated virtual function overrides
  33. //{{AFX_VIRTUAL(CAboutDlg)
  34. protected:
  35. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  36. //}}AFX_VIRTUAL
  37. // Implementation
  38. protected:
  39. //{{AFX_MSG(CAboutDlg)
  40. //}}AFX_MSG
  41. DECLARE_MESSAGE_MAP()
  42. };
  43. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  44. {
  45. //{{AFX_DATA_INIT(CAboutDlg)
  46. //}}AFX_DATA_INIT
  47. }
  48. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  49. {
  50. CDialog::DoDataExchange(pDX);
  51. //{{AFX_DATA_MAP(CAboutDlg)
  52. //}}AFX_DATA_MAP
  53. }
  54. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  55. //{{AFX_MSG_MAP(CAboutDlg)
  56. // No message handlers
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CEnumGuidDlg dialog
  61. CEnumGuidDlg::CEnumGuidDlg(CWnd* pParent /*=NULL*/)
  62. : CDialog(CEnumGuidDlg::IDD, pParent), guids(0)
  63. {
  64. //{{AFX_DATA_INIT(CEnumGuidDlg)
  65. // NOTE: the ClassWizard will add member initialization here
  66. //}}AFX_DATA_INIT
  67. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  68. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  69. }
  70. CEnumGuidDlg::~CEnumGuidDlg()
  71. {
  72. if (guids)
  73. delete[] guids;
  74. }
  75. void CEnumGuidDlg::DoDataExchange(CDataExchange* pDX)
  76. {
  77. CDialog::DoDataExchange(pDX);
  78. //{{AFX_DATA_MAP(CEnumGuidDlg)
  79. DDX_Control(pDX, IDC_GUID_LIST, guidList);
  80. //}}AFX_DATA_MAP
  81. }
  82. BEGIN_MESSAGE_MAP(CEnumGuidDlg, CDialog)
  83. //{{AFX_MSG_MAP(CEnumGuidDlg)
  84. ON_WM_SYSCOMMAND()
  85. ON_WM_PAINT()
  86. ON_WM_QUERYDRAGICON()
  87. ON_BN_CLICKED(IDC_QUERY_FULL, OnQueryFull)
  88. ON_BN_CLICKED(IDC_QUERY_SINGLE, OnQuerySingle)
  89. ON_BN_CLICKED(IDC_SET_SINGLE_INSTANCE, OnSetSingleInstance)
  90. ON_BN_CLICKED(IDC_SET_SINGLE_ITEM, OnSetSingleItem)
  91. ON_BN_CLICKED(IDC_REENUMERATE_GUIDS, OnReenumerateGuids)
  92. //}}AFX_MSG_MAP
  93. END_MESSAGE_MAP()
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CEnumGuidDlg message handlers
  96. BOOL CEnumGuidDlg::OnInitDialog()
  97. {
  98. CDialog::OnInitDialog();
  99. // Add "About..." menu item to system menu.
  100. // IDM_ABOUTBOX must be in the system command range.
  101. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  102. ASSERT(IDM_ABOUTBOX < 0xF000);
  103. CMenu* pSysMenu = GetSystemMenu(FALSE);
  104. if (pSysMenu != NULL)
  105. {
  106. CString strAboutMenu;
  107. strAboutMenu.LoadString(IDS_ABOUTBOX);
  108. if (!strAboutMenu.IsEmpty())
  109. {
  110. pSysMenu->AppendMenu(MF_SEPARATOR);
  111. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  112. }
  113. }
  114. // Set the icon for this dialog. The framework does this automatically
  115. // when the application's main window is not a dialog
  116. SetIcon(m_hIcon, TRUE); // Set big icon
  117. SetIcon(m_hIcon, FALSE); // Set small icon
  118. OnReenumerateGuids();
  119. return TRUE;
  120. }
  121. void CEnumGuidDlg::OnSysCommand(UINT nID, LPARAM lParam)
  122. {
  123. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  124. {
  125. CAboutDlg dlgAbout;
  126. dlgAbout.DoModal();
  127. }
  128. else
  129. {
  130. CDialog::OnSysCommand(nID, lParam);
  131. }
  132. }
  133. // If you add a minimize button to your dialog, you will need the code below
  134. // to draw the icon. For MFC applications using the document/view model,
  135. // this is automatically done for you by the framework.
  136. void CEnumGuidDlg::OnPaint()
  137. {
  138. if (IsIconic())
  139. {
  140. CPaintDC dc(this); // device context for painting
  141. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  142. // Center icon in client rectangle
  143. int cxIcon = GetSystemMetrics(SM_CXICON);
  144. int cyIcon = GetSystemMetrics(SM_CYICON);
  145. CRect rect;
  146. GetClientRect(&rect);
  147. int x = (rect.Width() - cxIcon + 1) / 2;
  148. int y = (rect.Height() - cyIcon + 1) / 2;
  149. // Draw the icon
  150. dc.DrawIcon(x, y, m_hIcon);
  151. }
  152. else
  153. {
  154. CDialog::OnPaint();
  155. }
  156. }
  157. // The system calls this to obtain the cursor to display while the user drags
  158. // the minimized window.
  159. HCURSOR CEnumGuidDlg::OnQueryDragIcon()
  160. {
  161. return (HCURSOR) m_hIcon;
  162. }
  163. int CEnumGuidDlg::EnumerateGuids()
  164. {
  165. GUID guid;
  166. DWORD ret;
  167. CString msg;
  168. if (guids)
  169. delete[] guids;
  170. guidsCount = 0;
  171. //
  172. // Determine the present number of guids registered
  173. //
  174. ret = WmiEnumerateGuids(&guid,
  175. &guidsCount);
  176. if (ret != ERROR_MORE_DATA) {
  177. msg.Format(_T("Call to determine number of Guids failed. (%u)\n"), ret);
  178. MessageBox(msg);
  179. return (0);
  180. }
  181. // Return if there are no guids registered
  182. //
  183. if (guidsCount != 0) {
  184. // Allocate buffer for Guids. Note: we will allocate room for
  185. // and extra 10 Guids in case new Guids were registed between the
  186. // previous call to WMIMofEnumeratGuid, and the next call.
  187. //
  188. guidsCount += 10;
  189. guids = new GUID[guidsCount];
  190. if (!guids) {
  191. MessageBox(_T("Out of Memory Failure. Unable to allocate memory for guid list array\n"));
  192. return (0);
  193. }
  194. // Get the list of Guids
  195. //
  196. ret = WmiEnumerateGuids(guids,
  197. &guidsCount);
  198. if ((ret != ERROR_SUCCESS) && (ret != ERROR_MORE_DATA)) {
  199. msg.Format(_T("Failure to get guid list. (%u)\n"), ret);
  200. MessageBox(msg);
  201. return (0);
  202. }
  203. }
  204. return guidsCount;
  205. }
  206. void CEnumGuidDlg::ListGuids()
  207. {
  208. CString line;
  209. DWORD tmp1, tmp2, tmp3, tmp4, tmp5;
  210. for (ULONG i = 0; i < guidsCount; i++) {
  211. line.Empty();
  212. tmp1 = guids[i].Data2;
  213. tmp2 = guids[i].Data3;
  214. tmp3 = guids[i].Data4[0] << 8 | guids[i].Data4[1];
  215. tmp4 = guids[i].Data4[2] << 8 | guids[i].Data4[3];
  216. memcpy(&tmp5, &guids[i].Data4[4], sizeof(DWORD));
  217. tmp5 = guids[i].Data4[4] << 24 |
  218. guids[i].Data4[5] << 16 |
  219. guids[i].Data4[6] << 8 |
  220. guids[i].Data4[7];
  221. line.Format(_T("{%.8X-%.4X-%.4X-%.4X-%.4X%.8X}"),
  222. guids[i].Data1,
  223. tmp1,
  224. tmp2,
  225. tmp3,
  226. tmp4,
  227. tmp5);
  228. guidList.AddString(line);
  229. }
  230. }
  231. void CEnumGuidDlg::OnQueryFull()
  232. {
  233. HANDLE hDataBlock;
  234. DWORD dwRet;
  235. PBYTE Buffer;
  236. ULONG dwBufferSize = 0;
  237. PWNODE_ALL_DATA pWnode;
  238. LPGUID lpGuid = GetSelectedGuid();
  239. CString msg;
  240. // Open the wnode
  241. //
  242. dwRet = WmiOpenBlock(lpGuid, 0, &hDataBlock);
  243. if (dwRet != ERROR_SUCCESS) {
  244. msg.Format(_T("Unable to open data block (%u)\n"), dwRet);
  245. MessageBox(msg);
  246. return;
  247. }
  248. // Query the data block
  249. //
  250. dwRet = WmiQueryAllData(hDataBlock,
  251. &dwBufferSize,
  252. NULL);
  253. if (dwRet == ERROR_INSUFFICIENT_BUFFER) {
  254. #ifdef DBG
  255. printf("Initial buffer too small reallocating to %u\n", dwBufferSize);
  256. #endif
  257. Buffer = new BYTE[dwBufferSize];
  258. if (Buffer != NULL) {
  259. dwRet = WmiQueryAllData(hDataBlock,
  260. &dwBufferSize,
  261. Buffer);
  262. }
  263. else {
  264. MessageBox(_T("Reallocation failed\n"));
  265. }
  266. if (dwRet == ERROR_SUCCESS) {
  267. pWnode = (PWNODE_ALL_DATA) Buffer;
  268. CDisplayDataDlg dlg(pWnode, this);
  269. dlg.DoModal();
  270. delete[] Buffer;
  271. // WmiCloseBlock(hDataBlock);
  272. }
  273. else {
  274. msg.Format(_T("WMIQueryAllData returned error: %d\n"), dwRet);
  275. MessageBox(msg);
  276. }
  277. }
  278. else {
  279. msg.Format(_T("Out of Memory Error. Unable to allocate buffer of size %u\n"),
  280. dwBufferSize);
  281. MessageBox(msg);
  282. }
  283. WmiCloseBlock( hDataBlock );
  284. }
  285. void CEnumGuidDlg::OnQuerySingle()
  286. {
  287. HANDLE hDataBlock;
  288. DWORD dwRet;
  289. TCHAR Buffer[0x4000];
  290. DWORD dwBufferSize = 0x4000;
  291. LPTSTR lpInstanceName;
  292. PWNODE_SINGLE_INSTANCE pWnode;
  293. LPGUID lpGuid = GetSelectedGuid();
  294. CString tmp;
  295. CSelectInstanceName sin(lpGuid, Buffer, &dwBufferSize, this);
  296. dwRet = sin.Select();
  297. if (dwRet != ERROR_SUCCESS) {
  298. return;
  299. }
  300. lpInstanceName = new TCHAR[dwBufferSize];
  301. if (lpInstanceName == NULL) {
  302. MessageBox(_T("Out of Memory Error"));
  303. return;
  304. }
  305. else {
  306. _tcscpy(lpInstanceName, Buffer);
  307. dwBufferSize = 0x4000;
  308. }
  309. // Open the wnode
  310. //
  311. dwRet = WmiOpenBlock(lpGuid,
  312. 0,
  313. &hDataBlock);
  314. if (dwRet != ERROR_SUCCESS) {
  315. tmp.Format(_T("Unable to open data block (%u)"), dwRet);
  316. MessageBox(tmp);
  317. return;
  318. }
  319. // Query the data block
  320. //
  321. dwRet = WmiQuerySingleInstance(hDataBlock,
  322. lpInstanceName,
  323. &dwBufferSize,
  324. Buffer);
  325. if (dwRet != ERROR_SUCCESS) {
  326. tmp.Format(_T("WmiQuerySingleInstance returned error: %d"), dwRet);
  327. MessageBox(tmp);
  328. WmiCloseBlock( hDataBlock );
  329. return;
  330. }
  331. pWnode = (PWNODE_SINGLE_INSTANCE) Buffer;
  332. CDisplayDataDlg dlg(pWnode);
  333. dlg.DoModal();
  334. delete[] lpInstanceName;
  335. WmiCloseBlock(hDataBlock);
  336. return;
  337. }
  338. void CEnumGuidDlg::OnSetSingleInstance()
  339. {
  340. DWORD dwRet;
  341. DWORD dwVersionNumber;
  342. DWORD dwData[MAX_DATA];
  343. DWORD dwDataSize = MAX_DATA;
  344. UINT iLoop;
  345. LPTSTR lpInstanceName;
  346. TCHAR Buffer[1024];
  347. DWORD dwBufferSize = 1024;
  348. HANDLE hDataBlock;
  349. LPGUID lpGuid = GetSelectedGuid();
  350. CString tmp, msg;
  351. CSelectInstanceName sin(lpGuid, Buffer, &dwBufferSize, this);
  352. CSelectInstanceDataMany sid(&dwVersionNumber,
  353. &dwDataSize,
  354. dwData,
  355. MAX_DATA,
  356. this);
  357. // Get the instance to set
  358. //
  359. dwRet = sin.Select();
  360. if (dwRet != ERROR_SUCCESS) {
  361. return;
  362. }
  363. sid.DoModal();
  364. lpInstanceName = new TCHAR[dwBufferSize];
  365. _tcscpy(lpInstanceName, Buffer);
  366. // Open the wnode
  367. //
  368. dwRet = WmiOpenBlock(lpGuid,
  369. 0,
  370. &hDataBlock);
  371. if (dwRet != ERROR_SUCCESS) {
  372. msg.Format(_T("Unable to open data block (%u)"), dwRet);
  373. MessageBox(msg);
  374. delete[] lpInstanceName;
  375. return;
  376. }
  377. // Set the data
  378. //
  379. msg.Format(_T("Setting Instance: %s\nData: "), lpInstanceName);
  380. for (iLoop = 0; iLoop < dwDataSize; iLoop++) {
  381. tmp.Format(_T("0x%x "), dwData[iLoop]);
  382. msg += tmp;
  383. }
  384. msg += _T("\n\n");
  385. dwRet = WmiSetSingleInstance( hDataBlock,
  386. lpInstanceName,
  387. dwVersionNumber,
  388. dwDataSize * sizeof(DWORD),
  389. dwData);
  390. if ( dwRet != ERROR_SUCCESS) {
  391. tmp.Format(_T("WMISetSingleInstance returned error: %d"), dwRet);
  392. }
  393. else {
  394. tmp = _T("Set Success!!!");
  395. }
  396. msg += tmp;
  397. MessageBox(msg);
  398. WmiCloseBlock( hDataBlock );
  399. delete[] lpInstanceName;
  400. }
  401. void CEnumGuidDlg::OnSetSingleItem()
  402. {
  403. DWORD dwRet;
  404. DWORD dwVersionNumber;
  405. DWORD dwItemNumber;
  406. DWORD dwData;
  407. LPTSTR lpInstanceName;
  408. TCHAR Buffer[1024];
  409. DWORD dwBufferSize = 1024;
  410. HANDLE hDataBlock;
  411. LPGUID lpGuid = GetSelectedGuid();
  412. CSelectInstanceName sin(lpGuid, Buffer, &dwBufferSize, this);
  413. CSelectInstanceData sid(&dwData, &dwVersionNumber, &dwItemNumber, this);
  414. CString msg;
  415. dwRet = sin.Select();
  416. if (dwRet != ERROR_SUCCESS) {
  417. return;
  418. }
  419. lpInstanceName = new TCHAR[dwBufferSize];
  420. _tcscpy( lpInstanceName, Buffer );
  421. dwBufferSize = 4096;
  422. sid.DoModal();
  423. // Open the wnode
  424. //
  425. dwRet = WmiOpenBlock(lpGuid, 0, &hDataBlock);
  426. if (dwRet != ERROR_SUCCESS) {
  427. msg.Format(_T("Unable to open data block (%u)"), dwRet);
  428. MessageBox(msg);
  429. delete[] lpInstanceName;
  430. return;
  431. }
  432. // Set the data
  433. //
  434. msg.Format(_T("Setting Instance: %s\nData: 0x%x\n\n"),
  435. lpInstanceName, dwData);
  436. dwRet = WmiSetSingleItem(hDataBlock,
  437. lpInstanceName,
  438. dwItemNumber,
  439. dwVersionNumber,
  440. sizeof(DWORD),
  441. &dwData);
  442. if (dwRet != ERROR_SUCCESS) {
  443. CString tmp;
  444. tmp.Format(_T("WMISetSingleInstance returned error: %d"), dwRet);
  445. msg += tmp;
  446. }
  447. else {
  448. msg += ("Set Success!!!");
  449. }
  450. MessageBox(msg);
  451. WmiCloseBlock( hDataBlock );
  452. delete[] lpInstanceName;
  453. }
  454. void CEnumGuidDlg::OnReenumerateGuids()
  455. {
  456. guidList.ResetContent();
  457. if (EnumerateGuids()) {
  458. ListGuids();
  459. // make sure there is a selection, makes for a saner life later on
  460. guidList.SetCurSel(0);
  461. }
  462. }
  463. LPGUID CEnumGuidDlg::GetSelectedGuid()
  464. {
  465. return guids + guidList.GetCurSel();
  466. }