Browse for network computer...

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

This opens a dialog box and lets you select a computer on the network; also returns IP address of a named PC. Should be useful for LAN chat programs and stuff...

Code: Select all

Procedure.s GetNetworkComputer (parentwindow, title$)

    ; Note: take a look at the Win32 docs for SHBrowseForFolder, as you can easily
    ; modify this function to return printers, etc...
    
    #CSIDL_NETWORK = $12

    ; Weird Windows structure (holds an internal reference to a folder location, I think)...
    Structure ****EMID
        cb.b
        abID.b[1]
    EndStructure

    ; And more...
    Structure ITEMIDLIST
        mkid.****EMID
    EndStructure

    ; Create an item, whatever that may be, and stuff the required location into it...
    *itemid.ITEMIDLIST = #NULL
    SHGetSpecialFolderLocation_ (0, #CSIDL_NETWORK, @*itemid) = #NOERROR

    ; Create a BROWSEINFO structure and zero it (there's probably a quicker way to do this :wink:
    DefType.BROWSEINFO bi
    For b = 0 To SizeOf (BROWSEINFO) - 1
        PokeB (@bi + b, 0)
    Next

    ; Create a buffer for the computer name to be placed in...
    computer$ = Space (#MAX_PATH)

    ; Fill in the structure...
    bi\hwndOwner = parentwindow
    bi\pidlRoot = *itemid
    bi\pszDisplayName = @computer$
    bi\lpszTitle = @title$
    bi\ulFlags = #BIF_BROWSEFORCOMPUTER

    ; Show the browser dialog...
    pidl.ITEMIDLIST = SHBrowseForFolder_ (@bi)

    ; Set the computer name to "" if there's nothing returned...
    If computer$ = Space (#MAX_PATH)
        computer$ = ""
    EndIf

    ProcedureReturn computer$
        
EndProcedure

Procedure.s GetNetworkComputerIP (computer$)

    ; Structure used for host information...
    Structure HOSTENT
     h_name.l
     h_aliases.l
     h_addrtype.l
     h_length.l
     h_addr_list.l
    EndStructure

    If computer$

        ; Create WSA version number (damn, ugly!)...    
        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

        ; Try to access Windows sockets stuff...
        If WSAStartup_ (wsaversion, wsa.WSAData) = #NOERROR
            ; Get host information for named computer...
            *host.HOSTENT = gethostbyname_ (computer$)
            If *host  #NULL
                ; Get IP address of named computer...
                ip$ = PeekS (inet_ntoa_  (PeekL (*host\h_addr_list)))
            EndIf
            ; Close Windows sockets stuff...
            WSACleanup_ ()
        EndIf
        ProcedureReturn ip$
        
    EndIf
    
EndProcedure

; D E M O . . .

; Show network computer selection dialog (note that '0' can be replaced by a window handle)...

pc$ = GetNetworkComputer (0, "Select a local computer...")

If pc$
    ; Get IP address of selected computer...
    ip$ = GetNetworkComputerIP (pc$)
    MessageRequester ("Selected PC...", "You selected " + pc$ + ", at IP address " + ip$, #MB_ICONINFORMATION)
EndIf

--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Ha ha! Just noticed that the forum automatically censors out swear words. Unfortunately, S-H-I-T-E-M-I-D is a genuine, valid Windows structure, defined in the Windows API (it stands for Shell Item ID)! Replace the **** above with S, H, I, and T :)


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Excellent :).

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Leigh.

LOL

Leigh.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

BTW I've been readiing the docs for this again -- I'm supposed to release the pidl.ITEMIDLIST using the IMalloc::Free call, but this COM stuff is beyond me! Anyone know what to add to this code to free 'pidl' after I'm finished using it?

Now I just want to say ****EMID ****EMID ****EMID a few times... :wink:


--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Bodind.

Hello, Hi-Toro.

I think you can use GLOBALFREE_() to release the memory used by the list.

Dominique
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Hi-Toro.

Doesn't seem to work. The docs state you have to get the shell's Malloc interface (I think that's its handle to memory allocated during shell function calls), and that you have to free it using the IMalloc::Free interface. I've tried this, but it doesn't work...

Code: Select all

    If SHGetMalloc_ (@*mem) = #NOERROR
        If GlobalFree_ (mem) = 0
            MessageRequester ("", "Yes", 0)
        EndIf
    EndIf

--
See ya,
James L Boyd.
http://www.hi-toro.com/
--
Lucifer
User
User
Posts: 26
Joined: Wed Apr 30, 2003 6:11 pm
Location: UK

Post by Lucifer »

Does anyone know how to modify this code to also retrieve the OS version of the selected PC?
idStefke
User
User
Posts: 25
Joined: Sun May 04, 2003 10:01 pm
Location: BELGIUM

Post by idStefke »

Hi

It doesn't compile and execute in PB v3.70

Greetz
Stephane
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Post the error, I'm sure someone can help you out.
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

this works also in 3.7
(remember 3.7 did more errorchecking on fault vaiables)

Code: Select all

Procedure.s GetNetworkComputer (parentwindow, title$)

; Note: take a look at the Win32 docs for SHBrowseForFolder, as you can easily
; modify this function to return printers, etc...
#CSIDL_NETWORK = $12
; Weird Windows structure (holds an internal reference to a folder location, I think)...
Structure EMID
 cb.b
 abID.b[1]
EndStructure

; And more...
Structure ITEMIDLIST
 mkid.EMID
EndStructure

; Create an item, whatever that may be, and stuff the required location into it...
itemid.ITEMIDLIST 

If SHGetSpecialFolderLocation_ (0, #CSIDL_NETWORK, @itemid) = #NOERROR

; Create a BROWSEINFO structure and zero it (there's probably a quicker way to do this  
DefType.BROWSEINFO bi
For b = 0 To SizeOf (BROWSEINFO) - 1
PokeB (@bi + b, 0)
Next

; Create a buffer for the computer name to be placed in...
computer$ = Space (#MAX_PATH)

; Fill in the structure...
bi\hwndOwner = parentwindow
bi\pidlRoot = *itemid
bi\pszDisplayName = @computer$
bi\lpszTitle = @title$
bi\ulFlags = #BIF_BROWSEFORCOMPUTER

; Show the browser dialog...
pidl = SHBrowseForFolder_ (@bi)

EndIf
; Set the computer name to "" if there's nothing returned...
If computer$ = Space (#MAX_PATH)
computer$ = ""
EndIf

ProcedureReturn computer$

EndProcedure

Procedure.s GetNetworkComputerIP (computer$)

; Structure used for host information...
Structure HOSTENT
h_name.l
h_aliases.l
h_addrtype.l
h_length.l
h_addr_list.l
EndStructure

If computer$

; Create WSA version number (damn, ugly!)... 
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

; Try to access Windows sockets stuff...
If WSAStartup_ (wsaversion, wsa.WSAData) = #NOERROR
; Get host information for named computer...
*host.HOSTENT = gethostbyname_ (computer$)
If *host <> #NULL
; Get IP address of named computer...
ip$ = PeekS (inet_ntoa_ (PeekL (*host\h_addr_list)))
EndIf
; Close Windows sockets stuff...
WSACleanup_ ()
EndIf
ProcedureReturn ip$

EndIf

EndProcedure

; D E M O . . .

; Show network computer selection dialog (note that '0' can be replaced by a window handle)...

pc$ = GetNetworkComputer (0, "Select a local computer...")

If pc$
 ; Get IP address of selected computer...
 ip$ = GetNetworkComputerIP (pc$)
 MessageRequester ("Selected PC...", "You selected " + pc$ + ", at IP address " + ip$, #MB_ICONINFORMATION)
EndIf

SPAMINATOR NR.1
Post Reply