Delphi:呼叫用户选择界面

时间:2015-11-16 16:27:02

标签: windows delphi

我可以使用Windows API调用此屏幕吗?

enter image description here

我想使用此界面选择合适的域用户。

谢谢!

1 个答案:

答案 0 :(得分:6)

此对话框称为对象选择器对话框。您可以使用IDsObjectPicker界面和InvokeDialog方法从Delphi调用。

这是一个如何使用Delphi中的对话框的示例。

implementation
{$R *.dfm}

uses
   ActiveX;

const
  CLSID_DsObjectPicker: TGUID = '{17D6CCD8-3B7B-11D2-B9E0-00C04FD8DBF7}';
  IID_IDsObjectPicker : TGUID = '{0c87e64e-3b7a-11d2-b9e0-00c04fd8dbf7}';

  CFSTR_DSOP_DS_SELECTION_LIST = 'CFSTR_DSOP_DS_SELECTION_LIST';

  DSOP_SCOPE_TYPE_TARGET_COMPUTER              = $00000001;
  DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN        = $00000002;
  DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN      = $00000004;
  DSOP_SCOPE_TYPE_ENTERPRISE_DOMAIN            = $00000008;
  DSOP_SCOPE_TYPE_GLOBAL_CATALOG               = $00000010;
  DSOP_SCOPE_TYPE_EXTERNAL_UPLEVEL_DOMAIN      = $00000020;
  DSOP_SCOPE_TYPE_EXTERNAL_DOWNLEVEL_DOMAIN    = $00000040;
  DSOP_SCOPE_TYPE_WORKGROUP                    = $00000080;
  DSOP_SCOPE_TYPE_USER_ENTERED_UPLEVEL_SCOPE   = $00000100;
  DSOP_SCOPE_TYPE_USER_ENTERED_DOWNLEVEL_SCOPE = $00000200;

  DSOP_FILTER_INCLUDE_ADVANCED_VIEW  = $00000001;
  DSOP_FILTER_USERS                  = $00000002;
  DSOP_FILTER_BUILTIN_GROUPS         = $00000004;
  DSOP_FILTER_WELL_KNOWN_PRINCIPALS  = $00000008;
  DSOP_FILTER_UNIVERSAL_GROUPS_DL    = $00000010;
  DSOP_FILTER_UNIVERSAL_GROUPS_SE    = $00000020;
  DSOP_FILTER_GLOBAL_GROUPS_DL       = $00000040;
  DSOP_FILTER_GLOBAL_GROUPS_SE       = $00000080;
  DSOP_FILTER_DOMAIN_LOCAL_GROUPS_DL = $00000100;
  DSOP_FILTER_DOMAIN_LOCAL_GROUPS_SE = $00000200;
  DSOP_FILTER_CONTACTS               = $00000400;
  DSOP_FILTER_COMPUTERS              = $00000800;


  DSOP_SCOPE_FLAG_STARTING_SCOPE              = $00000001;
  DSOP_SCOPE_FLAG_WANT_PROVIDER_WINNT         = $00000002;
  DSOP_SCOPE_FLAG_WANT_PROVIDER_LDAP          = $00000004;
  DSOP_SCOPE_FLAG_WANT_PROVIDER_GC            = $00000008;
  DSOP_SCOPE_FLAG_WANT_SID_PATH               = $00000010;
  DSOP_SCOPE_FLAG_WANT_DOWNLEVEL_BUILTIN_PATH = $00000020;
  DSOP_SCOPE_FLAG_DEFAULT_FILTER_USERS        = $00000040;
  DSOP_SCOPE_FLAG_DEFAULT_FILTER_GROUPS       = $00000080;
  DSOP_SCOPE_FLAG_DEFAULT_FILTER_COMPUTERS    = $00000100;
  DSOP_SCOPE_FLAG_DEFAULT_FILTER_CONTACTS     = $00000200;

  DSOP_DOWNLEVEL_FILTER_USERS                  = $80000001;
  DSOP_DOWNLEVEL_FILTER_LOCAL_GROUPS           = $80000002;
  DSOP_DOWNLEVEL_FILTER_GLOBAL_GROUPS          = $80000004;
  DSOP_DOWNLEVEL_FILTER_COMPUTERS              = $80000008;
  DSOP_DOWNLEVEL_FILTER_WORLD                  = $80000010;
  DSOP_DOWNLEVEL_FILTER_AUTHENTICATED_USER     = $80000020;
  DSOP_DOWNLEVEL_FILTER_ANONYMOUS              = $80000040;
  DSOP_DOWNLEVEL_FILTER_BATCH                  = $80000080;
  DSOP_DOWNLEVEL_FILTER_CREATOR_OWNER          = $80000100;
  DSOP_DOWNLEVEL_FILTER_CREATOR_GROUP          = $80000200;
  DSOP_DOWNLEVEL_FILTER_DIALUP                 = $80000400;
  DSOP_DOWNLEVEL_FILTER_INTERACTIVE            = $80000800;
  DSOP_DOWNLEVEL_FILTER_NETWORK                = $80001000;
  DSOP_DOWNLEVEL_FILTER_SERVICE                = $80002000;
  DSOP_DOWNLEVEL_FILTER_SYSTEM                 = $80004000;
  DSOP_DOWNLEVEL_FILTER_EXCLUDE_BUILTIN_GROUPS = $80008000;
  DSOP_DOWNLEVEL_FILTER_TERMINAL_SERVER        = $80010000;
  DSOP_DOWNLEVEL_FILTER_ALL_WELLKNOWN_SIDS     = $80020000;
  DSOP_DOWNLEVEL_FILTER_LOCAL_SERVICE          = $80040000;
  DSOP_DOWNLEVEL_FILTER_NETWORK_SERVICE        = $80080000;
  DSOP_DOWNLEVEL_FILTER_REMOTE_LOGON           = $80100000;

  DSOP_FLAG_MULTISELECT                   = $00000001;
  DSOP_FLAG_SKIP_TARGET_COMPUTER_DC_CHECK = $00000002;


