procedure TPipeForm.EditorKeyPress(Sender: TObject; var Key: Char); var ECharPos: TPoint; begin if Key = Chr(VK_RETURN) then begin // ShowMessage(GetCmdStr); SendCmdToShell(GetCmdStr); end else if Key = Chr(VK_BACK) then begin ECharPos := Editor.CaretPos; if ECharPos.X = WPos.X + 1 then Key := #0; end; end;
procedure TPipeForm.SendCmdToShell(Const CmdStr: String); var ShellCmdStr: array[0..256] of char; RBuffer: array[0..25000] of char; nByteToWrite: DWORD; nByteWritten: DWORD; nByteReaden: DWORD; begin if CreateOK then begin StrPCopy(ShellCmdStr, CmdStr); nByteToWrite := StrLen(ShellCmdStr); ShellCmdStr[nByteToWrite] := #13; ShellCmdStr[nByteToWrite+1] := #10; ShellCmdStr[nByteToWrite+2] := #0; Inc(nByteToWrite, 2); WriteFile(hWriteFile, ShellCmdStr, nByteToWrite, nByteWritten, nil); Sleep(400); Editor.Lines.Clear; FillChar(RBuffer, Sizeof(RBuffer), #0); ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil); Editor.Lines.Add(StrPas(RBuffer)); WPos.Y := Editor.Lines.Count-1; WPos.X := Length(Editor.Lines[WPos.Y])-1; end; end;
procedure TPipeForm.FormDestroy(Sender: TObject); var shellexitcode: Cardinal; begin if GetExitCodeProcess(processinfo.hProcess, shellexitcode) then begin if shellexitcode = STILL_ACTIVE then TerminateProcess(processinfo.hProcess, 0); end; if hWriteFile <> 0 then CloseHandle(hWriteFile); if hReadFile <> 0 then CloseHandle(hReadFile); end;
procedure TPipeForm.FormCreate(Sender: TObject); var Pipeattr: SECURITY_ATTRIBUTES; ShellStartInfo: STARTUPINFO; shellstr: array [0..256] of char; RBuffer: array[0..25000] of char; I: Integer; nByteReaden: DWORD; begin CreateOK := False; I := 0; Editor.ReadOnly := False; Wpos.X := 0; WPos.Y := 0; with Pipeattr do begin nLength := Sizeof(SECURITY_ATTRIBUTES); lpSecurityDescriptor := nil; bInheritHandle := true; end;
if CreatePipe(hReadPipe, hWriteFile, @Pipeattr, 0) then Inc(i); if CreatePipe(hReadFile, hWritePipe, @pipeattr, 0) then Inc(i);
GetStartupInfo(ShellStartInfo); with ShellStartInfo do begin dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; hStdInput := hReadPipe; hStdError := hWritePipe; hStdOutput := hWritePipe; wShowWindow := SW_HIDE; end; GetSystemDirectory(@Shellstr, MAX_PATH+1); StrCat(@ShellStr, Pchar('\\cmd.exe')); if CreateProcess(Shellstr, nil, nil, nil, True, 0, nil, nil, ShellStartInfo, processinfo) then begin Inc(i); end else begin MessageBox(Handle, Pchar('调用Shell错误!'), Pchar('错误'), (MB_OK or MB_ICONERROR)); end; if i = 3 then begin CreateOK := True; Editor.Lines.Clear; sleep(250); ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil); Editor.Lines.Add(StrPas(RBuffer)); WPos.Y := Editor.Lines.Count-1; WPos.X := Length(Editor.Lines[WPos.Y])-1; end; end;
procedure TPipeForm.EditorKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); var ECharPos: TPoint; begin ECharPos := Editor.CaretPos; if ECharPos.Y > WPos.Y then Editor.ReadOnly := False else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then begin Editor.ReadOnly := False; end else Editor.ReadOnly := True; end;
function TPipeForm.GetCmdStr: string; var LastLine: Integer; begin LastLine := Editor.Lines.Count - 1; if LastLine > WPos.Y then begin result := Editor.Lines[LastLine]; end else if LastLine = WPos.Y then begin result := Editor.Lines[LastLine]; result := Copy(result, WPos.X+2, Length(result)); end else begin result := ' '; end; end;
procedure TPipeForm.EditorMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var ECharPos: TPoint; begin ECharPos := Editor.CaretPos; if ECharPos.Y > WPos.Y then Editor.ReadOnly := False else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then Editor.ReadOnly := False else Editor.ReadOnly := True; end;
end.
Function Loacalip:string;//本地ip地址 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 result:=''; wsastartup($101,ginitdata); try gethostname(buffer,sizeof(buffer)); phe:=gethostbyname(buffer); if phe<>nil then begin pptr:=papinaddr(phe^.h_addr_list); i:=0; while pptr^[i] <> nil do begin result:=strpas(inet_ntoa(pptr^[i]^)); inc(i); end; end; finally wsacleanup; end; end;
Function Getcomputername():string;//電腦名稱 var buffer:array[1..32] of char; ws_data:Twsadata; begin if Wsastartup(2,ws_data)<>0 then getcomputername:='error'; if gethostname(@buffer[1],32)=0 then getcomputername:=buffer; end;
比较的简单1、获取本机名称function LocalIP : 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); Result := ''; GetHostName(Buffer, SizeOf(Buffer)); phe :=GetHostByName(buffer); if phe = nil then Exit; pptr := PaPInAddr(Phe^.h_addr_list); I := 0; while pptr^[I] <> nil do begin result:=StrPas(inet_ntoa(pptr^[I]^)); Inc(I); end; WSACleanup; end; 2、根据本地名称获取本机IP function GetWIPByHosName(HosName: Pchar): 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); Result := ''; phe :=GetHostByName(HosName); if phe = nil then Exit; pptr := PaPInAddr(Phe^.h_addr_list); I := 0; while pptr^[I] <> nil do begin result:=StrPas(inet_ntoa(pptr^[I]^)); Inc(I); end; WSACleanup;end;如果用D7就不用这么麻烦的IdIPWatch控件可以完成 IdIPWatch1.LocalIP3、获取网络中的机器信息 ◇[DELPHI]获取网上邻居 procedure getnethood();//NT做服务器,WIN98上调试通过。 var a,i:integer; errcode:integer; netres:array[0..1023] of netresource; enumhandle:thandle; enumentries:dword; buffersize:dword; s:string; mylistitems:tlistitems; mylistitem:tlistitem; alldomain:tstrings; begin //listcomputer is a listview to list all computers;controlcenter is a form. alldomain:=tstringlist.Create ; with netres[0] do begin dwscope :=RESOURCE_GLOBALNET; dwtype :=RESOURCETYPE_ANY; dwdisplaytype :=RESOURCEDISPLAYTYPE_DOMAIN; dwusage :=RESOURCEUSAGE_CONTAINER; lplocalname :=nil; lpremotename :=nil; lpcomment :=nil; lpprovider :=nil; end; // 获取所有的域 errcode:=wnetopenenum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@netres[0],enumhandle); if errcode=NO_ERROR then begin enumentries:=1024; buffersize:=sizeof(netres); errcode:=wnetenumresource(enumhandle,enumentries,@netres[0],buffersize); end; a:=0; mylistitems :=controlcenter.lstcomputer.Items ; mylistitems.Clear ; while (string(netres[a].lpprovider)<>'') and (errcode=NO_ERROR) do begin alldomain.Add (netres[a].lpremotename); a:=a+1; end; wnetcloseenum(enumhandle); // 获取所有的计算机 mylistitems :=controlcenter.lstcomputer.Items ; mylistitems.Clear ; for i:=0 to alldomain.Count-1 do begin with netres[0] do begin dwscope :=RESOURCE_GLOBALNET; dwtype :=RESOURCETYPE_ANY; dwdisplaytype :=RESOURCEDISPLAYTYPE_SERVER; dwusage :=RESOURCEUSAGE_CONTAINER; lplocalname :=nil; lpremotename :=pchar(alldomain[i]); lpcomment :=nil; lpprovider :=nil; end; ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@netres[0],EnumHandle); if errcode=NO_ERROR then begin EnumEntries:=1024; BufferSize:=SizeOf(NetRes); ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[0],BufferSize); end; a:=0; while (string(netres[a].lpprovider)<>'') and (errcode=NO_ERROR) do begin mylistitem :=mylistitems.Add ; mylistitem.ImageIndex :=0; mylistitem.Caption :=uppercase(stringreplace(string(NetRes[a].lpremotename),'\\','',[rfReplaceAll])); a:=a+1; end; wnetcloseenum(enumhandle); end; end;
var a: array[0..MAX_COMPUTERNAME_LENGTH+1] of Char; l: Cardinal; ComputerName: PChar; size: DWord; begin getmem(computername,255); size:=255; if getcomputername(computername,size)=false then begin freemem(computername); exit; end; g_user_machine_name:=computername; freemem(computername); //主机名称 l := MAX_COMPUTERNAME_LENGTH; GetComputerName(a, l); //本机IP g_user_ip := GetIPAddress(a); end; 就知道这么多
2.得到网络适配器的MAC地址 //uses Windows, SysUtils, NB30, 以下为类型定义及函数代码 const sNetBiosError = "NetBIOS错误%d"; type TMACAddress = packed array[0..5] of Byte; ENetBiosError = class( Exception ); TAStat = record Adapt : TAdapterStatus; NameBuff : array[0..30] of TNameBuffer; end; function GetMacAddress( AdapterNum : Integer ) : TMACAddress; var Ncb : TNCB; uRetCode : Char; J : Integer; Adapter : TAStat; begin FillChar( NCB, SizeOf(NCB), 0 ); with NCB do begin ncb_command := Char(NCBRESET); ncb_lana_num := Char( AdapterNum ); end; uRetCode := Netbios( @Ncb ); if uRetCode <> #0 then raise Exception.CreateFmt( sNetBIOSError, [Ord(uRetCode)] ); FillChar( NCB, SizeOf(NCB), 0 ); with NCB do begin ncb_command := Char(NCBASTAT); ncb_lana_num := Char( AdapterNum ); StrCopy( ncb_callname, "* " ); ncb_buffer := @Adapter; ncb_length := sizeof(Adapter); end; uRetCode := Netbios( @Ncb ); if uRetCode <> #0 then raise Exception.CreateFmt( sNetBIOSError, [Ord(uRetCode)] ); for J := 0 to 5 do Result[J] := Ord( Adapter.Adapt.Adapter_address[J] ); end;
3.得到当前网络用户的登录名 function GetNetUser : Ansistring; var dwI : Dword; begin dwI := MAX_PATH; SetLength (Result, dwI + 1); if WNetGetUser (Nil, Pchar (Result), dwI) = NO_ERROR then SetLength (Result, StrLen (Pchar (Result))) else SetLength (Result, 0) end; 4.获取本地IP地址 uses WinSock, SysUtils; …… type ESockUtilErr = Class(Exception); ………… function GetLocalIpAddress : string; type pu_long = ^u_long; var varTWSAData : TWSAData; varPHostEnt : PHostEnt; varTInAddr : TInAddr; namebuf : Array[0..255] of char; fd : integer; rc : integer; begin result := ""; rc := WSAStartup(1,varTWSAData); if rc <> 0 then raise ESockUtilErr.CreateFmt( "WSA启动错误: %d",[rc] ) else begin gethostname(namebuf,sizeof(namebuf)); varPHostEnt := gethostbyname(namebuf); varTInAddr.S_addr := u_long(pu_long(varPHostEnt^.h_addr_list^)^); result := inet_ntoa(varTInAddr); end; fd := WinSock.Socket( PF_INET, SOCK_STREAM, 0 ); if fd = INVALID_SOCKET then raise ESockUtilErr.CreateFmt( "%d %d: 无效 socket",[fd,WSAGetLastError] ); WSACleanup; end;
回复人: aiirii(ari-爱的眼睛) ( ) 信誉:372 2004-10-15 10:47:49 得分: 190
這個可以實現你要的, 看下:
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=199752管道的例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TPipeForm = class(TForm)
Editor: TMemo;
procedure EditorKeyPress(Sender: TObject; var Key: Char);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure EditorKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure EditorMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
CreateOk: Boolean;
WPos: TPoint;
hReadPipe, hWritePipe, hWriteFile, hReadFile: THandle;
processinfo: PROCESS_INFORMATION;
procedure SendCmdToShell(Const CmdStr: String);
function GetCmdStr: string;
public
{ Public declarations }
end;
var
PipeForm: TPipeForm;
implementation
{$R *.dfm}
procedure TPipeForm.EditorKeyPress(Sender: TObject; var Key: Char);
var
ECharPos: TPoint;
begin
if Key = Chr(VK_RETURN) then
begin
// ShowMessage(GetCmdStr);
SendCmdToShell(GetCmdStr);
end else if Key = Chr(VK_BACK) then
begin
ECharPos := Editor.CaretPos;
if ECharPos.X = WPos.X + 1 then
Key := #0;
end;
end;
procedure TPipeForm.SendCmdToShell(Const CmdStr: String);
var
ShellCmdStr: array[0..256] of char;
RBuffer: array[0..25000] of char;
nByteToWrite: DWORD;
nByteWritten: DWORD;
nByteReaden: DWORD;
begin
if CreateOK then
begin
StrPCopy(ShellCmdStr, CmdStr);
nByteToWrite := StrLen(ShellCmdStr);
ShellCmdStr[nByteToWrite] := #13;
ShellCmdStr[nByteToWrite+1] := #10;
ShellCmdStr[nByteToWrite+2] := #0;
Inc(nByteToWrite, 2);
WriteFile(hWriteFile, ShellCmdStr, nByteToWrite, nByteWritten, nil);
Sleep(400);
Editor.Lines.Clear;
FillChar(RBuffer, Sizeof(RBuffer), #0);
ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);
Editor.Lines.Add(StrPas(RBuffer));
WPos.Y := Editor.Lines.Count-1;
WPos.X := Length(Editor.Lines[WPos.Y])-1;
end;
end;
procedure TPipeForm.FormDestroy(Sender: TObject);
var
shellexitcode: Cardinal;
begin
if GetExitCodeProcess(processinfo.hProcess, shellexitcode) then
begin
if shellexitcode = STILL_ACTIVE then
TerminateProcess(processinfo.hProcess, 0);
end;
if hWriteFile <> 0 then
CloseHandle(hWriteFile);
if hReadFile <> 0 then
CloseHandle(hReadFile);
end;
procedure TPipeForm.FormCreate(Sender: TObject);
var
Pipeattr: SECURITY_ATTRIBUTES;
ShellStartInfo: STARTUPINFO;
shellstr: array [0..256] of char;
RBuffer: array[0..25000] of char;
I: Integer;
nByteReaden: DWORD;
begin
CreateOK := False;
I := 0;
Editor.ReadOnly := False;
Wpos.X := 0;
WPos.Y := 0;
with Pipeattr do
begin
nLength := Sizeof(SECURITY_ATTRIBUTES);
lpSecurityDescriptor := nil;
bInheritHandle := true;
end;
if CreatePipe(hReadPipe, hWriteFile, @Pipeattr, 0) then
Inc(i);
if CreatePipe(hReadFile, hWritePipe, @pipeattr, 0) then
Inc(i);
GetStartupInfo(ShellStartInfo);
with ShellStartInfo do
begin
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
hStdInput := hReadPipe;
hStdError := hWritePipe;
hStdOutput := hWritePipe;
wShowWindow := SW_HIDE;
end;
GetSystemDirectory(@Shellstr, MAX_PATH+1);
StrCat(@ShellStr, Pchar('\\cmd.exe'));
if CreateProcess(Shellstr, nil, nil, nil, True, 0,
nil, nil, ShellStartInfo, processinfo) then
begin
Inc(i);
end else begin
MessageBox(Handle, Pchar('调用Shell错误!'), Pchar('错误'), (MB_OK or MB_ICONERROR));
end;
if i = 3 then
begin
CreateOK := True;
Editor.Lines.Clear;
sleep(250);
ReadFile(hReadFile, RBuffer, 25000, nByteReaden, nil);
Editor.Lines.Add(StrPas(RBuffer));
WPos.Y := Editor.Lines.Count-1;
WPos.X := Length(Editor.Lines[WPos.Y])-1;
end;
end;
procedure TPipeForm.EditorKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
ECharPos: TPoint;
begin
ECharPos := Editor.CaretPos;
if ECharPos.Y > WPos.Y then
Editor.ReadOnly := False
else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then
begin
Editor.ReadOnly := False;
end else
Editor.ReadOnly := True;
end;
function TPipeForm.GetCmdStr: string;
var
LastLine: Integer;
begin
LastLine := Editor.Lines.Count - 1;
if LastLine > WPos.Y then
begin
result := Editor.Lines[LastLine];
end else if LastLine = WPos.Y then
begin
result := Editor.Lines[LastLine];
result := Copy(result, WPos.X+2, Length(result));
end else
begin
result := ' ';
end;
end;
procedure TPipeForm.EditorMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ECharPos: TPoint;
begin
ECharPos := Editor.CaretPos;
if ECharPos.Y > WPos.Y then
Editor.ReadOnly := False
else if (ECharPos.Y = WPos.Y) and (ECharPos.X > WPos.X) then
Editor.ReadOnly := False
else
Editor.ReadOnly := True;
end;
end.
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
result:='';
wsastartup($101,ginitdata);
try
gethostname(buffer,sizeof(buffer));
phe:=gethostbyname(buffer);
if phe<>nil then
begin
pptr:=papinaddr(phe^.h_addr_list);
i:=0;
while pptr^[i] <> nil do
begin
result:=strpas(inet_ntoa(pptr^[i]^));
inc(i);
end;
end;
finally
wsacleanup;
end;
end;
var buffer:array[1..32] of char;
ws_data:Twsadata;
begin
if Wsastartup(2,ws_data)<>0 then
getcomputername:='error';
if gethostname(@buffer[1],32)=0 then
getcomputername:=buffer;
end;
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);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
2、根据本地名称获取本机IP
function GetWIPByHosName(HosName: Pchar): 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);
Result := ''; phe :=GetHostByName(HosName);
if phe = nil then Exit; pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;end;如果用D7就不用这么麻烦的IdIPWatch控件可以完成
IdIPWatch1.LocalIP3、获取网络中的机器信息
◇[DELPHI]获取网上邻居
procedure getnethood();//NT做服务器,WIN98上调试通过。
var
a,i:integer;
errcode:integer;
netres:array[0..1023] of netresource;
enumhandle:thandle;
enumentries:dword;
buffersize:dword;
s:string;
mylistitems:tlistitems;
mylistitem:tlistitem;
alldomain:tstrings;
begin //listcomputer is a listview to list all computers;controlcenter is a form.
alldomain:=tstringlist.Create ;
with netres[0] do begin
dwscope :=RESOURCE_GLOBALNET;
dwtype :=RESOURCETYPE_ANY;
dwdisplaytype :=RESOURCEDISPLAYTYPE_DOMAIN;
dwusage :=RESOURCEUSAGE_CONTAINER;
lplocalname :=nil;
lpremotename :=nil;
lpcomment :=nil;
lpprovider :=nil;
end; // 获取所有的域
errcode:=wnetopenenum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@netres[0],enumhandle);
if errcode=NO_ERROR then begin
enumentries:=1024;
buffersize:=sizeof(netres);
errcode:=wnetenumresource(enumhandle,enumentries,@netres[0],buffersize);
end;
a:=0;
mylistitems :=controlcenter.lstcomputer.Items ;
mylistitems.Clear ;
while (string(netres[a].lpprovider)<>'') and (errcode=NO_ERROR) do
begin
alldomain.Add (netres[a].lpremotename);
a:=a+1;
end;
wnetcloseenum(enumhandle);
// 获取所有的计算机
mylistitems :=controlcenter.lstcomputer.Items ;
mylistitems.Clear ;
for i:=0 to alldomain.Count-1 do
begin
with netres[0] do begin
dwscope :=RESOURCE_GLOBALNET;
dwtype :=RESOURCETYPE_ANY;
dwdisplaytype :=RESOURCEDISPLAYTYPE_SERVER;
dwusage :=RESOURCEUSAGE_CONTAINER;
lplocalname :=nil;
lpremotename :=pchar(alldomain[i]);
lpcomment :=nil;
lpprovider :=nil;
end;
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@netres[0],EnumHandle);
if errcode=NO_ERROR then
begin
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[0],BufferSize);
end;
a:=0;
while (string(netres[a].lpprovider)<>'') and (errcode=NO_ERROR) do
begin
mylistitem :=mylistitems.Add ;
mylistitem.ImageIndex :=0;
mylistitem.Caption :=uppercase(stringreplace(string(NetRes[a].lpremotename),'\\','',[rfReplaceAll]));
a:=a+1;
end;
wnetcloseenum(enumhandle);
end;
end;
a: array[0..MAX_COMPUTERNAME_LENGTH+1] of Char;
l: Cardinal;
ComputerName: PChar;
size: DWord;
begin
getmem(computername,255);
size:=255;
if getcomputername(computername,size)=false then
begin
freemem(computername);
exit;
end;
g_user_machine_name:=computername;
freemem(computername);
//主机名称
l := MAX_COMPUTERNAME_LENGTH;
GetComputerName(a, l);
//本机IP
g_user_ip := GetIPAddress(a);
end;
就知道这么多
uRetCode := Netbios( @Ncb ); if uRetCode <> #0 then raise Exception.CreateFmt( sNetBIOSError, [Ord(uRetCode)] ); FillChar( NCB, SizeOf(NCB), 0 ); with NCB do begin ncb_command := Char(NCBASTAT); ncb_lana_num := Char( AdapterNum ); StrCopy( ncb_callname, "* " ); ncb_buffer := @Adapter; ncb_length := sizeof(Adapter); end; uRetCode := Netbios( @Ncb ); if uRetCode <> #0 then raise Exception.CreateFmt( sNetBIOSError, [Ord(uRetCode)] ); for J := 0 to 5 do Result[J] := Ord( Adapter.Adapt.Adapter_address[J] ); end;
3.得到当前网络用户的登录名 function GetNetUser : Ansistring; var dwI : Dword; begin dwI := MAX_PATH; SetLength (Result, dwI + 1); if WNetGetUser (Nil, Pchar (Result), dwI) = NO_ERROR then SetLength (Result, StrLen (Pchar (Result))) else SetLength (Result, 0) end; 4.获取本地IP地址 uses WinSock, SysUtils; …… type ESockUtilErr = Class(Exception); ………… function GetLocalIpAddress : string; type pu_long = ^u_long; var varTWSAData : TWSAData; varPHostEnt : PHostEnt; varTInAddr : TInAddr; namebuf : Array[0..255] of char; fd : integer; rc : integer; begin result := ""; rc := WSAStartup(1,varTWSAData); if rc <> 0 then raise ESockUtilErr.CreateFmt( "WSA启动错误: %d",[rc] ) else begin gethostname(namebuf,sizeof(namebuf)); varPHostEnt := gethostbyname(namebuf); varTInAddr.S_addr := u_long(pu_long(varPHostEnt^.h_addr_list^)^); result := inet_ntoa(varTInAddr); end; fd := WinSock.Socket( PF_INET, SOCK_STREAM, 0 ); if fd = INVALID_SOCKET then raise ESockUtilErr.CreateFmt( "%d %d: 无效 socket",[fd,WSAGetLastError] ); WSACleanup; end;
ftp://delphi-jedi.org/api/IpHlpApi.zip,有demo和源代码