Board index » cppbuilder » How using a resource file

How using a resource file


2004-05-25 11:17:23 PM
cppbuilder19
I have create a bitmap inside a resource file (test.res) by the Borland's
Resource Editor. My bitmap as for name 'Bitmap1'.
Then for changing an Image from my TImage I use this code:
Image1->Picture->Bitmap->LoadFromResourceName((int)HInstance,"Bitmap1");
This line raised an exception because 'Bitmap1' said the compiler is unknown
!
What is wrong ?
Sam
 
 

Re:How using a resource file

"sam" < XXXX@XXXXX.COM >wrote in message
Quote
This line raised an exception because 'Bitmap1' said
the compiler is unknown !
Please always quote exact error messages word-for-word.
Did you add test.res to your project in the first place? It is not enough
to simply create the file, the project has to know about the file as well so
that it gets compiled into the final executable.
Gambit
 

Re:How using a resource file

I have added the res file to the project.
Now there is a new Bitmap resource named: Bitmap1. But it raised an
exception told that the Bitmap1 Bitmap resource was not found.
Sam
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >a écrit dans le
message de news:40b38409$ XXXX@XXXXX.COM ...
Quote

"sam" < XXXX@XXXXX.COM >wrote in message
news:40b36383$ XXXX@XXXXX.COM ...

>This line raised an exception because 'Bitmap1' said
>the compiler is unknown !

Please always quote exact error messages word-for-word.

Did you add test.res to your project in the first place? It is not enough
to simply create the file, the project has to know about the file as well
so
that it gets compiled into the final executable.


Gambit


 

{smallsort}

Re:How using a resource file

"sam" < XXXX@XXXXX.COM >wrote:
Quote
I have added the res file to the project.
How did you do that?
Quote
Now there is a new Bitmap resource named: Bitmap1. But it
raised an exception told that the Bitmap1 Bitmap resource
was not found.
You should always show the offending code and as Gambit stated earlier:
"Please always quote exact error messages word-for-word."
The res file should be in the same folder as your project and
this is what the top of the unit that uses the resource should
look like:
//-------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//-------------------------------------------------------------#pragma package(smart_init)
#pragma resource "test.res"
//-------------------------------------------------------------
Then to get something from that resource:
Graphics::TBitmap* Bitmap1 = new Graphics::TBitmap;
Bitmap1->LoadFromResourceName( (int)HInstance, "BITMAP1" );
~ JD
 

Re:How using a resource file

1) Graphics::TBitmap* Bitmap1 = new Graphics::TBitmap;
2) Bitmap1->LoadFromResourceName( (int)HInstance, "Bitmap1" );
3) Form1->Image1->Picture->Bitmap->Assign(Bitmap1);
At line 2 it raised always an exception EResNotFound at line 2:
'Resource Bitmap1 non trouvee'.
Of course in my Dll the resource exists at the Bitamp category, then there
is a folder named 'Bitmap1' and then the resource at the number 1036.
So what is wrong ?
"JD" < XXXX@XXXXX.COM >a écrit dans le message de
Quote

"sam" < XXXX@XXXXX.COM >wrote:
>I have added the res file to the project.

How did you do that?

>Now there is a new Bitmap resource named: Bitmap1. But it
>raised an exception told that the Bitmap1 Bitmap resource
>was not found.

You should always show the offending code and as Gambit stated earlier:

"Please always quote exact error messages word-for-word."

The res file should be in the same folder as your project and
this is what the top of the unit that uses the resource should
look like:

//-------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//-------------------------------------------------------------#pragma
package(smart_init)

#pragma resource "test.res"
//-------------------------------------------------------------

Then to get something from that resource:

Graphics::TBitmap* Bitmap1 = new Graphics::TBitmap;
Bitmap1->LoadFromResourceName( (int)HInstance, "BITMAP1" );

~ JD

 

Re:How using a resource file

"sam" < XXXX@XXXXX.COM >wrote:
Quote
[...] Of course in my Dll the resource exists at the Bitamp
category, [...]
You didn't tell us that you were loading the resource from a
dll. VERY IMPORTANT fact that you over looked.
You need to first load the dll using LoadLibrary to get a
handle to the DLL, and then LoadBitmap to get a handle to the
bitmap:
Graphics::TBitmap* Bitmap1 = NULL;
THandle dll = LoadLibrary( "MyDll.dll" );
if( dll )
{
try
{
Bitmap1 = new Graphics::TBitmap;
Bitmap1->Handle = LoadBitmap( dll, "BITMAP1" );
Form1->Image1->Picture->Bitmap->Assign( Bitmap1 );
}
catch( ... )
{
MessageDlg("Couldn't load the bitmap.", mtError, TMsgDlgButtons() << mbOK, 0 );
}
FreeLibrary( dll );
}
else MessageDlg("Error Loading the dll : " + SysErrorMessage(GetLastError()), mtError, TMsgDlgButtons() << mbOK, 0 );
delete Bitmap1;
~ JD
 

Re:How using a resource file

JD, the form is ownered by the Dll. I have not to do any LoadLibrary.
"JD" < XXXX@XXXXX.COM >a écrit dans le message de
Quote

"sam" < XXXX@XXXXX.COM >wrote:
>[...] Of course in my Dll the resource exists at the Bitamp
>category, [...]

You didn't tell us that you were loading the resource from a
dll. VERY IMPORTANT fact that you over looked.

You need to first load the dll using LoadLibrary to get a
handle to the DLL, and then LoadBitmap to get a handle to the
bitmap:

Graphics::TBitmap* Bitmap1 = NULL;
THandle dll = LoadLibrary( "MyDll.dll" );
if( dll )
{
try
{
Bitmap1 = new Graphics::TBitmap;
Bitmap1->Handle = LoadBitmap( dll, "BITMAP1" );
Form1->Image1->Picture->Bitmap->Assign( Bitmap1 );
}
catch( ... )
{
MessageDlg("Couldn't load the bitmap.", mtError,
TMsgDlgButtons() << mbOK, 0 );
}
FreeLibrary( dll );
}
else MessageDlg("Error Loading the dll : " +
SysErrorMessage(GetLastError()), mtError, TMsgDlgButtons() << mbOK, 0 );
delete Bitmap1;

~ JD

 

Re:How using a resource file

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
Bitmap1 = new Graphics::TBitmap;
Bitmap1->Handle = LoadBitmap( dll, "BITMAP1" );
Form1->Image1->Picture->Bitmap->Assign( Bitmap1 );
You can simply that code:
THandle dll = LoadLibrary( "MyDll.dll" );
if( dll )
{
Image1->Picture->Bitmap->Handle = LoadBitmap( dll, "BITMAP1" );
FreeLibrary( dll );
}
Gambit
 

Re:How using a resource file

"sam" < XXXX@XXXXX.COM >wrote in message
Quote
JD, the form is ownered by the Dll.
Are you saying that the form is inside the DLL itself? If so, then you
cannot use the global HInstance variable when calling
LoadFromResourceName(). That points to the *application's* instance, not
the DLL's. In which case, the error is correct, the bitmap does not exist
inside the application, it is inside the DLL instead. As such, you need to
use the HINSTANCE that is provided by the DLL's DllEntryPoint() function
instead.
Gambit