type
  PDSOPUplevelFilterFlags = ^TDSOPUplevelFilterFlags;
  _DSOP_UPLEVEL_FILTER_FLAGS = record
    flBothModes     : ULONG;
    flMixedModeOnly : ULONG;
    flNativeModeOnly: ULONG;
  end;
  TDSOPUplevelFilterFlags = _DSOP_UPLEVEL_FILTER_FLAGS;
  DSOP_UPLEVEL_FILTER_FLAGS = _DSOP_UPLEVEL_FILTER_FLAGS;


  PDSOPFilterFlags = ^TDSOPFilterFlags;
  _DSOP_FILTER_FLAGS = record
    Uplevel    : TDSOPUplevelFilterFlags;
    flDownlevel: ULONG;
  end;
  TDSOPFilterFlags = _DSOP_FILTER_FLAGS;
  DSOP_FILTER_FLAGS = _DSOP_FILTER_FLAGS;


  PDSOPScopeInitInfo = ^TDSOPScopeInitInfo;
  PDSOP_SCOPE_INIT_INFO = PDSOPScopeInitInfo;
  _DSOP_SCOPE_INIT_INFO = record
    cbSize     : ULONG;
    flType     : ULONG;
    flScope    : ULONG;
    FilterFlags: TDSOPFilterFlags;
    pwzDcName  : LPCWSTR;
    pwzADsPath : LPCWSTR;
    hr         : HRESULT;
  end;
  TDSOPScopeInitInfo = _DSOP_SCOPE_INIT_INFO;
  DSOP_SCOPE_INIT_INFO = _DSOP_SCOPE_INIT_INFO;


  PDSOPInitInfo = ^TDSOPInitInfo;
  PDSOP_INIT_INFO = PDSOPInitInfo;
  _DSOP_INIT_INFO = record
    cbSize            : ULONG;
    pwzTargetComputer : LPCWSTR;
    cDsScopeInfos     : ULONG;
    aDsScopeInfos     : PDSOPScopeInitInfo;
    flOptions         : ULONG;
    cAttributesToFetch: ULONG;
    apwzAttributeNames: Pointer;
  end;
  TDSOPInitInfo = _DSOP_INIT_INFO;
  DSOP_INIT_INFO = _DSOP_INIT_INFO;

  PDSSelection = ^TDSSelection;
  PDS_SELECTION = PDSSelection;
  _DS_SELECTION = record
    pwzName              : LPCWSTR;
    pwzADsPath           : LPCWSTR;
    pwzClass             : LPCWSTR;
    pwzUPN               : LPCWSTR;
    pvarFetchedAttributes: PVariant;
    flScopeType          : ULONG;
  end;
  TDSSelection = _DS_SELECTION;
  DS_SELECTION = _DS_SELECTION;

