John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I made several calls to a few Win32 primitives in this file, and all went well. Until I get to this one: The first returns success, but the second complains that the handle is invalid.

Am I importing them wrong, passing it wrong, or what?

###### ## try WNetOpenEnum et.al. to find connections w/o letters. my $WNetOpenEnum= new Win32::API ("mpr", "WNetOpenEnum", "IIIIP", "N") + or die; $handle= 'xxxx'; # allocate 4 bytes use constant RESOURCE_CONNECTED => 0x00000001; use constant RESOURCETYPE_DISK => 0x00000001; $result= $WNetOpenEnum->Call (RESOURCE_CONNECTED, RESOURCETYPE_DISK, 0 +, 0, $handle); print "result for WNetOpenEnum is $result\n"; my $WNetEnumResource= new Win32::API ("mpr", "WNetEnumResource", "PPPP +", "I") or die; my $count= pack ("V", -1); my $bufsize= 5000; $buffer= 'x' x (1+$bufsize); for (;;) { $bufsize= pack ("V", 5000); $result= $WNetEnumResource->Call ($handle, $count, $buffer, $bufsiz +e); print "result for WNetEnumResource is $result\n"; last if $result != 0; print "count=$count, bufsize=$bufsize\n"; print $buffer; # just to see if it's not all x's. }

Replies are listed 'Best First'.
Re: Passing/Returning HANDLE's with Win32::API
by Corion (Patriarch) on Feb 07, 2005 at 22:56 UTC

    I believe your declaration for WNetEnumResource is wrong: The MSDN declares it as

    DWORD WNetEnumResource( HANDLE hEnum, LPDWORD lpcCount, LPVOID lpBuffer, LPDWORD lpBufferSize );

    which I interpret as LPPP and not as PPPP. Try it with the following code:

    my $WNetEnumResource= new Win32::API ("mpr", "WNetEnumResource", "VPPP +", "I") or die; my $count= pack ("V", -1); my $bufsize= 5000; $buffer= 'x' x (1+$bufsize); for (;;) { $bufsize= pack ("V", 5000); $result= $WNetEnumResource->Call ($handle, $count, $buffer, $bufsiz +e); print "result for WNetEnumResource is $result\n"; last if $result != 0; print "count=$count, bufsize=$bufsize\n"; print $buffer; # just to see if it's not all x's. }

    Updated: The prototype should have been LPPP, and not VPPP

      Then it tells me that the argument is not numeric. The HANDLE received out of the first function is a 4-byte opaque cookie.

        Treat this opaque cookie as a number, that is, unpack it, and then pass it in again:

        $handle = unpack "V", $handle; ....