log4cplus cannot find AppenderFactory - c++

I regularly use log4cplus, but I have encountered a new problem.
I have a windows application which uses a dll (LoadLibrary)
They are built on different compilers, but use dlls with the same name (including log4cplus.dll) also built on different compilers. The app and all the dlls it uses are built in one environment (vs2008). The dll and all the dlls it uses are built on another environment (vs2013).
LoadLibrary failed until I changed the application to pass the LOAD_WITH_ALTERED_SEARCH_PATH flag to LoadLibraryEx, which appears to allow the dll to load its own dependencies successfully, except now I get these runtime errors
log4cplus:ERROR PropertyConfigurator::configureAppenders()- Cannot find AppenderFactory: log4cplus::RollingFileAppender
log4cplus:ERROR PropertyConfigurator::configureAppenders()- Cannot find AppenderFactory: log4cplus::ConsoleAppender
log4cplus:ERROR PropertyConfigurator::configureLogger()- Invalid appender: ROLLING
Logging works for all applications built in either environment.
Logging also works for this app and dll both built in the same environment.
I changed the dll to statically link log4cplusS.lib, but I still get the same errors.

First off, use the same compiler for everything. It is basically impossible to make things work when using different compiler versions. Once you are compiling everything with the same compiler, try to solve other problems, if any still remain.

Related

Q: Proper way to access data in a data only dll file

My application store some data in data only dll files. Those dll files are loaded with LoadLibrary() when needed at runtime and then discarded with FreeLibrary() after finish using them. The main application access the data stored in the dll files using GetProcAddress(). The program is written in C++ and uses WinAPI calls, no MFC or other libraries. It has two versions x64 and x86. It works fine on most systems. My dll files do not call other libraries or depend on anything else. Each is a stand alone file.
Recently, I discovered the program does not work on one machine. this specific one has Windows 10 x64 installed on it. After investigations I found the following:
LoadLibrary() fails with error message "Could not find module". The dll is in same directory with main program.
I replaced the call to LoadLibrary() with LoadLibraryEx() and tried the following falgs:
LOAD_IGNORE_CODE_AUTHZ_LEVEL did not work. The dll could not be loaded.
DONT_RESOLVE_DLL_REFERENCES ... works?? But, Microsoft strongly recommends not to use it.
LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE ... loading the dll succeeds?? But, the call to GetProcessAddress later fails. the program could not access the data in the dll file. So this is not actually working.
I could not find anything wrong with this machine. All other programs are working fine.
I tried the x86 version on this machine and it worked fine using original LoadLibrary().
My installer is dual system and automatically installs x64 version when it finds x64 windows. Normal user can not simply switch to x86 when he gets such error.
My question is how can I eliminate this error and make my program works on any machine:
Should I call LoadLibraryEx() using DONT_RESOLVE_DLL_REFERENCES flag and ignore Microsoft warning?
Is there any way I can get my library loaded with simple call to LoadLibrary()?
If I call LoadLibraryEx() with LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE flag, as recommended by Microsoft, how can I access the data without calling GetProcessAddress()?
Thank you in advance.

Build C++ dll for Release and Debug

So I'm getting into c++ and I recently built a logger class ie print any type to the screen and/or to a log file. I built it as a dll and the output files are
console.h
console.lib
console.dll
I then imported these files as additional includes to another project and it works, only in debug mode (in which it was built) I wish for it to work regardless of build config. How can I accomplish this. An example, I recently used the GLFW library and can build in both how was it compiled for this to work.
If I correctly understood you're trying to link same version of your lib/dll with both debug and release configs of your app.
In general case you need two versions of your lib/dll files, debug and release, and link with the one that matches your application configuration, so for debug config of you app link with debug config of your lib/dll, and release with release.
The most possible problem of using mixed configs (like debug dll with release exe) is allocating memory in one domain and releasing it in another.
EDIT:
To elaborate, the problem can be allocating memory in your DLL and releasing it in your EXE, or vise versa. This doesn't work, at least with VS C-Runtime. For more details, see: https://stackoverflow.com/a/45806858/453271

Load a DLL from another directory at program start

