Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

a use for Win32::LoadLibrary()

by bulk88 (Priest)
on Oct 10, 2011 at 03:42 UTC ( #930527=perlmeditation: print w/replies, xml ) Need Help??

Ever wondered what on earth can you do with Win32's LoadLibrary on Windows Perl? This isn't C of course.

Reading GetProcAddress's documentation is funnier, and the same applies for LoadLibrary right?

Returns the address of a function inside a loaded library. The information about what you can do with this address has been lost in the mist of time. Use the Win32::API module instead of this deprecated function.

What can you do with LoadLibrary in Perl? DLL redirection and versioning!!

Lets say I have an XS module that has psapi.dll in its PE Import Table, but XS module doesn't have a manifest file. I have a psapi.dll (a fictional example) from Windows 2000 or ReactOS/Wine that I want to test my XS module with, but I run XP. I am not going to replace the psapi.dll in System32. What to do? Let me put the custom psapi.dll in the perl bin dir and make the perl.exe.local file in perl bin dir. Using Dependency Walker or Visual Studio I see it loaded the system32's psapi.dll after Dynaloader's LoadLibrary of the XS DLL (with _boot*). What happened? SxS happened. Any modern Perl VC compiled has a manifest file in perl.exe to turn on XP themed (ver 6) Comctl32.dll, self compiled and ActivePerl. Not sure about Mingw Perls. .local dll redirection is disabled if the exe has a manifest file (see ). ActivePerl 5.12 has a manifest. ActivePerl 5.10 doesn't. SxS is a nightmare. I gave up trying to use SxS to load my old psapi.dll.

There is a solution.
use Win32; BEGIN { print "PSAPI HMODULE is ".Win32::LoadLibrary('C:\Perl\psapi.dll'). +"\n"; } use MyPsapi;
MyPsapi.dll (XS dll) will now load and use the old psapi.dll even thought .local doesn't work. Since this works, I figured the design of Windows DLL loader is as follows, first check the already loaded DLL list, do name match based on filename minus extension, if matched, increase ref count on the DLL and returns existing HMODULE, else pass to SxS/Path search system. So there you go, a valid use for Win32::LoadLibrary in Perl, in 2011.

This solution worked perfectly for me to load old Microsoft dlls in XP for backwards compatibility test with a manifested perl that disallows .local overriding.

Replies are listed 'Best First'.
Re: a use for Win32::LoadLibrary()
by sundialsvc4 (Abbot) on Oct 12, 2011 at 21:32 UTC

    I, for one, have never understood why Windows still has such a primitive way of supporting libraries ... no versioning.

      I'm a strict Unixian, but apparently Windows supports some form of library versioning since Windows XP. According to this page, true DLL numbering even became standard in Visual Studio 2010 (better late than never).

      Those who don't understand UNIX are condemned to reinvent it, poorly. -- Henry Spencer


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Are .so files versioned?

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://930527]
Front-paged by keszler
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2021-04-18 00:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found