type
  PDSSelectionList = ^TDSSelectionList;
  PDS_SELECTION_LIST = PDSSelectionList;
  _DS_SELECTION_LIST = record
    cItems            : ULONG;
    cFetchedAttributes: ULONG;
    aDsSelection      : array [0..0] of TDSSelection;
  end;
  TDSSelectionList = _DS_SELECTION_LIST;
  DS_SELECTION_LIST = _DS_SELECTION_LIST;


  IDsObjectPicker = interface(IUnknown)
    ['{17D6CCD8-3B7B-11D2-B9E0-00C04FD8DBF7}']
    function Initialize(var pInitInfo: TDSOPInitInfo): HRESULT; stdcall;
    function InvokeDialog(hwndParent: HWND; out ppdoSelections: IDataObject): HRESULT; stdcall;
  end;

function InitObjectPicker(Picker: IDsObjectPicker): HRESULT;
var
  ScopeInit: array [0..0] of TDSOPScopeInitInfo;
  InitInfo: TDSOPInitInfo;
begin
  if nil = Picker then
    Result := E_INVALIDARG
  else
  begin
    ZeroMemory(@ScopeInit, SizeOf(ScopeInit));
    ScopeInit[0].cbSize := SizeOf(TDSOPScopeInitInfo);
    ScopeInit[0].flType := DSOP_SCOPE_TYPE_TARGET_COMPUTER;
    ScopeInit[0].flScope := DSOP_SCOPE_TYPE_USER_ENTERED_DOWNLEVEL_SCOPE;
    ScopeInit[0].FilterFlags.Uplevel.flBothModes := DSOP_FILTER_USERS;
    ScopeInit[0].FilterFlags.flDownlevel := DSOP_DOWNLEVEL_FILTER_USERS;

    ZeroMemory(@InitInfo, SizeOf(InitInfo));
    InitInfo.cbSize := SizeOf(InitInfo);
    InitInfo.cDsScopeInfos := SizeOf(ScopeInit) div SizeOf(TDSOPScopeInitInfo);
    InitInfo.aDsScopeInfos := @ScopeInit;
    InitInfo.flOptions := DSOP_FLAG_SKIP_TARGET_COMPUTER_DC_CHECK;

    Result := Picker.Initialize(InitInfo);
  end;
end;

function ProcessSelectedObjects(DatObj: IDataObject): HRESULT;
var
  StgMed: TStgMedium;
  FmtEtc: TFormatEtc;
  SelLst: PDSSelectionList;
  Index: ULONG;
  Text: string;
begin
  if nil = DatObj then
    Result := E_INVALIDARG
  else
  begin
    with FmtEtc do
    begin
      cfFormat := RegisterClipboardFormat(CFSTR_DSOP_DS_SELECTION_LIST);
      ptd := nil;
      dwAspect := DVASPECT_CONTENT;
      lindex := -1;
      tymed := TYMED_HGLOBAL;
    end;
    Result := DatObj.GetData(FmtEtc, StgMed);
    if Succeeded(Result) then
    begin
      SelLst := PDS_SELECTION_LIST(GlobalLock(StgMed.hGlobal));
      if SelLst <> nil then
      try
        Text := '';
        for Index := 0 to SelLst.cItems - 1 do
        begin
          Text := Text + Format(
            'Object : %u'#13#10 +
            ' Name : %s'#13#10 +
            ' Class: %s'#13#10 +
            ' Path : %s'#13#10 +
            ' UPN : %s'#13#10, [
            Index,
            SelLst.aDsSelection[Index].pwzName,
            SelLst.aDsSelection[Index].pwzClass,
            SelLst.aDsSelection[Index].pwzADsPath,
            SelLst.aDsSelection[Index].pwzUPN]);
        end;
        ShowMessage(Text);
      finally
        GlobalUnlock(StgMed.hGlobal);
      end
      else
        Result := E_POINTER;

      ReleaseStgMedium(StgMed);
    end;
  end;
end;

//invoke the dialog
procedure TForm1.Button1Click(Sender: TObject);
var
  Picker: IDsObjectPicker;
  DatObj: IDataObject;
begin
  if Succeeded(CoInitialize(nil)) then
  try
    if Succeeded(CoCreateInstance(CLSID_DsObjectPicker, nil,
      CLSCTX_INPROC_SERVER, IID_IDsObjectPicker, Picker)) then
    try
      if Succeeded(InitObjectPicker(Picker)) then
        case Picker.InvokeDialog(Self.Handle, DatObj) of
          S_OK:
            try
              ProcessSelectedObjects(DatObj);
            finally
              DatObj := nil;
            end;
          S_FALSE:
            ShowMessage('Dialog canceled by the user.');
        end;
    finally
      Picker := nil;
    end;
  finally
    CoUninitialize;
  end;
end;


end.

enter image description here