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.

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