我如何获得本机的工作组名,谢(100分)

  • 主题发起人 主题发起人 yuanjj76
  • 开始时间 开始时间
测试代码如下,你自已看一下吧 ^_^
------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ImgList;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
TreeView1: TTreeView;
Button2: TButton;
ImageList1: TImageList;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
private
{ Private declarations }
Function GetGroupList( var List : TStringList ) : Boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

//自定义列举函数
Function TForm1.GetGroupList( var List : TStringList ) : Boolean;
type
TNetResourceArray = ^TNetResource;//网络类型的数组
Var
NetResource: TNetResource;
Buf: Pointer;
Count,BufSize,Res: DWORD;
lphEnum: THandle;
p: TNetResourceArray;
i,j: SmallInt;
NetworkTypeList: TList;
Begin
Result := False;
NetworkTypeList := TList.Create;
List.Clear;
//获取整个网络中的文件资源的句柄,lphEnum为返回名柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, Nil,lphEnum);
if Res <> NO_ERROR then exit;//Raise Exception(Res);//执行失败
//获取整个网络中的网络类型信息
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
//资源列举完毕 //执行失败
if ( Res = ERROR_NO_MORE_ITEMS ) or (Res <> NO_ERROR ) then Exit;
P := TNetResourceArray(Buf);
for i := 0 to Count - 1 do//记录各个网络类型的信息
begin
NetworkTypeList.Add(p);
Inc(P);
end;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
if Res <> NO_ERROR then exit;
for j := 0 to NetworkTypeList.Count-1 do //列出各个网络类型中的所有工作组名称
begin//列出一个网络类型中的所有工作组名称
NetResource := TNetResource(NetworkTypeList.Items[J]^);//网络类型信息
//获取某个网络类型的文件资源的句柄,NetResource为网络类型信息,lphEnum为返回名柄
Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
if Res <> NO_ERROR then break;//执行失败
while true do//列举一个网络类型的所有工作组的信息
begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取一个网络类型的文件资源信息,
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
//资源列举完毕 //执行失败
if ( Res = ERROR_NO_MORE_ITEMS ) or (Res <> NO_ERROR) then break;
P := TNetResourceArray(Buf);
for i := 0 to Count - 1 do//列举各个工作组的信息
begin
List.Add( StrPAS( P^.lpRemoteName ));//取得一个工作组的名称
Inc(P);
end;
end;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
if Res <> NO_ERROR then break;//执行失败
end;
Result := True;
FreeMem(Buf);
NetworkTypeList.Destroy;
End;

//列举过程实例...
procedure TForm1.Button1Click(Sender: TObject);
var WorkGroup : TStringList;
i : integer;
begin
WorkGroup:=TStringList.Create ;
try
if GetGroupList(WorkGroup) then begin
memo1.Clear ;
TreeView1.Items.Clear ;
for i:=0 to WorkGroup.Count-1 do begin
memo1.Lines.Add(WorkGroup.Strings);
TreeView1.Items.Add(nil,WorkGroup.Strings);

end;
WorkGroup.Free ;
end;
except
WorkGroup.Free ;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var WorkGroup, HostList : TStringList;
i,j : integer;
Node,SubNode: TTreeNode;
begin
WorkGroup:=TStringList.Create ;
WorkGroup.Add('1111');
WorkGroup.Add('2222');
WorkGroup.Add('3333');
WorkGroup.Add('4444');
WorkGroup.Add('5555');
HostList:=TStringList.Create ;
HostList.Add('aaa');
HostList.Add('bbb');
HostList.Add('ccc');
HostList.Add('ddd');
HostList.Add('eee');
//---------------------------------------------------------------------------
for i:=0 to WorkGroup.Count-1 do begin
memo1.Lines.Add(WorkGroup.Strings);
Node:=TreeView1.Items.Add(nil,WorkGroup.Strings); //根结点
Node.ImageIndex :=1;
Node.SelectedIndex :=1;
for j:=0 to HostList.Count-1 do begin
SubNode:=TreeView1.Items.AddChild(Node,HostList.Strings[j]);
SubNode.ImageIndex :=2;
SubNode.SelectedIndex :=2;
end;
end;
//---------------------------------------------------------------------------
WorkGroup.Free;
HostList.Free ;
end;

procedure TForm1.TreeView1Click(Sender: TObject);
var node, chNode : Ttreenode;

begin
Node:=TreeView1.Selected ;
if Node.Index<0 then exit;

edit1.Text :='Content: '+Node.Text ;
chNode:=Node.getFirstChild ;

if chNode<>Nil then

Edit1.Text :=Edit1.Text +' Sub: '+chNode.Text ;

end;

end.
 
还有列举工作组所在的所有计算机:
-----------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;

implementation
{$R *.dfm}
// 列举工作组中所有的计算机
Function GetUsers(GroupName: string; var List: TStringList): Boolean;
type
TNetResourceArray = ^TNetResource;//网络类型的数组
Var
i: Integer;
Buf: Pointer;
Temp: TNetResourceArray;
lphEnum: THandle;
NetResource: TNetResource;
Count,BufSize,Res: DWord;
begin
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0);//初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1];//指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;//类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK;//列举文件资源信息
//获取指定工作组的网络资源句柄
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
if Res <> NO_ERROR then Exit; //执行失败

