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.

178 lines
4.4 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Revision History:
  4. --*/
  5. // SelectInstanceName.cpp : implementation file
  6. //
  7. #include "stdafx.h"
  8. #include "EnumGuid.h"
  9. #include "SelName.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #include "wmihlp.h"
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CSelectInstanceName dialog
  18. CSelectInstanceName::CSelectInstanceName(LPGUID lpGuid,
  19. PTCHAR lpInstanceName, LPDWORD lpSize, CWnd* pParent /*=NULL*/)
  20. : CDialog(CSelectInstanceName::IDD, pParent), lpGuid(lpGuid),
  21. buffer(lpInstanceName), lpSize(lpSize)
  22. {
  23. //{{AFX_DATA_INIT(CSelectInstanceName)
  24. // NOTE: the ClassWizard will add member initialization here
  25. //}}AFX_DATA_INIT
  26. }
  27. CSelectInstanceName::~CSelectInstanceName()
  28. {
  29. lpGuid = 0;
  30. buffer = 0;
  31. lpSize = 0;
  32. }
  33. void CSelectInstanceName::DoDataExchange(CDataExchange* pDX)
  34. {
  35. CDialog::DoDataExchange(pDX);
  36. //{{AFX_DATA_MAP(CSelectInstanceName)
  37. DDX_Control(pDX, IDC_INSTANCE_LIST, lstInstance);
  38. //}}AFX_DATA_MAP
  39. }
  40. BEGIN_MESSAGE_MAP(CSelectInstanceName, CDialog)
  41. //{{AFX_MSG_MAP(CSelectInstanceName)
  42. ON_LBN_DBLCLK(IDC_INSTANCE_LIST, OnDblclkInstanceList)
  43. //}}AFX_MSG_MAP
  44. END_MESSAGE_MAP()
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CSelectInstanceName message handlers
  47. BOOL CSelectInstanceName::OnInitDialog()
  48. {
  49. CDialog::OnInitDialog();
  50. EnumerateInstances();
  51. return TRUE; // return TRUE unless you set the focus to a control
  52. // EXCEPTION: OCX Property Pages should return FALSE
  53. }
  54. DWORD CSelectInstanceName::Select()
  55. {
  56. DoModal();
  57. return dwError;
  58. }
  59. void CSelectInstanceName::EnumerateInstances()
  60. {
  61. HANDLE hDataBlock;
  62. BYTE *BufferPtr;
  63. DWORD dwBufferSize = 0x8000;
  64. UINT iLoop;
  65. CString tmp;
  66. UINT iCount = 0;
  67. // Get the entire Wnode
  68. //
  69. dwError = WmiOpenBlock(lpGuid, 0, &hDataBlock);
  70. if (dwError == ERROR_SUCCESS) {
  71. dwError = WmiQueryAllData(hDataBlock,
  72. &dwBufferSize,
  73. XyzBuffer);
  74. if (dwError == ERROR_SUCCESS) {
  75. WmiCloseBlock(hDataBlock);
  76. }
  77. else {
  78. tmp.Format(_T("WMIQueryAllData returned error: %d"), dwError);
  79. lstInstance.AddString(tmp);
  80. }
  81. }
  82. else {
  83. tmp.Format(_T("Unable to open data block (%u)"), dwError);
  84. lstInstance.AddString(tmp);
  85. }
  86. if (dwError != ERROR_SUCCESS) {
  87. return;
  88. }
  89. BufferPtr = XyzBuffer;
  90. while (BufferPtr != NULL)
  91. {
  92. // Print Each Instance
  93. //
  94. pWnode = (PWNODE_ALL_DATA) BufferPtr;
  95. nameOffsets = (LPDWORD) OffsetToPtr(pWnode,
  96. pWnode->OffsetInstanceNameOffsets);
  97. for (iLoop = 0; iLoop < pWnode->InstanceCount; iLoop++) {
  98. tmp.Format(_T("Instance %u: "), iCount);
  99. PrintCountedString( (LPTSTR) OffsetToPtr( pWnode, nameOffsets[iLoop]), tmp );
  100. namePtr[iCount++] = (LPTSTR)OffsetToPtr( pWnode, nameOffsets[iLoop]);
  101. lstInstance.AddString(tmp);
  102. }
  103. if (pWnode->WnodeHeader.Linkage == 0)
  104. {
  105. BufferPtr = NULL;
  106. } else {
  107. BufferPtr += pWnode->WnodeHeader.Linkage;
  108. }
  109. }
  110. lstInstance.SetCurSel(0);
  111. }
  112. void CSelectInstanceName::OnOK()
  113. {
  114. USHORT usNameSize;
  115. LPTSTR lpStringLocal;
  116. if (dwError == ERROR_SUCCESS) {
  117. // Check the size of the input buffer
  118. //
  119. lpStringLocal = (LPTSTR) namePtr[lstInstance.GetCurSel()];
  120. usNameSize = * ((USHORT *) lpStringLocal);
  121. lpStringLocal = (LPTSTR) ((PBYTE)lpStringLocal + sizeof(USHORT));
  122. if (*lpSize < (usNameSize + sizeof(TCHAR))) {
  123. *lpSize = usNameSize + sizeof(TCHAR);
  124. dwError = ERROR_INSUFFICIENT_BUFFER;
  125. }
  126. // Copy the instance name over to the output parameter.
  127. // Also, a null character needs to be added to the end of
  128. // the string because the counted string may not contain a
  129. // NULL character.
  130. //
  131. if (MyIsTextUnicode(lpStringLocal)) {
  132. usNameSize /= 2;
  133. }
  134. _tcsncpy(buffer, lpStringLocal, usNameSize );
  135. buffer += usNameSize;
  136. _tcscpy(buffer, __T(""));
  137. }
  138. CDialog::OnOK();
  139. }
  140. void CSelectInstanceName::OnDblclkInstanceList()
  141. {
  142. OnOK();
  143. }