in reply to Re^2: [OT][Win32] How to load the right dll
in thread [OT][Win32] How to load the right dll

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?