如何得到本地计算机名称以及所属的工作组(5分)

  • 主题发起人 主题发起人 边城浪子
  • 开始时间 开始时间

边城浪子

Unregistered / Unconfirmed
GUEST, unregistred user!
如何得到本地计算机名称以及所属的工作组
 
user winsock;

//取得本地IP
function GetLocalIP:String;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;

begin
WSAStartup($101, GInitData);
try
Result:='';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^ <> nil do
begin
result:=StrPas(inet_ntoa(pptr^^));
Inc(I);
end;
finally
WSACleanup;
end;
end;


//取得计算机名

function GetComputerName:string;
var
pComputerName:PChar;
ComputerNameLen:DWORD;
begin
ComputerNameLen:=255;
GetMem(pComputerName,ComputerNameLen);
try
if not GetComputerName(pComputerName,ComputerNameLen) then
pComputerName:='没有计算机名';
ComputerName:=String(PComputerName);
finally
FreeMem(pComputerName);
End;
result:=ComputerName;
End;

得到工作组:
function GetNetWorkgroup : string;
var
Reg : TRegistry;
begin
Reg := TRegistry.create;
Result := '(n/a)';
with Reg do
try
RootKey := HKEY_LOCAL_MACHINE;
if OpenKey('System/CurrentControlSet/Services/VxD/VNETSUP',
false) then
Result := ReadString('Workgroup');
finally
CloseKey;
free;
end;
end;
 
如何得到所属的工作组名称和当前登录的用户名称呢??
 
给个例子给你,是我copy过来的。unit Unit1;
{ 这是一个演示如何去获取目标NT计算机上用户列表的程序,
由于是为了演示原理,所以没有使用多线程,也没有考虑太高
的强壮性,希望诸位海涵。欢迎和我联系
Vader Yang
ciert@soim.net}
interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls;

type
TForm1 = class(TForm)
EdtHostName: TEdit;
Button1: TButton;
Label1: TLabel;
cpDomain: TLabel;
lbAdmin: TLabel;
Label3: TLabel;
Label2: TLabel;
ListBox1: TListBox;
Bevel1: TBevel;
StatusBar1: TStatusBar;
Panel1: TPanel;
Button2: TButton;
Button3: TButton;
Button4: TButton;
SaveDialog1: TSaveDialog;
Options: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure OptionsClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
seedline:String;
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

var
seaNetResource:NETRESOURCE;
seaResult:DWORD;
seaAccessBuffer:string[255];
seaAccessBufferLength:DWORD;
sidbuffer:string[255];
lpAccessBuffer:PChar;
seaSID:PSID;
seaSIDlength:DWORD;
seaReferencedDomainName:string[255];
seaReferencedDomainNameLength:DWORD;
lpReferencedDomainName:PChar;
seaSIDnameuse:SID_NAME_USE;
tempPchar:Pchar;
SidSubCount:PUCHAR;
i,j:integer;
StoreCount:byte;
tempPDWORD:PDWORD;
tempDWORD:DWORD;
storeSIDsub: array [0..8] of Integer;
seaSidIdentify:PSIDIdentifierAuthority;
newSID:PSID;
seaAccountBuffer:String[255];
pAccountName:LPTSTR;
AccountLength:DWORD;
BlResult:Boolean;
const
AccountType:array [0..8] of string=('','User','Group','Domain','alias',
'WelknowGroup','Deleted','Invalid','Unknown');
begin
Button1.Enabled:=False;
lpAccessBuffer:=@seaAccessBuffer;
lpReferencedDomainName:=@seaReferencedDomainName;
seaSID:=@sidbuffer;
seaAccessBufferLength:=64;
seaSIDLength:=255;
seaReferencedDomainNameLength:=255;
seaNetResource.dwScope:=RESOURCE_GLOBALNET;
seaNetResource.dwType:=RESOURCETYPE_ANY;
seaNetResource.lpLocalName:=PChar('');
seaNetResource.lpRemoteName:=PChar('//'+EdtHostName.Text+'/IPC$');
seaNetResource.lpProvider:=PChar('');
seaResult:=WNetAddConnection2(seaNetResource,PChar(''),PChar(''),CONNECT_PROMPT);
if seaResult=NO_ERROR then
begin
if LookupAccountName(PChar('//'+EdtHostName.Text),Pchar(seedline),seaSID,seaSIDlength,
lpReferencedDomainName,seaReferencedDomainNamelength,seaSIDnameuse)
then
begin
cpDomain.Caption:=string(lpReferencedDomainName);
seaSidIdentify:=GetSidIdentifierAuthority(seaSID);
SidSubCount:=GetSidSubAuthorityCount(seaSID);
StoreCount:=SidSubCount^;

