in reply to Win32::API help

That pack is definitely wrong. $dwReserved should be simply 8.

The signature is completely wrong

HRESULT -> N or I, not sure URLDownloadToFile( LPUNKNOWN pCaller, -> P LPCTSTR szURL, -> P LPCTSTR szFileName, -> P DWORD dwReserved, -> N LPBINDSTATUSCALLBACK lpfnCB -> P );
Win32::API->Import( 'urlmon', 'URLDownloadToFile', 'PPPNP', 'N', ); my $pCaller = 0; my $szURL = 'http://somedomain.com/pic.jpg'; my $szFileName = 'c:\1img.jpg'; my $dwReserved = 8; # Download from offline cache my $lpfnCB = 0; my $res = URLDownloadToFile( $pCaller, $szURL, $szFileName, $dwReserved, $lpfnCB, ) or die Win32::FormatMessage(Win32::GetLastError);

If that doesn't work, also make sure the DLL function was compiled using the __stdcall (aka WINAPI) calling convention. Win32::API can't call it properly if it wasn't.

HRESULT WINAPI URLDownloadToFile( LPUNKNOWN pCaller, LPCTSTR szURL, LPCTSTR szFileName, DWORD dwReserved, LPBINDSTATUSCALLBACK lpfnCB );

Update: Replaced undef with $pCaller and $lpfnCB.

Replies are listed 'Best First'.
Re^2: Win32::API help
by BrowserUk (Patriarch) on Nov 07, 2006 at 02:06 UTC

    That won't work. The result is a trap (protection violation), because Win32::API doesn't know how to translate undef into a C-type null. This is 5.8.8, but you get the same result from 5.8.6:

    C:\test>\AS817\perl\bin\perl5.8.8.exe -Mlib=\perl\site\lib -MWin32::A +PI -mstrict -w Win32::API->Import( 'urlmon', 'URLDownloadToFile', 'PPPNP', 'N', ); my $pCaller = 0; my $szURL = 'http://somedomain.com/pic.jpg'; my $szFileName = 'c:\1img.jpg'; my $dwReserved = 8; # Download from offline cache my $lpfnCB = 0; my $res = URLDownloadToFile( undef, $szURL, $szFileName, $dwReserved, undef, ) or die Win32::FormatMessage(Win32::GetLastError); ^Z Use of uninitialized value in subroutine entry at (eval 2) line 2, <DA +TA> line 164. Use of uninitialized value in subroutine entry at (eval 2) line 2, <DA +TA> line 164.

    This way doesn't result in a trap, though I've failed to make it download any file. Possibly because 0x8 means fetch it from the cache, but as I don't use IE, it won't find it there.

    C:\test>perl -Mlib=\perl\site\lib -MWin32::API -mstrict -w Win32::API->Import( 'urlmon', 'URLDownloadToFile', 'PPPNP', 'N', ); printf "%08x\n", URLDownloadToFile( 0, 'http://images.google.com/intl/en/images/images_res.gif', 'google.gif', 8, 0, ) or die $^E; ^Z 800c0005

    The return code 0x800c005 apparently means file or server not found.

    The following codes are other common error codes: - 80100003. During install, one or more files are missing from downlo +ad folder. - 800bxxxx. Anything starting with 800b is a trust failure (for examp +le, 800b010b - trust check failed). - 800Cxxxx. Anything starting with 800C is a Urlmon failure (for exam +ple, 800C0005 - file or server not found, 800C000B - connection timeout). - 80040004. User canceled.

    Which doesn't make a great deal of sense as my browser can fetch the gif above, but at least the call has successfully been made without trapping.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      oops, I meant to put zero (well, $pCaller and $lpfnCB, which I set to zero). Thanks.
Re^2: Win32::API help
by pKai (Priest) on Nov 07, 2006 at 11:53 UTC
    The signature is completely wrong

    Well, not completely I would say.
    Changing the D -> N for the dwReserved (nice catch), does the trick for me on the formerly crashing (as in system/OS popup) 5.8.8.

    As for the 2 pointers (1st and 5th argument). Using P is O. K. of course, but since these have to be given as Null anyway, going whith longs (N) is fine too, with the additional convenience to choose between either 0 or undef as a valid initialization value.

    It's a bit confusing that all 5.8.6 installations I tried were far more forgiving and ran the OP code successfully for me.