Network Share Management

Share your advanced PureBasic knowledge/code with the community.
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Re: Network Share Management Functions

Post by jpd »

really nice work JHPJHP thanks for that!

here the NetConnectionEnum function part of the Network Share Management Functions.

Code: Select all

EnableExplicit


#MAX_PREFERRED_LENGTH = -1 
#NERR_SUCCESS=0 
#ERROR_MORE_DATA = 234
#LB_SETTABSTOPS= 402
#STYPE_DISKTREE = 0   
#STYPE_PRINTQ = 1
#STYPE_DEVICE = 2
#STYPE_IPC = 3

Structure CONNECTION_INFO_1
  coni1_id.i
  coni1_type.i
  coni1_num_opens.i
  coni1_num_users.i
  coni1_time.i
  coni1_username.i
  coni1_netname.i
EndStructure

Structure CONNECTION_INFO_1_Container
  coni1_id.l
  coni1_type.s
  coni1_num_opens.l
  coni1_num_users.l
  coni1_time.s
  coni1_username.s
  coni1_netname.s
EndStructure

Global NewList NetConnectEnum.CONNECTION_INFO_1_Container()


Procedure.s GetConnectionType(dwSessionType.i)
  
  Select  Val(Hex(dwSessionType))
    Case #STYPE_DISKTREE
      ProcedureReturn  "Disk drive"
    Case #STYPE_PRINTQ
      ProcedureReturn  "Print queue"
    Case #STYPE_DEVICE
      ProcedureReturn   "Communication device"
    Case #STYPE_IPC
      ProcedureReturn "(IPC)"
    Default
      ProcedureReturn ""
  EndSelect
  
EndProcedure 


#SIZEOF_WORD = 2


;  _____________________________________________________________________________
;  |                                                                           |
;  |                            Ansi2Uni                                       |
;  |                            ________                                       |
;  |                                                                           |
;  |___________________________________________________________________________|
;{ Ansi2Uni                                    
;/ BackupUser
; part of the Droopy Library..  

Procedure Ansi2Uni(string.s) ; Converts normal (Ansi) string To Unicode 
  Protected  *out
  *out = AllocateMemory(Len(string)*2 * #SIZEOF_WORD) 
  MultiByteToWideChar_(#CP_ACP, 0, string, -1, *out, Len(string))  
  ProcedureReturn *out  
EndProcedure 

Procedure NetConnectionEnum(sServer.s = "",sShare.s);Command1_Click()
  
  Protected dwEntriesread.l,dwTotalentries.l,dwResumehandle.l,success.l,nStructSize.l,cnt.l
  Protected pServer, pShare
  Protected *buffer, *ci1.CONNECTION_INFO_1
  dwEntriesread=#Null
  dwTotalentries=#Null
  dwResumehandle=#Null
  nStructSize = SizeOf(CONNECTION_INFO_1)
  
  If sServer <> ""
    sServer=RemoveString(sServer, "\" );[, Modus])
    sServer="\\"+sServer
  Else
    sServer="\\"+ComputerName()  
  EndIf
  
  CompilerIf #PB_Compiler_Unicode
    success = NetConnectionEnum_(@sServer, @sShare,1,@*buffer,#MAX_PREFERRED_LENGTH, @dwEntriesread,@dwTotalentries,@dwResumehandle)
  CompilerElse
    pServer= Ansi2Uni(sServer)
    pShare= Ansi2Uni(sShare)
    success = NetConnectionEnum_(pServer, pShare,1,@*buffer,#MAX_PREFERRED_LENGTH, @dwEntriesread,@dwTotalentries,@dwResumehandle)
  CompilerEndIf
  
  If success = #NERR_SUCCESS And success <> #ERROR_MORE_DATA And dwEntriesread <>0
    
    For cnt = 0 To dwEntriesread - 1
      *ci1.CONNECTION_INFO_1=*buffer +nStructSize*cnt
      AddElement(NetConnectEnum())
      NetConnectEnum()\coni1_username=PeekS(*ci1\coni1_username, -1, #PB_Unicode)
      NetConnectEnum()\coni1_netname=PeekS(*ci1\coni1_netname, -1, #PB_Unicode)
      NetConnectEnum()\coni1_time=FormatDate("%hh:%ii:%ss",*ci1\coni1_time)
      NetConnectEnum()\coni1_num_users=*ci1\coni1_num_users
      NetConnectEnum()\coni1_num_opens=*ci1\coni1_num_opens
      NetConnectEnum()\ coni1_id=*ci1\coni1_id
      NetConnectEnum()\coni1_type=GetConnectionType(*ci1\coni1_type)
      
    Next
  EndIf
  NetApiBufferFree_(*buffer)
  
  
  Debug "Enumconnect: "+Str(success)+" Entry Nr: "+Str(dwEntriesread)
  
EndProcedure 


; Usage NetConnectionEnum(Computername,Sharename) if computername omissed then the local computername is used 

NetConnectionEnum("C$")

ForEach NetConnectEnum()
  Debug "ConnectionID: " + NetConnectEnum()\coni1_id
  Debug "Connection Name: " + NetConnectEnum()\coni1_netname
  Debug "Open Connection: " + NetConnectEnum()\coni1_num_opens
  Debug "Nr. Connected Users: " + NetConnectEnum()\coni1_num_users
  Debug "Connection Time: " + NetConnectEnum()\coni1_time
  Debug "Connection Type: " + NetConnectEnum()\coni1_type
  Debug "Connected User: " + NetConnectEnum()\coni1_username
Next

br
jpd
PB 5.10 Windows 7 x64 SP1
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Network Share Management Functions

Post by JHPJHP »

Hi jpd,

Thanks for the heads-up and example, I didn't realize I missed that one.

------------------------------------

Updated:
- added example: NetConnectionEnum.pb

------------------------------------

Updated:
- added support for x64
Locked