...enumerate the network connections (drives)?
Author: Igor Siticov
{
From the MS-DOS prompt, you can enumerate the network
connections (drives) by using the following command:
net use
Programmatically, you would call WNetOpenEnum() to start
the enumeration of connected resources and
WNetEnumResources() to continue the enumeration.
The following sample code enumerates the network connections:
Vom MS-DOS Prompt können die Netzwerk Verbdindungen
(Laufwerke) mittels "net use" angezeigt werden.
Das folgende Beispiel braucht die WNetOpenEnum,
WNetEnumResources Funktionen, um die Netzwerk Verbdindungen aufzulisten.
}
procedure TForm1.Button1Click(Sender: TObject);
var
i, dwResult: DWORD;
hEnum: THandle;
lpnrDrv: PNETRESOURCE;
s: string;
const
cbBuffer: DWORD = 16384;
cEntries: DWORD = $FFFFFFFF;
begin
dwResult := WNetOpenEnum(RESOURCE_CONNECTED,
RESOURCETYPE_ANY,
0,
nil,
hEnum);
if (dwResult <> NO_ERROR) then
begin
ShowMessage('Cannot enumerate network drives.');
Exit;
end;
s := '';
repeat
lpnrDrv := PNETRESOURCE(GlobalAlloc(GPTR, cbBuffer));
dwResult := WNetEnumResource(hEnum, cEntries, lpnrDrv, cbBuffer);
if (dwResult = NO_ERROR) then
begin
s := 'Network drives:'#13#10;
for i := 0 to cEntries - 1 do
begin
if lpnrDrv^.lpLocalName <> nil then
s := s + lpnrDrv^.lpLocalName + #9 + lpnrDrv^.lpRemoteName;
Inc(lpnrDrv);
end;
end
else if dwResult <> ERROR_NO_MORE_ITEMS then
begin
s := s + 'Cannot complete network drive enumeration';
GlobalFree(HGLOBAL(lpnrDrv));
break;
end;
GlobalFree(HGLOBAL(lpnrDrv));
until (dwResult = ERROR_NO_MORE_ITEMS);
WNetCloseEnum(hEnum);
if s = '' then s := 'No network connections.';
ShowMessage(s);
end;
{***********************************************************************
FindComp Unit from
Fatih Olcer
fatiholcer@altavista.com
***********************************************************************}
unit FindComp;
interface
uses
Windows, Classes;
function FindComputers: DWORD;
var
Computers: TStringList;
implementation
uses
SysUtils;
const
MaxEntries = 250;
function FindComputers: DWORD;
var
EnumWorkGroupHandle, EnumComputerHandle: THandle;
EnumError: DWORD;
Network: TNetResource;
WorkGroupEntries, ComputerEntries: DWORD;
EnumWorkGroupBuffer, EnumComputerBuffer: array[1..MaxEntries] of TNetResource;
EnumBufferLength: DWORD;
I, J: DWORD;
begin
Computers.Clear;
FillChar(Network, SizeOf(Network), 0);
with Network do
begin
dwScope := RESOURCE_GLOBALNET;
dwType := RESOURCETYPE_ANY;
dwUsage := RESOURCEUSAGE_CONTAINER;
end;
EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0,
@Network, EnumWorkGroupHandle);
if EnumError = NO_ERROR then
begin
WorkGroupEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumWorkGroupBuffer);
EnumError := WNetEnumResource(EnumWorkGroupHandle, WorkGroupEntries,
@EnumWorkGroupBuffer, EnumBufferLength);
if EnumError = NO_ERROR then
begin
for I := 1 to WorkGroupEntries do
begin
EnumError := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0,
@EnumWorkGroupBuffer[I], EnumComputerHandle);
if EnumError = NO_ERROR then
begin
ComputerEntries := MaxEntries;
EnumBufferLength := SizeOf(EnumComputerBuffer);
EnumError := WNetEnumResource(EnumComputerHandle, ComputerEntries,
@EnumComputerBuffer, EnumBufferLength);
if EnumError = NO_ERROR then
for J := 1 to ComputerEntries do
Computers.Add(Copy(EnumComputerBuffer[J].lpRemoteName,
3, Length(EnumComputerBuffer[J].lpRemoteName) - 2));
WNetCloseEnum(EnumComputerHandle);
end;
end;
end;
WNetCloseEnum(EnumWorkGroupHandle);
end;
if EnumError = ERROR_NO_MORE_ITEMS then
EnumError := NO_ERROR;
Result := EnumError;
end;
initialization
Computers := TStringList.Create;
finalization
Computers.Free;
end.
printed from
www.swissdelphicenter.ch
developers knowledge base