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.

131 lines
4.6 KiB

  1. //------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows Shell
  4. // Copyright (C) Microsoft Corporation, 2000
  5. //
  6. // File: regobjpkr.cpp
  7. //
  8. // Contents: The implementation of the object picker for regedit
  9. //
  10. // Classes: none
  11. //
  12. //------------------------------------------------------------------------
  13. #include <accctrl.h>
  14. #include <objsel.h>
  15. #include <TCHAR.h>
  16. extern "C" HRESULT SelectComputer(HWND hWnd, LPTSTR pszRemoteName, int cchMax);
  17. HRESULT InitObjectPicker(IDsObjectPicker *pDsObjectPicker);
  18. void GetNameFromObject(IDataObject *pdo, LPTSTR pszName, int cchMax);
  19. //------------------------------------------------------------------------------
  20. // SelectComputer
  21. //
  22. // DESCRIPTION: Invokes the Object Picker and returns computer name
  23. //
  24. // PARAMETERS: hWnd - handle to parent window
  25. // pszRemoteName[OUT] - LPTSTR
  26. //------------------------------------------------------------------------------
  27. HRESULT SelectComputer(HWND hWnd, LPTSTR pszRemoteName, int cchMax)
  28. {
  29. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  30. if (SUCCEEDED(hr))
  31. {
  32. IDsObjectPicker *pDsObjectPicker = NULL;
  33. hr = CoCreateInstance(CLSID_DsObjectPicker, NULL, CLSCTX_INPROC_SERVER,
  34. IID_IDsObjectPicker, (LPVOID*) &pDsObjectPicker);
  35. if (SUCCEEDED(hr))
  36. {
  37. hr = InitObjectPicker(pDsObjectPicker);
  38. if (SUCCEEDED(hr))
  39. {
  40. IDataObject *pdo = NULL;
  41. if (pDsObjectPicker->InvokeDialog(hWnd, &pdo) == S_OK)
  42. {
  43. GetNameFromObject(pdo, pszRemoteName, cchMax);
  44. pdo->Release();
  45. hr = S_OK;
  46. }
  47. else
  48. {
  49. hr = E_FAIL;
  50. }
  51. }
  52. pDsObjectPicker->Release();
  53. }
  54. CoUninitialize();
  55. }
  56. return hr;
  57. }
  58. //------------------------------------------------------------------------------
  59. // InitObjectPicker
  60. //
  61. // DESCRIPTION: Initializes the InitObjectPicker
  62. //
  63. // PARAMETERS: pDsObjectPicker - pointer to object picker obj.
  64. //------------------------------------------------------------------------------
  65. HRESULT InitObjectPicker(IDsObjectPicker *pDsObjectPicker)
  66. {
  67. DSOP_SCOPE_INIT_INFO aScopeInit = {0};
  68. DSOP_INIT_INFO InitInfo = {0};
  69. // Initialize the DSOP_SCOPE_INIT_INFO structure.
  70. aScopeInit.cbSize = sizeof(DSOP_SCOPE_INIT_INFO);
  71. aScopeInit.flType = DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN |
  72. DSOP_SCOPE_TYPE_WORKGROUP |
  73. DSOP_SCOPE_TYPE_EXTERNAL_DOWNLEVEL_DOMAIN |
  74. DSOP_SCOPE_TYPE_EXTERNAL_UPLEVEL_DOMAIN |
  75. DSOP_SCOPE_TYPE_GLOBAL_CATALOG |
  76. DSOP_SCOPE_TYPE_ENTERPRISE_DOMAIN |
  77. DSOP_SCOPE_TYPE_USER_ENTERED_UPLEVEL_SCOPE |
  78. DSOP_SCOPE_TYPE_USER_ENTERED_DOWNLEVEL_SCOPE;
  79. aScopeInit.FilterFlags.Uplevel.flBothModes = DSOP_FILTER_COMPUTERS;
  80. aScopeInit.FilterFlags.flDownlevel = DSOP_DOWNLEVEL_FILTER_COMPUTERS;
  81. // Initialize the DSOP_INIT_INFO structure.
  82. InitInfo.cbSize = sizeof(InitInfo);
  83. InitInfo.pwzTargetComputer = NULL; // Target is the local computer.
  84. InitInfo.cDsScopeInfos = 1;
  85. InitInfo.aDsScopeInfos = &aScopeInit;
  86. return pDsObjectPicker->Initialize(&InitInfo);
  87. }
  88. //------------------------------------------------------------------------------
  89. // GetNameFromObject
  90. //
  91. // DESCRIPTION: Revieves the name of an object
  92. //
  93. // PARAMETERS: IDataObject - data object
  94. // pszName[OUT] - LPTSTR
  95. //------------------------------------------------------------------------------
  96. void GetNameFromObject(IDataObject *pdo, LPTSTR pszName, int cchMax)
  97. {
  98. PDS_SELECTION_LIST pDsSelList = NULL;
  99. STGMEDIUM stgmedium = {TYMED_HGLOBAL, NULL, NULL};
  100. CLIPFORMAT cfDsObjectPicker = (CLIPFORMAT) RegisterClipboardFormat(CFSTR_DSOP_DS_SELECTION_LIST);
  101. FORMATETC formatetc = {cfDsObjectPicker, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  102. // Get the global memory block containing the user's selections.
  103. if (SUCCEEDED(pdo->GetData(&formatetc, &stgmedium)))
  104. {
  105. // Retrieve pointer to DS_SELECTION_LIST structure.
  106. pDsSelList = (PDS_SELECTION_LIST) GlobalLock(stgmedium.hGlobal);
  107. if (pDsSelList)
  108. {
  109. _tcsncpy(pszName, pDsSelList->aDsSelection[0].pwzName, cchMax);
  110. GlobalUnlock(stgmedium.hGlobal);
  111. }
  112. ReleaseStgMedium(&stgmedium);
  113. }
  114. }