API function that returns the same information as NET USE?

Windows specific forum
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

API function that returns the same information as NET USE?

Post by Little John »

Hi all,

when I open a console window an execute the command NET USE here (Windows XP), I get the following information about our LAN:

Code: Select all

Status       Lokal     Remote                    Netzwerk

---------------------------------------------------------------------------
OK           S:        \\Server\Dokumente        Microsoft Windows-Netzwerk
OK           U:        \\Server\Users\Luethje    Microsoft Windows-Netzwerk
OK                     \\Server\Dokumente        Microsoft Windows-Netzwerk
OK                     \\SERVER\SYSVOL           Microsoft Windows-Netzwerk
OK                     \\Server\Users            Microsoft Windows-Netzwerk
How can a PB program retrieve the same information? Is there an API function for this purpose?

Regards, Little John
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: API function that returns the same information as NET US

Post by skywalk »

Code: Select all

;-{ NETWORK DRIVES
; PureBasic Forum: Rings, Hi-Toro, Fangbeast
; skywalk: cosmetic changes to include in FileLib = "FL_xxx"
; Tested on Windows XP Pro SP3.
Structure FL_DriveList ; Keep track of drive letters and net paths
  DriveLetter.s
  DrivePath.s
EndStructure
Global NewList FL_NetDrives.FL_DriveList()  ; Tracking list for the network drives on your system

Procedure FL_GetNetDrives()                         ; 
  ; Find all network drives attached to the system and store them in a linked list
  Protected.i DriveCount, DriveType, LenUNCPath, ErrorNum, ExistsFlag
  Protected.s DriveLetter, UNCPath
  #dllMPR = 1                                                 ; Use a constant for the dll
  If OpenLibrary(#dllMPR, "MPR.DLL")                          ; Open the necessary windows dll
    For DriveCount = 4 To 26                                  ; Count through drives, exclude floppies
      DriveLetter = Chr(DriveCount + 64) + ":"                ; Get current drive letter
      DriveType = GetDriveType_(DriveLetter)                  ; What sort of drive are we looking for
      If DriveType = #DRIVE_REMOTE                            ; Did we find a remote drive?
        UNCPath = Space(128)                                  ; Allocate space for the path length
        LenUNCPath = 127                                      ; Allocate the length for this space
        ErrorNum = CallFunction(#dllMPR, "WNetGetConnectionA", @DriveLetter, @UNCPath, @LenUNCPath)
        ;WNetGetConnection_()
        If ErrorNum = #NO_ERROR                               ; We didn't get an error
          UNCPath = Left(UNCPath, LenUNCPath)                 ; Get the current drive found
          ForEach FL_NetDrives()                                 ; Iterate through our tracking list
            If FL_NetDrives()\DrivePath = UNCPath                ; If the drive already exists in the list
              ExistsFlag = 1                                  ; Set the flag on
            Else                                              ; Otherwise
              ExistsFlag = 0                                  ; Set the flag off
            EndIf                                             ; No more tests
          Next                                                ; Next drive in the tracking list
          If ExistsFlag = 0                                   ; If the flag was set off
            AddElement(FL_NetDrives())                           ; Add a blank element to the tracking list
            FL_NetDrives()\DriveLetter = DriveLetter             ; Add the found drive to it
            FL_NetDrives()\DrivePath   = UNCPath                 ; Add the found drive to it
          EndIf                                               ; No more tests
          ForEach FL_NetDrives()                                 ; Iterate through the tracking list
            ; Debug NetDrives()                               ; Show the found network drives or other things you need to do with them
          Next                                                ; Next drive in the list
        Else                                                  ; Otherwise, we have an error
          Select ErrorNum                                     ; Test the error number
            Case #ERROR_BAD_DEVICE         : UNCPath = "ERROR_BAD_DEVICE"
            Case #ERROR_NOT_CONNECTED      : UNCPath = "ERROR_NOT_CONNECTED"
            Case #ERROR_MORE_DATA          : UNCPath = "ERROR_MORE_DATA"
            Case #ERROR_CONNECTION_UNAVAIL : UNCPath = "ERROR_CONNECTION_UNAVAIL"
            Case #ERROR_NO_NETWORK         : UNCPath = "ERROR_NO_NETWORK"
            Case #ERROR_EXTENDED_ERROR     : UNCPath = "ERROR_EXTENDED_ERROR"
            Case #ERROR_NO_NET_OR_BAD_PATH : UNCPath = "ERROR_NO_NET_OR_BAD_PATH"
            Case #ERROR_INVALID_ADDRESS    : UNCPath = "ERROR_INVALID_ADDRESS"
            Default                        : UNCPath = "UNKNOWN ERROR " + Str(ErrorNum)
          EndSelect                                           ; No more tests to do
        EndIf                                                 ; No more conditional tests
        ; AllDrives = AllDrives + UNCPath + #CRLF$            ; Usually add up all the network drives found here
      EndIf                                                   ; No more conditional tests
    Next                                                      ; Look for the next possible drive letter
    ; If just reporting ALL the drives found, this is a good place to put it
    ; If Len(AllDrives)    
    ;   Debug AllDrives
    ; EndIf
    ; ------------------------------------------------------------------------------------------------  
  Else                                                        ; Otherwise, we cannot find the right dll for these functions
   MessageRequester("Error", "MPR.DLL not found", 0)          ; Give the user an error message
  EndIf                                                       ; No more tests
EndProcedure                                                  

Procedure.i FL_ConnectNetDrive(LocalDriveLetter.s, RemotePath.s)
  ; Connect a remote network drive to your system. Must already be shared
  ; RemotePath.s        = "\\FurBox\Roo's incoming"           ; Remote path
  ; LocalDriveLetter.s  = "Z:"                                ; Local drive to be 'created'
  Protected.NETRESOURCE res                                   ; res points to structure that specifies connection details
  res\dwType        = #RESOURCETYPE_DISK 
  res\lpLocalName   = @LocalDriveLetter.s
  res\lpRemoteName  = @RemotePath.s     
  res\lpProvider    = #Null             
  If WNetAddConnection2_(res, #Null, #Null, 0) = #NO_ERROR   
    FL_GetNetDrives()                                         ; Rescan system and add the new drive not in the list
    ;MessageRequester("Information:", "Connected the network drive successfully.", #MB_ICONINFORMATION)
    ProcedureReturn 1
  Else                                  
    MessageRequester("Error:", "Could not connect the network drive, check paths, network and share status.", #MB_ICONWARNING)
    ProcedureReturn 0
  EndIf                                                      
EndProcedure                                                 

Procedure.i FL_DisconnectNetDrive(LocalDriveLetter.s, Remove.i)
  ; Disconnect a network drive attached to the system. 
  ; Check the linked List to see if it had already been added before and
  ; optionally delete the item from the list
  ; RemotePath.s        = "\\FurBox\Roo's incoming"           ; Remote path
  ; LocalDriveLetter.s  = "Z:"                                ; Local drive to be 'created'
  ; Remove.i                                                  ; Remove the disconnected drive from the tracking linked list
  Protected.NETRESOURCE res                                   ; res points to Structure that specifies connection details
  ForEach FL_NetDrives()                                         ; Iterate through our tracking list
    If FL_NetDrives()\DriveLetter = LocalDriveLetter.s           ; If the found drive is in the tracking list
      res\dwType        = #RESOURCETYPE_DISK                   
      res\lpLocalName   = @LocalDriveLetter.s                  
      res\lpRemoteName  = @FL_NetDrives()\DrivePath              
      res\lpProvider    = #Null                                
      ; Disconnect the connection if it exists
      If WNetCancelConnection2_(LocalDriveLetter.s, #CONNECT_UPDATE_PROFILE, 1) = #NO_ERROR   
        ProcedureReturn 1
      Else
        ProcedureReturn 0
      EndIf      
    EndIf                                                      
    If Remove                                                 ; Do we want to keep this path in the list or not?
      DeleteElement(FL_NetDrives())                               
    EndIf                                                      
  Next                                                         
EndProcedure                                                  

Procedure FL_ConnectNetDriveD()                            
  ; Assign remote path to drive (eg. "Z:") using an interactive dialog box
  If WNetConnectionDialog_ (0, #RESOURCETYPE_DISK) = #NO_ERROR 
    MessageRequester("Information:", "Connected the network drive successfully", #MB_ICONINFORMATION)
    FL_GetNetDrives()                                         ; Rescan system and add the new drive not in the list
  Else                                                        
    MessageRequester("Error:", "Couldn't connect the network drive, check paths, network and share status", #MB_ICONWARNING)
  EndIf                                                       
EndProcedure                                                 

Procedure FL_CopyFileToNetDrive(DriveLetter.s, FileName.s)
  ; Copy a local file to a network drive. Check if it is in the list first. Check if the dir exists
  Protected.s NewFile
  ForEach FL_NetDrives()                                         ; Iterate through our tracking list
    If FL_NetDrives()\DriveLetter = DriveLetter                  ; If the found drive is in the tracking list
      If FileSize(FL_NetDrives()\DrivePath) = -2                 
        NewFile = FL_NetDrives()\DrivePath + "\" + GetFilePart(FileName)   
        If CopyFile(FileName, NewFile)                    
          MessageRequester("Information:", "Copied " + FileName + " to " + NewFile, #MB_ICONWARNING)
        Else                                                   
          MessageRequester("Error:", "Cannot copy the file to the destination", #MB_ICONWARNING)
        EndIf                                                  
      Else                                                     
        MessageRequester("Error:", "This drive does not exist, cannot copy the file to destination", #MB_ICONWARNING)
      EndIf                                                    
    Else                                                       
      MessageRequester("Error:", "This drive does not exist in the list, cannot copy the file to destination", #MB_ICONWARNING)
    EndIf                                                     
  Next                                                         
EndProcedure                                                 
;-}
FL_GetNetDrives()
ForEach FL_NetDrives()
  Debug FL_NetDrives()\DriveLetter + " -> " + FL_NetDrives()\DrivePath
Next
;Debug FL_DisconnectNetDrive("Z:", 1)
;Debug FL_ConnectNetDrive("Z:","\\HostName\shared_drive_name")
;Debug FL_DisconnectNetDrive("Z:",1)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: API function that returns the same information as NET US

Post by Little John »

Works fine here, too. Thank you very much!

Best regards, Little John
User avatar
happer66
User
User
Posts: 33
Joined: Tue Jan 12, 2010 12:10 pm
Location: Sweden

Re: API function that returns the same information as NET US

Post by happer66 »

I think it would be wise to start enumerating drive letters from A. A,B aren't restricted for floppy disks only, windows will happily map a network drive to anything between A-Z as long as it's not already occupied.

Line 17:

Code: Select all

    For DriveCount = 1 To 26                                  ; Go through all drive letters (A-Z)
Image If your code isn't clean atleast make sure it's pure!
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: API function that returns the same information as NET US

Post by skywalk »

Good point.
I think this was just to avoid the 'C:' drive and 'A:' & 'B:' get the historical fixed drive bump.
As you say, most of those drives are now only useful for synthesized instrumentals. :wink:
http://www.youtube.com/watch?v=dmoDLyiQYKw
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply