Hi,<p>
ich hab mal wieder die Funktion gebraucht, alle verfügbaren Domänen aufzulisten. Bei unserem (sehr großen) Netzwerkverbund hat obige Funktion immer nach 626 Domänen aufgehört. Daher hab ich mich nochmal hingesetzt und die Funktion mal analysiert.<p>
WNetEnumResource meldet anscheinend nie ERROR_MORE_DATA zurück. Statt dessen immer NO_ERROR. Also hab ich die Funktion umgeschrieben. Jetzt funktioniert sie. Ich bekomme alle verfügbaren Domänen zurückgeliefert.<p>
Grüße, Reimund<p>
<pre>
function EnumDomains(sl: TStringlist): Boolean;
var
k: Longword; // auxiliary integer
p: PNetResource; // auxiliary pointer
s: string; // auxiliary string
dwBufSize: DWord; // buffer size
dwEntries: DWord; // entries' number
dwRC: DWord; // return code
hEnum: THandle; // handle for enumeration
pBuf: PNetResource; // pointer to buffer
sProvider: string; // provider's name
begin
// initialize the result
result := True;
// enumeration to get the provider's name
// initialize the enumeration
if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, nil, hEnum) <> NO_ERROR then
Exit;
try
// initialize the entries' number (as many as possible)
dwEntries := DWord(-1);
// initialize the buffer size
dwBufSize := 1024;
// get the buffer's memory space
pBuf := AllocMem(dwBufSize);
// start the enumeration
if WNetEnumResource(hEnum, dwEntries, pBuf, dwBufSize) <> NO_ERROR then
Exit;
// get the provider's name
sProvider := pBuf.lpProvider;
// release the buffer
FreeMem(pBuf);
finally
// close the enumeration
WNetCloseEnum(hEnum);
end;
// enumeration to get the domains' names
// initialize the buffer size
dwBufSize := 64 shl 10; // 64 * 1024
// initialize the buffer
pBuf := AllocMem(dwBufSize);
// initialize the provider
pBuf.lpProvider := PChar(sProvider);
// initialize the enumeration
if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pBuf, hEnum) <> NO_ERROR then
Exit;
try
// repeat until no more items were found
repeat
// initialize the entries' number (as many as possible)
dwEntries := DWord(-1);
// start the enumeration
dwRC := WNetEnumResource(hEnum, dwEntries, pBuf, dwBufSize);
if dwRC = NO_ERROR then
begin
// there are found items -> memorize them
k := dwEntries;
p := pBuf;
while k > 0 do
begin
// add the ressource name (domain name)
s := Trim(p.lpRemoteName);
if s > '' then
sl.Add(s);
// set the buffer pointer to the next entry
Inc(p);
// decrement the entries' number
Dec(k);
end;
// clear the buffer (release it and get it again)
FreeMem(pBuf);
pBuf := AllocMem(dwBufSize);
end;
if (dwRC <> NO_ERROR) and (dwRC <> ERROR_NO_MORE_ITEMS) then
begin
// there is an error occured
result := False;
Break;
end;
until dwRC = ERROR_NO_MORE_ITEMS;
finally
// close the enumeration
WNetCloseEnum(hEnum);
// if the buffer is assigned, release it
if Assigned(pBuf) then
FreeMem(pBuf);
end;
end;
</pre>
ich hab mal wieder die Funktion gebraucht, alle verfügbaren Domänen aufzulisten. Bei unserem (sehr großen) Netzwerkverbund hat obige Funktion immer nach 626 Domänen aufgehört. Daher hab ich mich nochmal hingesetzt und die Funktion mal analysiert.<p>
WNetEnumResource meldet anscheinend nie ERROR_MORE_DATA zurück. Statt dessen immer NO_ERROR. Also hab ich die Funktion umgeschrieben. Jetzt funktioniert sie. Ich bekomme alle verfügbaren Domänen zurückgeliefert.<p>
Grüße, Reimund<p>
<pre>
function EnumDomains(sl: TStringlist): Boolean;
var
k: Longword; // auxiliary integer
p: PNetResource; // auxiliary pointer
s: string; // auxiliary string
dwBufSize: DWord; // buffer size
dwEntries: DWord; // entries' number
dwRC: DWord; // return code
hEnum: THandle; // handle for enumeration
pBuf: PNetResource; // pointer to buffer
sProvider: string; // provider's name
begin
// initialize the result
result := True;
// enumeration to get the provider's name
// initialize the enumeration
if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, nil, hEnum) <> NO_ERROR then
Exit;
try
// initialize the entries' number (as many as possible)
dwEntries := DWord(-1);
// initialize the buffer size
dwBufSize := 1024;
// get the buffer's memory space
pBuf := AllocMem(dwBufSize);
// start the enumeration
if WNetEnumResource(hEnum, dwEntries, pBuf, dwBufSize) <> NO_ERROR then
Exit;
// get the provider's name
sProvider := pBuf.lpProvider;
// release the buffer
FreeMem(pBuf);
finally
// close the enumeration
WNetCloseEnum(hEnum);
end;
// enumeration to get the domains' names
// initialize the buffer size
dwBufSize := 64 shl 10; // 64 * 1024
// initialize the buffer
pBuf := AllocMem(dwBufSize);
// initialize the provider
pBuf.lpProvider := PChar(sProvider);
// initialize the enumeration
if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pBuf, hEnum) <> NO_ERROR then
Exit;
try
// repeat until no more items were found
repeat
// initialize the entries' number (as many as possible)
dwEntries := DWord(-1);
// start the enumeration
dwRC := WNetEnumResource(hEnum, dwEntries, pBuf, dwBufSize);
if dwRC = NO_ERROR then
begin
// there are found items -> memorize them
k := dwEntries;
p := pBuf;
while k > 0 do
begin
// add the ressource name (domain name)
s := Trim(p.lpRemoteName);
if s > '' then
sl.Add(s);
// set the buffer pointer to the next entry
Inc(p);
// decrement the entries' number
Dec(k);
end;
// clear the buffer (release it and get it again)
FreeMem(pBuf);
pBuf := AllocMem(dwBufSize);
end;
if (dwRC <> NO_ERROR) and (dwRC <> ERROR_NO_MORE_ITEMS) then
begin
// there is an error occured
result := False;
Break;
end;
until dwRC = ERROR_NO_MORE_ITEMS;
finally
// close the enumeration
WNetCloseEnum(hEnum);
// if the buffer is assigned, release it
if Assigned(pBuf) then
FreeMem(pBuf);
end;
end;
</pre>
Comment