My basic issue is this: my program (MyProgram.exe) has a dependency on a DLL from another program (OtherProgram), and I'm trying to avoid repackaging a new DLL every time OtherProgram updates. I'd like to have MyProgram.exe link in OtherProgram's DLL when it launches, but I'm not completely sure that Windows allows for this. So if there is some kind of workaround that would also be acceptable.
And just for some background, the platform is Windows 7 x64, and MyProgram.exe runs fine when I create a symlink in the MyProgram.exe project directory to the DLL in OtherProgram's install directory. When I try to run it without the symlink, I get the "program can't start because OtherProgramDLL.dll is missing from your computer" error.
Any advice or links to relevant info is greatly appreciated!
EDIT: Clarification: the DLL is not linked at compile-time, this issue crops up at runtime
There are two types of dynamic linking in the Windows world:
Load-Time linking is when a DLL is loaded automatically when your program starts up. Windows finds this DLL using a specific algorithm I'll discuss below.
Run-Time linking is when you specifically load a DLL by calling LoadLibrary in your code. Similar rules apply as to how the library is found, but you can specify a fully-qualified or relatively-qualified path to control the search.
In the case of Load-Time linking, MS recommends that your program's DLLs are stored in and loaded from the same directory where your application is loaded from. If this is at all workable, this is probably your best option.
If that doesn't work, there are several other options, outlined here. One is to leverage the search order by putting the DLL in either the working directory or the directory where the application was loaded from.
You can change the working directory of an application by:
Create a shortcut to your application.
Bring up the shortcut's properties
Edit the "Start in" property with the directory where the DLL is located.
When you launch your application using the shortcut, it will load the right DLL.
Other options for load-time linking include:
Adding a manifest to your application which specifies where your dependent assemblies are, or,
Setting the PATH.
You could use LoadLibrary, but you would need a way to guarantee the DLL's location. This Wikipedia article provides good example on how to use the DLL after it has been loaded.
You can add the directory where the dll is located to the PATH environment variable.
I have struggled with the same problem and also found a dead end with the suggested methods like LoadLibrary, SetDllDirectory, Qt's addLibraryPath and others. Regardless of what I tried, the problem still remained that the application checked the libraries (and didn't find them) before actually running the code, so any code solution was bound to fail.
I almost got desperate, but then discovered an extremely easy approach which might also be helpful in cases like yours: Use a batch file! (or a similar loader before the actual application)
A Windows batch file for such a purpose could look like this:
#echo off
PATH=%PATH%;<PATH_TO_YOUR_LIB>
<PATH_TO_YOUR_APP_EXE>
/edit: Just saw #SirDarius comment in Luchian's answer which describes that way, so just take my batch code bit as a reference and all credits go to him.
I have the same problem with one application I am working on.
I do not want to use runtime loading because there are tens of functions I would need to manually create function pointer for.
Mr Dibling's mention of manifest file opened a new door for me but I sadly found out that the oldest version of windows that supports the feature is Windows 7. It won't even work on Vista.
Long story short, a friend familiar with Windows Application development told me to look up Delay-Loaded DLL, which turns out to solve the problem perfectly with minimal effort. It delays the loading of DLL library to either the point you manually do, or the first time its function is called. So you just need to add your DLL path to the search path before that happens, where SetDllDirectory helps.
Here is the steps to make it work:
1) Specify the DLL to be delay-loaded to linker, either through your makefile, cmake or VS property page (Linker->Input of VS2015)
2) Call SetDllDirectory at the beginning of your program, before any call to the DLL is made.
Delay-loaded DLL is supported all the way back to VC6.
SetDllDirectory is supported after XP SP1.
Use Symbolic Links to the 3rd Party Executables
I found the approach advocated by Aaron Margosis useful. See:
Using NTFS Junctions to Fix Application Compatibility Issues on 64-bit Editions of Windows
Essentially, create symbolic links to each of the dependent 3rd Party executables. Place these symbolic link files in and amongst your own dependent executable files. Except for filename changes to the targets, the 'soft' symbolic links will resolve the load-time dependencies even as the target of the links are changed by future updates.

JNI system.loadlibrary problem - native C++ dll using C library

I have a pretty annoying compiling problem.
I am trying to do a System.loadlibrary on a C++ DLL in VS2010, which in turns uses a C DLL compiled in VS2008.
The error I am getting is:
java.lang.UnsatisfiedLinkError: The application has failed to start because
its side-by-side configuration is incorrect
This occurs whenever my C++ DLL tries to do a call to a function in the C DLL. Both compiles just fine, and both are in a folder accessible by Java Applet.
Is this possible to solve somehow? Do you need any more info?
You should try and use the dependency walker on your DLLs.
From the side-by-side error I assume that one of the c runtime redist packages or one of the noredist packages is missing.
But the dep walker should show you this.
If this does not help you can get some more information from the event log of Windows since side by side errors are logged there, or you need to use sxstrace for more info.
Have you installed the runtimes of VC2010 and VC2008?
Additionally you have to make sure the manifests of both DLLs are correctly configured so that the correct version of the used library can be loaded.
See also: http://msdn.microsoft.com/en-us/library/ms235342.aspx

C++ builder how to configure compiler to output just exe?

the question is how to configure in c++ builder 2010 compiler and debug option to output just one exe file, and all the other inside of that, so that i can easily use program on other maschine without installing them, with just runing exe file.
With all versions of C++Builder you don't need to make an installer for this, (although inno setup is simply brilliant if you do require one).
Just select the following project options:-
Project/Packages:, Build with Runtime Packages = DISABLED
C++/Linker: Dynamic RTL = FALSE
That's it. You will get a single exe with no dependencies (apart from any 3rd party DLL's you use. All your VCL components (including third party ones) will get statically linked.
I use this mode for all production builds (although I do then use Inno Setup installer to manage the install/removal process for customers).
In addition to Roddy's answer:
Do not forget to disable CodeGuard (Project->Options->Codeguard) or your program will fail on any machine that does not has the CodeGuard DLL's installed !