Solved

Load resources from file (probably a stupid question)

Posted on 2002-07-24
10
195 Views
Last Modified: 2012-06-27
Hi!

I would like to have separate resources for different types of data, compilation takes too long if I include the data inte the "resource.rc" thingie in visual c++, so I thougt that I should load the data from separate files instead, but then I got stuck, how do I load from a "external" resource file like a dll or somthig and how do I use that resource when loaded and how to include that in a visual c++ project and make it compile that resource only if modified when compiling the source code which ofcource should result in a different binary file after compilation.

and another stupid question, can I store classes in "resource" files so that I get for example: 1 binary for each class and some how include these classes in some kind of load session in the main class?

I think thats about it, if you know something that is relevant to this question that I did'nt know that I needed to know about this please tell me I'm not that experienced with c++ as you probably alread know if you have read this far :)
0
Comment
Question by:maho01
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 350 total points
ID: 7174081
>>how do I load from a "external" resource file like a dll
>>or somthig and how do I use that resource when loaded

That is pretty straight foward, all you have to do is loading the module using 'LoadLibrary()' (will also work for .exe files) and use the handle in the calls that access the resources:

HINSTANCE hResource = LoadLibrary ( "resource.dll");

HICON hIcon = LoadIcon ( hResource, MAKEINTRESOURCE ( ID_THEICON);

BTW: If you are using MFC, 'AfxSetResourceHandle()' is what youare looking for.

>>how do I load from a "external" resource file like a dll
>>or somthig and how do I use that resource when loaded

No, not in resource file, but you can 'store' them in DLLs also. See e.g. http://www.codeguru.com/win32/dyndllclass.shtml

0
 
LVL 1

Author Comment

by:maho01
ID: 7174100
Sounds really great I did'nt this it would be hard, I'll try this when, I get home from work, is there anything that I coul'd do wrong it does'nt look to difficult, this MFC do I wan't to use that, Last time I worked in c++ I did'nt really like MFC well the name it self says pretti much everything Microsoft Foundation Classes... is this the same **** as WFC?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7174116
>>... is this the same **** as WFC?

No, it's different 's**t' :o)
0
 
LVL 1

Expert Comment

by:ris
ID: 7174747
>>>> and another stupid question, can I store classes in "resource" files so that I get for example: 1 binary for each class and some how include these classes in some kind of load session in the main class?

You can't put a class in a "resource" file like a bitmap image, but you can put all your classes in separate DLLs.  I want to warn you against that approach though.  I used to try that approach, but I found that most of my classes called functions in each other, and that created an insane dependancy relationship between all of the separate DLLs, making it impossible to build my project when 3 separate DLLs were all implicitly linked to each other.  Consider when you "rebuild all" in your workspace and you have 2 DLLs that are implicitly linked to each other, lets call them one.dll and two.dll: the compiler attempts to build one.dll and can't because "cannot find link library two.lib" then it tries to build two.dll and can't because "cannot find link library one.lib".

So all you can do with it at that point is explicitly link your DLLs together, which requires a lot of manual coding to load the libraries and class functions in your code, or you have to do some tricky compiler work to force output even when there are errors and then build at least twice every time you do a build.

But if your classes can stand alone without calling functions in other parts of your project, then it is actually a very good idea to put them in their own DLLs.  That way they are easily reused in other projects and easily maintained if you improve just one class, you can upgrade your old project by replacing one DLL instead of recompiling the whole thing.

I hope I didn't go too far over your head with those comments.  Let me know if anything is unclear, confusing, or incomplete and I will be happy to explain it in more detail.
0
 
LVL 1

Author Comment

by:maho01
ID: 7178185
The genius is back ;) another stupid question what would be the easiest way to create a file(know as resource.dll above) containing all my resources that I want in another file than the application would it be to simply "compile xxx.rc" or is there any other way?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Expert Comment

by:jkr
ID: 7178201
YOu mean a "resource-only" DLL? Just create a Win32 DLL project that contains nothing but a .rc file...
0
 
LVL 1

Expert Comment

by:ris
ID: 7178277
You have to have a DllMain too, don't you?  I could be wrong about that, but I think if you try to compile xxx.rc by itself into a DLL, you get a linker error "cannot find function: int DllMain(HANDLE, DWORD, LPVOID)" or something like that...

If you need it, this is the simplest possible DllMain code:

#include <windows.h> //for typedefs, etc.

BOOL DllMain(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved)
{
  return TRUE;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 7178299
>>You have to have a DllMain too, don't you?

No, not necessarily - I just checked that with one of our resource-only DLLs.
0
 
LVL 1

Author Comment

by:maho01
ID: 7178343
Works great, thanks.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7178348
Tahnk you! Completely my pleasure :o)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now