while True do //列举指定工作组的网络资源
begin
Count := $FFFFFFFF;//不限资源数目
BufSize := 8192;//缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息
//获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
if Res = ERROR_NO_MORE_ITEMS then break;//资源列举完毕
if (Res <> NO_ERROR) then Exit;//执行失败
Temp := TNetResourceArray(Buf);
for i := 0 to Count - 1 do//列举工作组的计算机名称
begin
//获取工作组的计算机名称,+2表示删除"//",如//wangfajun=>wangfajun
List.Add(Temp^.lpRemoteName + 2);
inc(Temp);
end;
FreeMem(Buf);
end;

Res := WNetCloseEnum(lphEnum);//关闭一次列举
if Res <> NO_ERROR then exit;//执行失败
Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var PC : TStringList;
i : integer;
begin
PC:=TStringList.Create ;
try
if GetUsers(Edit1.Text,PC) then begin
memo1.Clear ;
for i:=0 to PC.Count-1 do
memo1.Lines.Add(PC.Strings);
PC.Free ;
end;
except
PC.Free ;
end;

end;

end.
 
我只想获得本机的工作组的名称.
 
取工作组的确不容易啊,用了我好长时间

function IsWinNT: Boolean;
var
VersionInfo: TOSVersionInfo;
begin
VersionInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
Result := GetVersionEx(VersionInfo);
if Result then
Result := VersionInfo.dwPlatformID = VER_PLATFORM_WIN32_NT;
end;


function GetDomainName: AnsiString;
type
WKSTA_INFO_100 = record
wki100_platform_id: Integer;
wki100_computername: PWideChar;
wki100_langroup: PWideChar;
wki100_ver_major: Integer;
wki100_ver_minor: Integer;
end;

WKSTA_USER_INFO_1 = record
wkui1_username: PChar;
wkui1_logon_domain: PChar;
wkui1_logon_server: PChar;
wkui1_oth_domains: PChar;
end;
type
//Win9X ANSI prototypes from RADMIN32.DLL and RLOCAL32.DLL

TWin95_NetUserGetInfo = function(ServerName, UserName: PChar; Level: DWORD; var
BfrPtr: Pointer): Integer;
stdcall;
TWin95_NetApiBufferFree = function(BufPtr: Pointer): Integer;
stdcall;
TWin95_NetWkstaUserGetInfo = function(Reserved: PChar; Level: Integer; var
BufPtr: Pointer): Integer;
stdcall;

//WinNT UNICODE equivalents from NETAPI32.DLL

TWinNT_NetWkstaGetInfo = function(ServerName: PWideChar; level: Integer; var
BufPtr: Pointer): Integer;
stdcall;
TWinNT_NetApiBufferFree = function(BufPtr: Pointer): Integer;
stdcall;

var

Win95_NetUserGetInfo: TWin95_NetUserGetInfo;
Win95_NetWkstaUserGetInfo: TWin95_NetWkstaUserGetInfo;
Win95_NetApiBufferFree: TWin95_NetApiBufferFree;

WinNT_NetWkstaGetInfo: TWinNT_NetWkstaGetInfo;
WinNT_NetApiBufferFree: TWinNT_NetApiBufferFree;

WSNT: ^WKSTA_INFO_100;
WS95: ^WKSTA_USER_INFO_1;

EC: DWORD;
hNETAPI: THandle;
begin
try

Result := '';

if IsWinNT then
begin
hNETAPI := LoadLibrary('NETAPI32.DLL');
if hNETAPI <> 0 then
begin @WinNT_NetWkstaGetInfo := GetProcAddress(hNETAPI, 'NetWkstaGetInfo');
@WinNT_NetApiBufferFree := GetProcAddress(hNETAPI, 'NetApiBufferFree');

EC := WinNT_NetWkstaGetInfo(nil, 100, Pointer(WSNT));
if EC = 0 then
begin
Result := WideCharToString(WSNT^.wki100_langroup);
WinNT_NetApiBufferFree(Pointer(WSNT));
end;
end;
end
else
begin
hNETAPI := LoadLibrary('RADMIN32.DLL');
if hNETAPI <> 0 then
begin @Win95_NetApiBufferFree := GetProcAddress(hNETAPI, 'NetApiBufferFree');
@Win95_NetUserGetInfo := GetProcAddress(hNETAPI, 'NetUserGetInfoA');

EC := Win95_NetWkstaUserGetInfo(nil, 1, Pointer(WS95));
if EC = 0 then
begin
Result := WS95^.wkui1_logon_domain;
Win95_NetApiBufferFree(Pointer(WS95));
end;
end;
end;

finally
if hNETAPI <> 0 then
FreeLibrary(hNETAPI);
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetDomainName);
end;
 
http://haitian.myrice.com/SoftRes.htm
的公用函数有
 
接受答案了.
 
后退
顶部