Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Win32-API-Prototype WNet

by Anonymous Monk
on Mar 01, 2005 at 14:13 UTC ( [id://435438]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hiya Monks,

I am having a terrible time trying to use the Win32-API-Prototype module! I basically would like to reference Windows Networking Functions to return a list of all devices available on a local network but I can't understand how to use the module. All i've got so far is pretty much:

use Win32::API::Prototype; ApiLink('kernel32.dll', 'DWORD WNetOpenEnum(DWORD dwScope,DWORD dwType,DWORD dwUsage,L +PNETRESOURCE lpNetResource,LPHANDLE lphEnum)'); ApiLink('kernel32.dll', 'DWORD WNetEnumResource(HANDLE hEnum,LPDWORD lpcCount,LPVOID l +pBuffer,LPDWORD lpBufferSize)'); ApiLink('kernel32.dll', 'DWORD WNetCloseEnum(HANDLE hEnum)');
Can any of you possibly shed some light on how i should use this module correctly to return what I want?

Thanks, Jenni xoxo

Replies are listed 'Best First'.
Re: Win32-API-Prototype WNet
by holli (Abbot) on Mar 01, 2005 at 15:05 UTC
      Hi, thanks for your reply. I'm not entirely sure how this module would be able to return a list of the nodes available on a network. It seems to identify those with shared resources. I tested the example enumerating all resources on a network, and it only picked up on my pc and not others on the network.

      Jenni

        You said,
        ...list of all devices available on a local network
        , and those are, imho, shared devices/network printers etc.
        What exactly are you trying to achieve?


        holli, /regexed monk/
      Hi all, have been working on it a while now and just can't seem to get the code working.
      To recap and confirm, it is supposed to return all nodes on a network by accessing Windows WNet API functions.
      #!/usr/bin/perl use strict; use Win32::API::Prototype; # WNetOpenEnum variables my $dwScope = "RESOURCE_CONNECTED"; my $dwType = "RESOURCETYPE_ANY"; my $dwUsage = "0"; my $lpNetResource = "NULL"; # assumes the ro +ot of the network my $lphEnum; # WNetEnumResource variables my $hEnum; # must be return +ed by WNetOpenEnum my $lpcCount = "-1"; # returns as ma +ny entries as possible my $lpBufferSize = "16384"; # specifies the +size of the lpBuffer parameter my $lpBuffer = AllocMemory( 16384 ); # points to the + buffer that receives the emuneration results # WNetOpenEnum ApiLink('mpr.dll', 'DWORD WNetOpenEnum(DWORD dwScope, DWORD dwType, DWORD dwUsage +, LPNETRESOURCE lpNetResource, LPHANDLE lphEnum)'); # WNetEnumResource ApiLink('mpr.dll', 'DWORD WNetEnumResource(HANDLE hEnum, LPDWORD lpcCount, LPVOID + lpBuffer, LPDWORD lpBufferSize)'); # WNetCloseEnum ApiLink('mpr.dll', 'DWORD WNetCloseEnum(HANDLE hEnum)'); WNetOpenEnum($dwScope, $dwType, $dwUsage, $lpNetResource, $lphEnum); $hEnum = $lphEnum; my $dwResultEnum = WNetEnumResource($hEnum, $lpcCount, $lpBuffer, $lpB +ufferSize); # // get remote name of resource... lpnrLocal[i].lpRemoteName) do { if($dwResultEnum eq "NO_ERROR") { for (my $i = 0; $i < $lpcCount; $i++) { CleanString( $lpBuffer ); print $lpBuffer->[$i]; } } elsif($dwResultEnum ne "ERROR_NO_MORE_ITEMS") { exit; } } while($dwResultEnum ne "ERROR_NO_MORE_ITEMS"); $lpBuffer = ''; my $dwResult = WNetCloseEnum($hEnum);
      Can anyone tell me what is wrong, literally nothing is returned so im not sure if im using the Win32-API-Prototype module correctly at all.

      Thanks in advance, Jen

Re: Win32-API-Prototype WNet
by Anonymous Monk on Mar 01, 2005 at 18:12 UTC
    Hi,

    This is the progress I have made so far. Still not there and havent been able to get anything to output as yet.

    #!/usr/bin/perl use strict; use Win32::API::Prototype; # NETRESOURCE #ApiLink('kernel.dll', # 'typedef struct _NETRESOURCE(DWORD dwScope, DWORD dwType,DWOR +D dwDisplayType, DWORD dwUsage, LPTSTR lpLocalName, LPTSTR lpRemoteNa +me, LPTSTR lpComment, LPTSTR lpProvider) NETRESOURCE'); # ---unable to parse function definition # WNetOpenEnum ApiLink('kernel32.dll', 'DWORD WNetOpenEnum(DWORD dwScope, DWORD dwType, DWORD dwUsage +, LPNETRESOURCE lpNetResource, LPHANDLE lphEnum)'); # WNetEnumResource ApiLink('kernel32.dll', 'DWORD WNetEnumResource(HANDLE hEnum, LPDWORD lpcCount, LPVOID + lpBuffer, LPDWORD lpBufferSize)'); # WNetCloseEnum ApiLink('kernel32.dll', 'DWORD WNetCloseEnum(HANDLE hEnum)'); # 4th WNetOpenEnum parameter - pointer to NETRESOURCE my $netresource = "NULL"; # assumes the root of the network # 5th WNetOpenEnum parameter - pointer to WNetEnumResource # define WNetEnumResource parameters my $hEnum; # must be returned by WNetOpenEnum my $lpcCount = "-1"; # returns as many entries as possible my $lpBuffer; # points to the buffer that receives +the emuneration results - don't know how to do this my $lpBufferSize = "16384"; # specifies the size of the lpBuffe +r parameter # actual pointer to WNetEnumResource my $WNetEnumResource = WNetEnumResource($hEnum, $lpcCount, $lpBuffer, +$lpBufferSize); # Process WNetEnumOpen to start enumeration of the network WNetOpenEnum("RESOURCE_CONNECTED", "RESOURCETYPE_ANY", 0, $netresource +, $WNetEnumResource); # To continue the enumeration, call WNetEnumResource
      First, you probably want to add use warnings after use strict. This may or may not make some warnings pop out.

      Next, if you look on the home page for Win32::API::Prototype, you'll see that all their examples of using ApiLink all have || die after them. This makes it so that the program will stop executing if it can't link the api call. It will also tell you what line it was on when it died. You can also add an informative message like "could not link WNetCloseEnum". Read all about it with perldoc -f die.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://435438]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found