//判断电脑有没有上线 if GetSystemMetrics(SM_NETWORK) and $01 = $01 then ShowMessage('Computer is attached to a network!') else ShowMessage('Computer is not attached to a network!'); //关闭网络连接 function NetCloseAll:boolean; const NETBUFF_SIZE=$208; type NET_API_STATUS=DWORD; LPByte=PByte; var dwNetRet :DWORD; i :integer; dwEntries :DWORD; dwTotalEntries :DWORD; szClient :LPWSTR; dwUserName :DWORD; Buff :array[0..NETBUFF_SIZE-1]of byte; Adword :array[0..NETBUFF_SIZE div 4-1] of dword; NetSessionEnum:function ( ServerName:LPSTR; Reserved:DWORD; Buf:LPByte; BufLen:DWORD; ConnectionCount:LPDWORD; ConnectionToltalCount:LPDWORD ):NET_API_STATUS; stdcall; NetSessionDel:function ( ServerName:LPWSTR; UncClientName: LPWSTR ; UserName: dword):NET_API_STATUS; stdcall; LibHandle : THandle; begin Result:=false; try { 加载 DLL } LibHandle := LoadLibrary('svrapi.dll'); try { 如果加载失败,LibHandle = 0.} if LibHandle = 0 then raise Exception.Create('不能加载SVRAPI.DLL'); { DLL 加载成功,取得到 DLL 输出函数的连接然后调用 } @NetSessionEnum := GetProcAddress(LibHandle, 'NetSessionEnum'); @NetSessionDel := GetProcAddress(LibHandle, 'NetSessionDel'); if (@NetSessionEnum = nil)or(@NetSessionDel=nil) then RaiseLastWin32Error { 连接函数失败 } else begin dwNetRet := NetSessionEnum( nil,$32, @Buff, NETBUFF_SIZE, @dwEntries, @dwTotalEntries ); if dwNetRet = 0 then begin Result := true; for i:=0 to dwTotalEntries-1 do begin Move(Buff,Adword,NETBUFF_SIZE); szClient:=LPWSTR(Adword[0]); dwUserName := Adword[2]; dwNetRet := NetSessionDel( nil,szClient,dwUserName); if( dwNetRet <> 0 ) then begin Result := false; break; end; Move(Buff[26],Buff[0],NETBUFF_SIZE-(i+1)*26); end end else Result := false; end; finally FreeLibrary(LibHandle); // Unload the DLL. end; except end; end;
if GetSystemMetrics(SM_NETWORK) and $01 = $01 then
ShowMessage('Computer is attached to a network!')
else
ShowMessage('Computer is not attached to a network!');
//关闭网络连接
function NetCloseAll:boolean;
const
NETBUFF_SIZE=$208;
type
NET_API_STATUS=DWORD;
LPByte=PByte;
var
dwNetRet :DWORD;
i :integer;
dwEntries :DWORD;
dwTotalEntries :DWORD;
szClient :LPWSTR;
dwUserName :DWORD;
Buff :array[0..NETBUFF_SIZE-1]of byte;
Adword :array[0..NETBUFF_SIZE div 4-1] of dword;
NetSessionEnum:function ( ServerName:LPSTR;
Reserved:DWORD;
Buf:LPByte;
BufLen:DWORD;
ConnectionCount:LPDWORD;
ConnectionToltalCount:LPDWORD ):NET_API_STATUS;
stdcall; NetSessionDel:function ( ServerName:LPWSTR;
UncClientName: LPWSTR ;
UserName: dword):NET_API_STATUS;
stdcall;
LibHandle : THandle;
begin
Result:=false;
try
{ 加载 DLL }
LibHandle := LoadLibrary('svrapi.dll');
try
{ 如果加载失败,LibHandle = 0.}
if LibHandle = 0 then
raise Exception.Create('不能加载SVRAPI.DLL');
{ DLL 加载成功,取得到 DLL 输出函数的连接然后调用 }
@NetSessionEnum := GetProcAddress(LibHandle, 'NetSessionEnum');
@NetSessionDel := GetProcAddress(LibHandle, 'NetSessionDel'); if (@NetSessionEnum = nil)or(@NetSessionDel=nil) then
RaiseLastWin32Error { 连接函数失败 }
else
begin
dwNetRet := NetSessionEnum( nil,$32, @Buff,
NETBUFF_SIZE, @dwEntries,
@dwTotalEntries );
if dwNetRet = 0 then
begin
Result := true;
for i:=0 to dwTotalEntries-1 do
begin
Move(Buff,Adword,NETBUFF_SIZE);
szClient:=LPWSTR(Adword[0]);
dwUserName := Adword[2];
dwNetRet := NetSessionDel( nil,szClient,dwUserName);
if( dwNetRet <> 0 ) then
begin
Result := false;
break;
end;
Move(Buff[26],Buff[0],NETBUFF_SIZE-(i+1)*26);
end
end
else
Result := false;
end;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
except
end;
end;