Delphi 4 and the WNet API
by Fritz Lowery

  
Listing One
try
    { do the enumeration of containers }
    if WnetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, nil, hEnum) <>
                     NO_ERROR then
                exit;
    { now enumerate the containers  }
    iBufSize := sizeof(aBuf);
    iEntries := 64;  { 64 entries at a gulp }
    while WNetEnumResource(hEnum, iEntries, @aBuf[0], iBufSize) = NO_ERROR
do begin
     for i := 0 to (iEntries-1) do begin
       if ((aBuf[i].dwUsage and RESOURCEUSAGE_CONTAINER) =
                RESOURCEUSAGE_CONTAINER) and
       (pos('Microsoft Windows', string(aBuf[i].lpRemoteName)) = 1) then begin
               { enum workgroups and domains }
               try
               if WnetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, 
                          @aBuf[i], hEnum2) <> NO_ERROR then begin
               showmessage('Error enumerating Microsoft Network Resources!');
                           exit;
                      end;          
                      iBufSize := sizeof(aBuf2);
                      iEntries2 := 64;
                      while WNetEnumResource(hEnum2, iEntries2, @aBuf2[0], 
                                               iBufSize) = NO_ERROR do begin
                      for j := 0 to (iEntries2 - 1) do
                       lbContainers.items.add(string(abuf2[j].lpRemoteName));
                           iEntries2 := 64;
                        end;
                     finally
                        WNetCloseEnum(hEnum2);
                     end;
                 end;
             end;
             iEntries := 64;
        end;
finally
        WnetCloseEnum(hEnum);
end;
     
     
Listing Two
try
   screen.cursor := crHourGlass;
   with rNetRez do begin
      dwScope := RESOURCE_GLOBALNET;
      dwType  := RESOURCETYPE_ANY;
      dwDisplayType := RESOURCEDISPLAYTYPE_GENERIC; 
      dwUsage := RESOURCEUSAGE_CONNECTABLE; 
      lpLocalName := nil;
      lpRemoteName := pchar(lbContainers.items[lbContainers.itemindex]);
      lpComment := nil;
      lpProvider := nil;
  end;
  if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, 
                                 @rNetRez, hEnum) <> NO_ERROR then begin
       ShowMessage('Could not enumerate container: ' + 
                                 string(rNetRez.lpRemoteName));
       exit;
   end;
     
   iBufsize := sizeof(abuf);
   iEntries := 64;
   while WNetEnumResource(hEnum, iEntries, @aBuf[0], iBufSize) = NO_ERROR
do begin
        for i := 0 to (iEntries -1) do
        lbMachines.items.add(string(aBuf[i].lpRemoteName));
        iEntries := 64;
   end;
finally
   WnetCloseEnum(hEnum);
   screen.cursor := crDefault;
end;
     
Listing Three
zeromemory(@rNetRez, sizeof(TNetResource));
with rNetRez do begin
        dwType := RESOURCETYPE_ANY;
        lpLocalName := nil;
        lpProvider := nil;
end;
try
        sMachineName := lbMachines.items[i]; 
        rNetRez.lpRemoteName := pchar(sMachineName);
        iRet := WNetAddConnection2(rNetRez, pPassword, pUsername, 0);
        { 
                do stuff 
        }
finally
        WNetCancelConnection2(pchar(sMachineName), 0, true);
end;
1


