MFC static library and external rc(resource) file icon loading problem - c++

I have a problem with loading icons from external resource files in main application. I'll try to explain how application is set right now. The resources of the main application includes external dialog *.rc and appropriate *.h. And linker includes dialog implementation (CDialog/CFormView) which resides in external static library *.lib.
External *.rc has:
IDI_MY_ICON ICON "my_icon.ico"
External *.h has:
#define IDI_MY_ICON 10000
Dialog implementation in static lib *.cpp has:
HICON MyDialog::GetNeededIcon()
{
return AfxGetApp()->LoadIcon(IDI_MY_ICON);
}
I thought that it should reside in the same folder as external *.rc file is. I also tried to place them in the main app folder, but application still doesn't load them. Could someone explain me where my_icon.ico is searched in?
P.S. - Contents of files are only examples here.

AfxGetApp()->LoadIcon(IDI_MY_ICON); tries to load the icon from the current app (exe).
If you want to load it from another module, you will either have to remember the handle returned from LoadLibrary, or call GetModuleHandle to retrieve it again.

Your .rc file is compiled by rc.exe to embed the icon in your executable. rc.exe uses the include path to find the resources. This is specified by either the INCLUDE environment variable or by using /I option to rc.exe. If "my_icon.ico" doesn't work, try moving the file or change the include path.
Clarification:
rc.exe compiles .rc files into .res, but its the linker that does the actual embedding into the executable.

Related

How to specify path for dlls a dll library uses in c++?

I am using a dll library for my c++ code using LoadLibrary.
HINSTANCE h_dll = LoadLibrary("library1.dll");
When I do this and load the functions, library1.dll seems to use a multiple of other dll files, say library2.dll and library3.dll. So for the program to work, I need to put all 3 dll files in the same directory as the exe file.
The problem happens when I try to put the dll files in a separate directory. I can just directly write the path for library1:
HINSTANCE h_dll = LoadLibrary("C:\\mydir\\library1.dll");
But this doesn't make library1.dll search for files inside C:\\mydir and I get error messages saying library2.dll and library3.dll couldn't be found. So for the program to work, I still need to put the other two library files with the exe.
Is there a way to specify the library path from modifying only the code itself and not the system search path? e.g. sending an argument while loading the library? Or is there a way to modify the dll library files so that it could be placed in separate directories? Or maybe I could create another dll to do these?

Dialog resources in a static library

I have a problem with dialog and icon resources in my static library.
I have created a MFC static library with Visual Studio 2008.
I am calling Func() in the static library from Win32 application, It tries to launch a MFC dialog in the static library.
When trying to access the resource I am getting afxCurrentResourceHandle is NULL assertion.
I add this line AFX_MANAGE_STATE(AfxGetStaticModuleState()); in the Func() as the first line. But it didn't help.
I need to use only static library. As per requirement, I should not use dll.
Please help me how to launch a dialog in MFC static library from a non MFC application.
The problem here is that a static library doesn't have an 'associated .res file'. If you are trying to migrate a DLL with resources to a static library, then you will need to also 'export' the resource script (its .rc file plus any associated .rc2 files and other referenced resources) to the client program!
So, just as you would have an #include "module.h" line in the .cpp source(s), you will also need an #include "module.rc" in your program's main .rc file (or, at least, in a file that it includes).
Note: Other fixes that folks have tried, like linking explicitly with extra (pre-compiled) .res files won't work! Although the internal structure of a binary .res file is very similar to any other .obj file, the linker will only ever include one!

Add c++ resource to static lib(Visual studio)

I've used #jeff_t's solution as presented here however i encountered several issues.
I have a lib that holds a resource file and set as an external file, file name is
Other Files\myfile.exe
A quick look onto the rc file reveals the following line:
IDR_RES RES "Other Files\\myfile.exe"
Now, i set a DLL project which lays at the same directory level as the LIB and includes both resource.h(Read only symbol directives #include "../LIBProj/resource.h") and LIB.rc(Compile time directives #include "../LibProj/LibProj.rc")
Compiling the LIB goes smoothly, however, when i try to compile the DLL, which links against LIB, results the following error:
../LibProj/LibProj.rc(52): error RC2135: file not found: Other Files\myfile.exe
I do understand that once the linker tries to access additional includes from the DLL projects, it goes out off scope, how to i fix it?
Edit: if i give an absolute path as follows:
IDR_GNRC_RES_AGENT_DATA RES "C:\\Develop\\Code\\LibProb\\Other Files\\myfile.exe"
it works well
After many hours of desperate tries, i realized the solution was quite simple.
I had to use the following path:
IDR_FILE RES "..\LIBProj\Other_Files\\MyFile.exe"
So for both projects that would work. LibProj wont be affected, because we go to the parent directory, and then return back. and for the DllProj we go to parent directory which holds both Lib & Dll projects, and then enter Lib's directory.

Error compiling after referencing a dll to an application

I have created these projects separately and compiled successfully.
MyApp - MyApp.exe
Mydll - Mydll.dll 'It has s dialoguebox.Simple.
I want io include this dll to the app so that I can instantiate obj and call methods of the dll from the app. This is what I want to do.
Steps so far
I have put the name of lib in
Project Properties->Linker->Input->Addtional Dependencies
Tools->Option->Project and Solution->VC++->Library Files
put the path where the library files were (do I need to put the path of the debug folder where the dll/lib contains or I can copy the dll/lib to different folder and put this path?)
Tools->Option->Project and Solution->VC++->Include files
put the path of the header file of the dll/lib
All this done!
Compiler error
error C2065: 'IDD_DIALOG_CRINV' : undeclared identifier" IDD_DIALOG_CRINV is the dialogbox in the dll
I cannot figure out why this error occurs?is this something related to resource files? Do I need to reference somewhere the resource file?
After fix this, how can I create a instance of the dll and call the messagebox?
Is it something like below?
I create a event for button_click on the application.exe;
void CApp::OnBnClickedCreateDLLDlg(){
// TODO: Add your control notification handler code here
CCReateDLLDlg test;
test.domodal();
}
This error means that your application source file does not have include of an h-file where your constants from the DLL project are defined.

Resources in a static lib file - MFC

MFC is failing to launch my dialog boxes, it seems, because it can't find the resource identifiers. The dialog boxes are in a separate .lib file (so it has a separate .rc file, which, I'm assuming, somehow conflicts with the one in my .exe file). How should I be handling this situation?
In the .rc file for the .exe file, add a line like this:
#include "YourLibResourceFile.rc"
Then, in the .exe's project settings, add an additional include directory to where YourLibResourceFile.rc is, in Resources/Additional Include Directories.
You can't store resources (.rc files contents) in a static library. And since you can have only one "main" .rc file, all other .rc files mst be included in that one using an #include statement, such as explained by Smashery (Edit: Oh! Smashery, you are the OP!).
Make sure all your resource IDs are unique.