在Delphi中ADSI创建Windows用户帐号

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 noil0125 的 blog

 

        在delphi中可以使用微软的adsi(活动目录服务接口)创建windows用户帐号。可能是由于adsi的名字,许多人以为adsi只能在windows 2000下使用,实际上所有win32平台都支持adsi。可能需要下载adsi为不同的windows版本。(更多详细资料http://www.microsoft.com/china/windows2000/library/howitworks/activedirectory/adsilinks.htm)。windows 2000 包含adsi。

        adsi是一个很大的话题。在这篇文章中我只粗略的介绍一下。adsi是一类为许多不同计算机服务(大部分基于目录)的通用接口。一些标准的adsi提供者(provider就像可以在你的程序中使用的com接口)有winnt,iis,ldap和nds。使用winnt提供者(provider)能创建、修改用户帐号,设置或修改组。

        下面的小程序展示了在nt/2000下使用adsi创建用户帐号必要的步骤:

        首先你需要导入adsi类库(project/import type library菜单)。该类库可以在system32子目录中找到(如 c:\winnt\system32\activeds.tlb)。文件名必须是“ activeds.tlb”。如果找不到该文件,你需要正确的安装adsi。在成功导入类库以后,可以在delphi的imports目录下找到activeds_tlb.pas这样一个文件(如 ..\delphi6\imports\activeds_tlb.pas)。还需要这个文件包含在你的uses语句,让adsi可以在delphi程序中使用。

    下面是adsi创建用户的例子。需要用你将要创建用户的真正的计算机名称替换[computername]。同样[accountname]是要创建的用户帐号。在windownt 4.0和windows 2000中测试通过。

...

uses activex,        // 使用了com moniker实例
     activeds_tlb,   // 已创建的类库
     comobj;         // 使用了olecheck和其他com函数

implementation

procedure tform1.btncreateuserclick(sender: tobject);
var
  usr:  iadsuser;
  comp: iadscontainer;
begin
  try
    comp := getobject('winnt://[computername],computer') as
      iadscontainer;
    usr := comp.create('user', '[accountname]') as iadsuser;
    usr.setinfo;
  except
    on e: eoleexception do begin
      showmessage(e.message);
    end;
  end;
end;


procedure tform1.btnsetpasswordclick(sender: tobject);
var
  usr: iadsuser;
begin
  try
    usr := getobject('winnt://[computername]/[accountname],user')
      as iadsuser;
    usr.setpassword('thenewpassword');
  except
    on e: eoleexception do begin
      showmessage(e.message);
    end;
  end;
end;


// getobject是在usenet上找到的vb代码(getobject)。
//
// getobject能使用“adsipath”(如winnt://....或iis://localhost)
// 和现有的adsi提供者捆绑在一起

function tform1.getobject(const name: string): idispatch;
var
  moniker: imoniker;
  eaten: integer;
  bindcontext: ibindctx;
  dispatch: idispatch;
begin
  olecheck(createbindctx(0, bindcontext));
  olecheck(mkparsedisplayname(bindcontext,
                              pwidechar(widestring(name)),
                              eaten,
                              moniker));
  olecheck(moniker.bindtoobject(bindcontext, nil, idispatch,
            dispatch));

  result := dispatch;
end;

end.

        也可以使用adsi修改用户帐号的设置。下列代码能改变任何帐号的“密码永不过期”选项。

procedure tformmain.buttonneverexpiresclick(sender: tobject);
var
  usr: iadsuser;
begin
  try
    usr := getobject('winnt://[computername]/[acccoutname],user') as iadsuser; 
                                  // 检查checkbox的状态
  if checkboxpasswordneverexpires.checked then
    usr.put('userflags', usr.get('userflags') or 65536) 
  // 65536在微软adsi sdk的iads.h中被定义为uf_dont_expire_password
  else
    usr.put('userflags', usr.get('userflags') xor 65536);  
    usr.setinfo;

  except
    on e: eoleexception do begin
      showmessage(e.message);
    end;
  end;
end;

本文关键:noil0125 Delphi ADSI 活动目录服务接口 Windows用户帐号
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top