//断开网络连接
function NetCloseAll:boolean;
const
NETBUFF_SIZE=$208;
type
NET_API_STATUS=DWORD;
LPByte=PByte;
var
dwNetRet
WORD;
i :integer;
dwEntries
WORD;
dwTotalEntries
WORD;
szClient :LPWSTR;
dwUserName
WORD;
Buff :array[0..NETBUFF_SIZE-1]of byte;
Adword :array[0..NETBUFF_SIZE div 4-1] of dword;
NetSessionEnum:function ( ServerName:LPSTR;
Reserved
WORD;
Buf:LPByte;
BufLen
WORD;
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;