Lister tous les Dossiers partagé sur un Réseau
Publié : dim. 07/mars/2004 20:06
Salut à tous,
Depuis un momment j'essaye de lister tous les dossier partagé d'un réseau (donc de tous les postes), mais la je n'y arrive pas du tout...
J'ai trouvé un début de code, mais il marche pas bien et ne fais pas ce que je veux....
Je pense qu'il faut utiliser les fonctions suivantes :
WNetOpenEnum_ (The WNetOpenEnum function starts an enumeration of network resources or existing connections.)
WNetEnumResource_ (The WNetEnumResource function continues a network-resource enumeration started by the WNetOpenEnum function.)
Voici le début du code :
Alors qu'en pensez vous, est ce possible ?
Depuis un momment j'essaye de lister tous les dossier partagé d'un réseau (donc de tous les postes), mais la je n'y arrive pas du tout...
J'ai trouvé un début de code, mais il marche pas bien et ne fais pas ce que je veux....
Je pense qu'il faut utiliser les fonctions suivantes :
WNetOpenEnum_ (The WNetOpenEnum function starts an enumeration of network resources or existing connections.)
WNetEnumResource_ (The WNetEnumResource function continues a network-resource enumeration started by the WNetOpenEnum function.)
Voici le début du code :
Code : Tout sélectionner
Structure HOSTENT
h_name.l
h_aliases.l
h_addrtype.l
h_length.l
h_addr_list.l
EndStructure
; -----------------------------------------------------------------------------
; Some constants not defined in PB...
; -----------------------------------------------------------------------------
#NO_ERROR = 0
#ERROR_NO_ERROR = 0
#ERROR_BAD_DEVICE = 1200
#ERROR_CONNECTION_UNAVAIL = 1201
#ERROR_EXTENDED_ERROR = 1208
#ERROR_MORE_DATA = 234
#ERROR_NOT_SUPPORTED = 50
#ERROR_NO_NET_OR_BAD_PATH = 1203
#ERROR_NO_NETWORK = 1222
#ERROR_NOT_CONNECTED = 2250
; -----------------------------------------------------------------------------
; Some error procedures...
; -----------------------------------------------------------------------------
; Used to set last error to 0 on startup (NEEDED!)...
Procedure InitErrors (defaultt)
SetLastError_ (defaultt)
EndProcedure
; Retrieves last reported error in program...
Procedure GetLastError ()
error = GetLastError_ ()
If error
AllocateMemory (0, 255)
FormatMessage_ (#FORMAT_MESSAGE_FROM_SYSTEM, #NULL, error, 0, MemoryID (), 255, #NULL)
e$ = PeekS (MemoryID ())
FreeMemory (0)
MessageRequester (title$, e$, #MB_ICONWARNING)
EndIf
EndProcedure
; Retrieves last network error in program...
Procedure GetNetError ()
error$ = Space (256)
provider$ = Space (256)
WNetGetLastError_ (code, error$, 256, provider$, 256)
If error$ = Space (256)
error$ = "Weird; there's no reported network error! Why am I here?!"
EndIf
MessageRequester ("Network error!", "A network error has occurred!" + Chr (10) + Chr (10) + error$, #MB_ICONWARNING)
EndProcedure
; -----------------------------------------------------------------------------
; MAIN PROGRAM...
; -----------------------------------------------------------------------------
; Reset the error system...
InitErrors (0)
; WSAStartup is needed before calling gethostbyname_ () further down...
; Version 1.1 of Windows Sockets needed...
high.b = 1
low.b = 1
DefType.w wsaversion
PokeB (@wsaversion, high) ; Gotta poke major version number into low byte...
PokeB (@wsaversion + 1, low) ; ... and minor version number into high byte
; WSAStartup returns 0 (no errors) if successful...
If WSAStartup_ (wsaversion, wsa.WSAData)
MessageRequester ("Network error", "WSA startup failed!" + Chr (10) + Chr (10) + error$, #MB_ICONWARNING)
End
EndIf
; Get a handle for retrieving a list of computers on local network...
error = WNetOpenEnum_ (#RESOURCE_CONTEXT, #RESOURCETYPE_DISK, 0, #NULL, @nethandle)
Select error
Case #ERROR_NO_ERROR
; ---------------------------------------------------------------------
; START OF NETWORK PC LIST CODE...
; ---------------------------------------------------------------------
; No error, so let's see what we have here...
OpenConsole ()
PrintN ("")
PrintN ("Local network resources...")
PrintN ("")
; Default to a large number of PCs (thanks to M$, you MUST do it like this!)...
check = 1024
entries = -1
size = SizeOf (NETRESOURCE) * check
Dim network.NETRESOURCE (check)
; Read list of PCs into network.NETRESOURCE array...
reserror = WNetEnumResource_ (nethandle, @entries, @network (), @size)
If reserror = #NO_ERROR
; Read array for however many we found (WNetEnumResource pokes the number into 'entries')...
For entry = 1 To entries
Select network (entry)\dwScope
; Dunno what this means exactly...
Case #RESOURCE_CONNECTED
PrintN ("Resource connected")
; But this is the one we want...
Case #RESOURCE_GLOBALNET
; Get network PC name and the type of network (eg. Microsoft Windows Network)...
computer$ = PeekS (network (entry)\lpRemoteName)
networktype$ = PeekS (network (entry)\lpProvider)
; Strip off backslash prefix (eg. "\\YOURCOMPUTERNAME")...
While Left (computer$, 1) = "\"
computer$ = Right (computer$, Len (computer$) - 1)
Wend
PrintN (Chr (34) + computer$ + Chr (34) + " on " + Chr (34) + networktype$ + Chr (34))
; Get pointer to the host data for the network PC...
*host.HOSTENT = gethostbyname_ (computer$)
If *host <> #NULL
; Here we print the name and the IP address (hooray for inet_ntoa!)...
PrintN ("")
PrintN (" Host name: " + PeekS (*host\h_name))
; PrintN (" IP address: " + PeekS (inet_ntoa_ (PeekL (*host\h_addr_list))))
Else
GetLastError ()
EndIf
; Dunno about this either...
Case #RESOURCE_REMEMBERED
PrintN ("Resource remembered")
EndSelect
PrintN ("")
Next
EndIf
PrintN ("Press ENTER...")
Input ()
CloseConsole ()
; We MUST free the handle we got for listing the network PCs...
WNetCloseEnum_ (nethandle)
; ... and we also MUST clean up the WSA thing!
WSACleanup_ ()
; ---------------------------------------------------------------------
; END OF NETWORK PC LIST CODE...
; ---------------------------------------------------------------------
; This stuff is the remainder of the first Select/EndSelect after WNetOpenEnum...
Case #ERROR_NO_NETWORK
MessageRequester ("Network error!", "There's no network here!", #MB_ICONWARNING)
End
Case #ERROR_EXTENDED_ERROR
GetNetError ()
End
Default
GetLastError ()
EndSelect
End
Alors qu'en pensez vous, est ce possible ?
