Rundll32 load order problem - c++

My product consists of two dlls (A.dll and B.dll for clarity), A.dll depends on B.dll. Both A and B dlls are in the same folder (say c:\app). At the same time old version of B.dll is in Windows\System32 folder. When I try to run following command from command prompt (current folder is c:\app):
rundll32.exe "c:\app\A.dll",DoWork
I receive error because rundll32 uses old version of B.dll from System32 folder. I tried to use SetDllDirectory API from DllMain function of A.dll library to add c:\app folder to the search path but it doesn't work for me.
I can't find any useful and complete information about rundll32 internals or any information about dll loading order.
Is it possible to execute rundll32 successfuly in this deployment configuration? (I mean load new B.dll version from c:\app folder).

DLL Hell on SO
Well, it's kind of cool in a retro sort of way. Here is a thought: try copying rundll32.exe into the same folder as the new dll's and your product, and run it from there. It might work...

I'd get rid of the old Dll, or use another name for the new dll. Having two versions of something that is supposed to do the same, and giving it the same name, is likely to cause havoc.

Related

Error when launching application

I have rebuilt the QGIS code on VC2008 on Windows 7.
I have all the correct DLLs in the folder of the exe, and can see that it knows where the QT DLLs are.
When I launch the app, I get this error:
The procedure entry point ??4QPixmap##QAEAAV0#$$QAV0##Z could not be
located in the dynamic link library QtGui4.dll
I'm not sure where to start to get this working. I checked the QtGUI4.dll to see that it is loading, with a dummy app. It seems fine.
Any help would be great.
Posting it as an answer since my hunch turned out correct:
This error shows that your application was loading the wrong Qt DLLs. For whatever reason, it was finding and loading DLLs different from the DLLs against which it was built. In your case it turned out it was finding Qt4 built for VS2010 DLLs, while it needed Qt4 built for VS2008.
The solution is to make sure it always finds the correct DLLs - place the correct DLLs in the EXE's folder, as this is usually the first place it looks for for DLLs. On Windows, it's not a good idea to rely on %PATH% or on the current directory.

rename dll file while making build in VC++

Am working on VC++ application that generates an exe file and dll file with same name.
Now i want to change the dll filename for purpose.
I tried it changing in Project-Properties but still no luck.
Application is giving the renamed dll while i build.
But if i run the exe it showing the error "The Program cant start, dll name is missing"
Please tell me how to generate the dll with a different name.
A DLL must be in the path of the executable, so first check if your path is setup correctly or copy the DLL into the directory where your EXE is.
If you link against a DLL, the name of the DLL may not change, as it will be used for reference. If you want to be able to rename the DLL as you whish you must either load the DLL dynamically, then you can load a DLL from wherever you want. Or you must link against the renamed DLL.

How can I automatically load DLLs from a subdirectory?

In Visual Studio, you create a .dll project and it creates a .dll and .lib files. You link statically to the .lib, and if the .dll is in the same folder as the .exe, everything works.
I suspect that everything would also work if the .dll was in System32 or any other PATH folder (confirm or correct, please).
But here's the question: I want my exe to find the .dll in ./DLLS/ folder, that is, if my exe is in ....../MyApp/MyApp.exe then it should look for the .dll in ...../MyApp/DLLS/MyDll.dll. I DO NOT want to include the DLLS folder in path. Is there any way I can do this?
Please note that I do not wish to use LoadLibrary explicitly, so I can't specify the path there.
Thanks in advance for any help.
Here is the default sequence which Win32 applications go through when looking for a DLL:
http://msdn.microsoft.com/en-us/library/7d83bc18(VS.80).aspx
So according to this, another approach might be to call SetCurrentDirectory or SetDllDirectory. But in order for this to work you have to use the Delay Loaded Library functionality (you specify that in Project Settings in Visual Studio). Delay loaded library means that the DLL is loaded only at the moment when the program needs it, not automatically at the programs' startup.
You could use SetDllDirectory for this. The loader will use the additional directory you specify when loading libraries. There can only be one additional directory, however, so you need to make sure that there aren't other calls to this at a later point in your application, otherwise the directory you specify will be ignored.
If that API does not allow relative directories (I'm not sure), you could always call GetModuleFileName with a NULL first parameter to get the file name of the currently executing program. With a little string manipulation, you can get the absolute path to your DLLs folder.

Infernal Libraries (aka DLL Hell)

In a Project of mine, I use a Delphi Application which dynamically loads a wrapper DLL (exporting C-Style functions) which in turn is statically link against a bunch of 3rd party DLLs.
It works fine on my test machines, but on my customers computer it failed to initialize with an error Message like "Couldn't find entrypoint _somefunction#4AKKZ in TMYlibrary.dll".
After some investigation with sysinternal's process monitor, I realized that Windows would look fror DLLs in windows/sytem32 first, so if a DLL named similar to my DLL was present in system32, windows would pick that one and try to find my function entry points in it - which would fail.
Do you know of a possiblity to change windows' DLL the searching behaviour?
Additional Information
[Update] The .exe file is located on the top level of the application's folder tree.
The Wrapper and the 3rd-party-DLLs ar e both located in the Subfolder /bin of my apps Folder
Dev platform is windows XP/7, using VS2008 for the dlll and Delphi 2010 for the application
I found another solution myself:
SetDllDirectory adds an additional search path to the list of locations to look at.
From http://msdn.microsoft.com/en-us/library/ms686203%28v=VS.85%29.aspx
After calling SetDllDirectory, the DLL search path is:
The directory from which the application loaded.
The directory specified by the lpPathName parameter.
The system directory. Use the GetSystemDirectory function to get the
path of this directory. The name of
this directory is System32.
The 16-bit system directory. There is no function that obtains the
path of this directory, but it is
searched. The name of this directory
is System.
The Windows directory. Use the GetWindowsDirectory function to get
the path of this directory.
The directories that are listed in the PATH environment variable.
(maybe i should do my googling before I post on SO ;)
Ship the DLL in your program's folder. (same as the exe file).
Then Windows should try your version first.

"The application has failed to start because cxcore210.dll was not found". Why would this happen?

I'm having trouble running basic opencv scripts on my new machine. I've installed opencv2.1. I am linking these files from the linker:
C:\OpenCV2.1\lib\cv210.lib
C:\OpenCV2.1\lib\cvaux210.lib
C:\OpenCV2.1\lib\cxcore210.lib
C:\OpenCV2.1\lib\highgui210.lib
I run the program, it compiles. However, it gives me the error stated above. Why is that? I tried linking to the dll it describes, and it doesn't like that either.
The DLL needs to be somewhere in the DLL search path, otherwise the loader won't be able to find it.
The two easiest options are to copy the DLL(s) to the directory in which your executable is located or the directory from which you are running your executable, or to add the directory in which the DLL(s) are located to the PATH.
this video will help you..
http://www.youtube.com/watch?v=9nPpa_WiArI
Or else I came up with another solution just copy the dll files directly into C:\Windows\SysWOW64 folder (this is my system folder). This helped me and my code was executed.
I had the same problem:
I configure correctly the properties of my project, like 100 tutorials says: But never work, so i try copy the dll to the application directory and works. I dont know why the linker dont see the dlls
I just copy the cv210.lib ,cvaux210.lib, cxcore210.lib ,highgui210.lib