Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: [OT][Win32] How to load the right dll

by BrowserUk (Pope)
on Mar 20, 2012 at 02:41 UTC ( #960511=note: print w/ replies, xml ) Need Help??


in reply to [OT][Win32] How to load the right dll

The MS solution to your problem is called side-by-side assemblies.

In a nutshell, it is the "manifest file" mechanism that we all know (and hate:). You need to add the name of the dependant dll to the manifests embedded within the dlls dependant upon it.

There is some (completely opaque) discussion about manifests here. And some reference material for the mt.exe tool here.

Whether Mt.exe work with Mingw-built dlls? Probably, but I don't know for sure.


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.

The start of some sanity?


Comment on Re: [OT][Win32] How to load the right dll
Re^2: [OT][Win32] How to load the right dll
by syphilis (Canon) on Mar 20, 2012 at 10:47 UTC
    Thanks for the reply and the links.

    Whether Mt.exe work with Mingw-built dlls? Probably, but I don't know for sure

    I think it works ok - though so far all I've established by using it is that none of these dll's have resource sections.

    I don't know why I didn't realize it sooner, but not long after I posted here it occurred to me that I could frame my questions in such a way that it would not be OT on the mingw64 mailing list - so I posted there, too.
    The same approach has been suggested there.

    It was also suggested that replacing the problem libgcc_s_sjlj-1.dll with the one from the 4.7.0 compiler is probably not the disaster-just-waiting-to-happen that I was worried about ... so there's the fallback solution :-)

    Cheers,
    Rob

      I made this work once a couple or so years ago, but I've forgotten something.

      Four files: main.exe link-time linked to a.dll; runtime links to dll\b.dll which is link-time linked to dll\a.dll. The a.dlls differ.

      The sources:

      #include <stdio.h> #define ISOLATION_AWARE_ENABLED 1 #include <windows.h> __declspec(dllimport) int funca( int i ); int main( int argc, char **argv ) { char buf[ 100 ]; HMODULE hb = LoadLibrary( "dll\\b.dll" ); int (*b)(int) = GetProcAddress( hb, "funcb" ); printf( "hmodule: %x *funcb:%x \n", hb, b ); funca( 1 ); printf( "Back in main after calling locally bound funca\n"); gets( + buf ); b( 2 ); return 0; } /* a.dll (v1)*/ #include <stdio.h> __declspec(dllexport) int _stdcall funca( int i ) { return printf( "a.dll{v1.0.0.0}:funca() called with %d\n", i ); } /* b.dll */ #include <stdio.h> __declspec(dllexport) int funcb( int i ) { printf( "b.dll:funcb() called with %d\n", i ); funca( i + 1 ); return 1; } /* a.dll (v2)*/ #include <stdio.h> __declspec(dllexport) int funca( int i ) { return printf( "a.dll{v2.0.0.0}:funca() called with %d\n", i ); }

      And these (handwritten) manifest files for the two a.dlls:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1. +0"> <assemblyIdentity type="win32" name="a.dll" version="1.0.0.0" /> <description></description> </assembly>
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1. +0"> <assemblyIdentity type="win32" name="a.dll" version="2.0.0.0" /> <description></description> </assembly>

      The other two are generated by the linker; and are attached to their executables using mt.exe)

      And then build them all using this:

      @rem makeit.cmd del /S /Q *.obj *.lib *.dll *.exe cd dll cl /MT /LD a.c mt -manifest a.dll.manifest -outputresource:a.dll;#1 cl /MT /LD b.c a.lib /link /manifest "/manifestdependency:type='win32' + name='a.dll' version='2.0.0.0'" mt -manifest b.dll.manifest -outputresource:b.dll;#1 cd .. cl /MT /LD a.c mt -manifest a.dll.manifest -outputresource:a.dll;#1 cl /MT main.c a.lib /link /manifest "/manifestdependency:type='win32' +name='a.dll' version='1.0.0.0'" mt -manifest main.exe.manifest -outputresource:main.exe;#1

      like so:

      C:\test\manifest>makeit C:\test\manifest>del /S /Q *.obj *.lib *.dll *.exe Deleted file - C:\test\manifest\a.obj Deleted file - C:\test\manifest\main.obj Deleted file - C:\test\manifest\a.lib Deleted file - C:\test\manifest\a.dll Deleted file - C:\test\manifest\main.exe Deleted file - C:\test\manifest\dll\a.obj Deleted file - C:\test\manifest\dll\b.obj Deleted file - C:\test\manifest\dll\a.lib Deleted file - C:\test\manifest\dll\b.lib Deleted file - C:\test\manifest\dll\a.dll Deleted file - C:\test\manifest\dll\b.dll C:\test\manifest>cd dll C:\test\manifest\dll>cl /MT /LD a.c Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. a.c Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:a.dll /dll /implib:a.lib a.obj Creating library a.lib and object a.exp C:\test\manifest\dll>mt -manifest a.dll.manifest -outputresource:a.dll +;#1 Microsoft (R) Manifest Tool version 5.2.3790.2075 Copyright (c) Microsoft Corporation 2005. All rights reserved. C:\test\manifest\dll>cl /MT /LD b.c a.lib /link /manifest "/manifestde +pendency:type='win32' name='a.dll' version='2.0.0.0'" Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. b.c Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:b.dll /dll /implib:b.lib /manifest "/manifestdependency:type='win32' name='a.dll' version='2.0.0.0'" b.obj a.lib Creating library b.lib and object b.exp C:\test\manifest\dll>mt -manifest b.dll.manifest -outputresource:b.dll +;#1 Microsoft (R) Manifest Tool version 5.2.3790.2075 Copyright (c) Microsoft Corporation 2005. All rights reserved. C:\test\manifest\dll>cd .. C:\test\manifest>cl /MT /LD a.c Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. a.c Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:a.dll /dll /implib:a.lib a.obj Creating library a.lib and object a.exp C:\test\manifest>mt -manifest a.dll.manifest -outputresource:a.dll;#1 Microsoft (R) Manifest Tool version 5.2.3790.2075 Copyright (c) Microsoft Corporation 2005. All rights reserved. C:\test\manifest>cl /MT main.c a.lib /link /manifest "/manifestdepende +ncy:type='win32' name='a.dll' version='1.0.0.0'" Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64 Copyright (C) Microsoft Corporation. All rights reserved. main.c main.c(11) : warning C4113: 'FARPROC' differs in parameter lists from +'int (__cdecl *)(int)' main.c(11) : warning C4133: 'initializing' : incompatible types - from + 'FARPROC' to 'int (__cdecl *)(int)' Microsoft (R) Incremental Linker Version 9.00.21022.08 Copyright (C) Microsoft Corporation. All rights reserved. /out:main.exe /manifest "/manifestdependency:type='win32' name='a.dll' version='1.0.0.0'" main.obj a.lib C:\test\manifest>mt -manifest main.exe.manifest -outputresource:main.e +xe;#1 Microsoft (R) Manifest Tool version 5.2.3790.2075 Copyright (c) Microsoft Corporation 2005. All rights reserved.

      The run the exe and it 'works' in very way ... except the crucial one of dynamically lining to the right a.dll from b.dll:

      C:\test\manifest>main hmodule: 640000 *funcb:641000 a.dll{v1.0.0.0}:funca() called with 1 Back in main after calling locally bound funca b.dll:funcb() called with 2 a.dll{v1.0.0.0}:funca() called with 3

      Which is a crap load of work to achieve the same result as you'd get without the manifest files, but I do remember when I first tried this I had to read gobloads of information and tear my hair out for several days to reach this point. So this might help you get started...

      The solution is something to do with "activation contexts", but I'm to tired to wrap my brain around it right now. Maybe tomorrow.

      HTH


      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.

      The start of some sanity?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-07-14 03:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (254 votes), past polls