Page 1 of 1

Posted: Sun Feb 16, 2003 9:27 pm
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/
--

Posted: Sat Feb 22, 2003 5:06 pm
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/
--

Posted: Sat Feb 22, 2003 5:34 pm
by BackupUser
Restored from previous forum. Originally posted by fred.

Excellent :).

Fred - AlphaSND

Posted: Sat Feb 22, 2003 5:40 pm
by BackupUser
Restored from previous forum. Originally posted by Leigh.

LOL

Leigh.

Posted: Sat Feb 22, 2003 6:30 pm
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/
--

Posted: Sat Feb 22, 2003 10:59 pm
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

Posted: Sun Feb 23, 2003 2:50 pm
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/
--

Posted: Mon Jun 09, 2003 5:20 pm
by Lucifer
Does anyone know how to modify this code to also retrieve the OS version of the selected PC?

Posted: Sat Jun 14, 2003 8:13 pm
by idStefke
Hi

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

Greetz
Stephane

Posted: Sat Jun 14, 2003 8:22 pm
by Karbon
Post the error, I'm sure someone can help you out.

Posted: Sat Jun 14, 2003 8:53 pm
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