for i:=0 to Integer(StoreCount)-1 do
begin
tempPDWORD:=GetSidSubAuthority(seaSID,i);
storeSIDsub:=tempPDWORD^;
end;

//start to get username
pAccountName:=@seaAccountBuffer ;
seaReferencedDomainNameLength:=255;
AccountLength:=255;
storeSIDsub[StoreCount-1]:=500;
if AllocateAndInitializeSid(seaSidIdentify^,SidSubCount^,StoreSidSub[0],
StoreSidSub[1],StoreSidSub[2],StoreSidSub[3],StoreSidSub[4],
StoreSidSub[5],StoreSidSub[6],StoreSidSub[7],newSID) then
begin

if LookupAccountSid(PChar('//'+EdtHostName.Text),newsid,
pAccountName,AccountLength,lpReferencedDomainName,
seaReferencedDomainNameLength,seaSIDnameuse)
then
begin
lbAdmin.Caption:=String(pAccountName);
ListBox1.Items.Add('//'+lpReferencedDomainName+'/'+pAccountName+' Built-in Admin');
end
else
exit;
FreeSid(newSID);
//tempDWORD:=GetLastError;
j:=1;
i:=1000;
while j<=30 do
begin
seaReferencedDomainNamelength:=255;
AccountLength:=255;
StoreSidSub[StoreCount-1]:=i;
AllocateAndInitializeSid(seaSidIdentify^,SidSubCount^,StoreSidSub[0],
StoreSidSub[1],StoreSidSub[2],StoreSidSub[3],StoreSidSub[4],
StoreSidSub[5],StoreSidSub[6],StoreSidSub[7],newSID);
if LookupAccountSid(PChar('//'+EdtHostName.Text),newsid,
pAccountName,AccountLength,lpReferencedDomainName,
seaReferencedDomainNameLength,seaSIDnameuse)
then
begin
if seaSIDnameuse=sidTypeInvalid then j:=j+1
else if seaSIDnameuse<>sidTypeDeletedAccount then
begin
j:=0;
ListBox1.Items.Add('//'+lpReferencedDomainName+
'/'+pAccountName+' '+AccountType[seaSIDnameuse]);
StatusBar1.SimpleText:=pAccountName;
end;
end
else
j:=j+1;
Application.ProcessMessages;
i:=i+1;
FreeSID(newsid);
end;
end;

end
else ShowMessage('Cannot locate sid infomation!');
end
else ShowMessage('Connection Error!');
WNetCancelConnection2(PChar('//'+EdtHostName.Text+'/IPC$'),0,true);
Button1.Enabled:=True;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ListBox1.Items.Clear;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
SaveDialog1.Execute;
if SaveDialog1.FileName<>'' then ListBox1.Items.SaveToFile(SaveDialog1.FileName);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage('如果有任何意见或者咨询'#13#10'请与ciert@soim.net联系');
end;

procedure TForm1.OptionsClick(Sender: TObject);
begin
seedline:=InputBox('Input seed','Input seed(recommend "domain users" or "guest")','guest');
end;

procedure TForm1.FormShow(Sender: TObject);
begin
seedline:='guest';
end;

end.
 
后退
顶部