Hello all, I've been tooling around with building a C++ application using Winelib and I've come across a couple of problems resulting from the use of global constructors. The problems can be reproduced with a simple C++ application which from a constructor calls a method which is resolved through a dll import.
1) If this program is the main program, wine_dll_set_callback has not yet been invoked so the __wine_dll_register call doesn't resolve the import list. The problem is that the constructors of the program are run immediately after the dll is registered which results in calling a non resolved import.
This can be worked around by simply creating a non C++ stub which does a LoadLibrary/GetProcAddress on the real main exe. However I'm wondering if there is a good reason for needing to wait until after the main exe is loaded before being able to resolve imports? I must be missing something from my quick view of the code.
2) The order of running constructor execution and DLL entry point is incorrect.
To show the problem assume you have foo.dll which imports bar.dll
Present order for builtins: Load foo.dll Start to resolve foo.dll dependencies Start to resolve bar.dll dependencies Finsh resolving bar.dll dependencies Run bar.dll C++ constructors Finish resolving foo.dll dependencies Run foo.dll C++ constructors Run bar.dll DLL entry point Run foo.dll DLL entry point
I actually haven't checked if PE images presently have the same problem since I don't know what triggers the ctor execution for a PE image. I assume that it's the running of the DLL entry point, but I don't know for sure. Hopefully I'll find the time at some point this week to whip up a test.
The correct order: Load foo.dll Start to resolve foo.dll dependencies Start to resolve bar.dll dependencies Finsh resolving bar.dll dependencies Run bar.dll C++ constructors Run bar.dll DLL entry point Finish resolving foo.dll dependencies Run foo.dll C++ constructors Run foo.dll DLL entry point
The problem seems reasonably simple enough to resolve but I'm sure that I'm missing something since my patch doesn't work for the present version of wine :(. I think that the problem is that there's an issue with inter DLL dependencies somewhere as it dies in SYSMETRICS_Init with a bad hdc.
I've attached a patch which removes the recursive dep search from MODULE_DllProcessAttach and moves MODULE_DllProcessAttach from LoadLibraryExA into MODULE_LoadLibraryExA. Does this seem like a reasonable approach to take? Any problems with the patch?
Ciao, Peter