Page 1 of 1
cResToStr working function
Posted: Fri Apr 17, 2009 1:07 am
by toninhofwi
Hi Antonio.
Look at this code please:
Code: Select all
HB_FUNC( CRESTOSTR )
{
HRSRC hRes = FindResource( ( HMODULE ) GetResources(), MAKEINTRESOURCE( hb_parni( 1 ) ), RT_BITMAP );
HANDLE hResource = LoadResource( ( HINSTANCE ) GetResources(), hRes );
hb_retclen( ( LPSTR ) LockResource( hResource ), sizeof( hResource ) );
}
? Len( cResToStr( 7777 ) ) always return 4 bytes instead a string of a full bitmap bits.
The same occur with original crestostr() FWH function.
Any hint please ?
Regards,
Toninho.
Re: cResToStr help needed
Posted: Fri Apr 17, 2009 11:08 am
by vailtom
Because sizeof(int) or sizeof(hResource) always return 4 (integers have 4 bytes in length)....
Re: cResToStr help needed
Posted: Fri Apr 17, 2009 2:36 pm
by Antonio Linares
Toninho,
You are not using SizeofResource() which it is needed.
Also you are not checking for wrong returned values! You are assuming that everything will be found, which can not be the case.
Re: cResToStr help needed
Posted: Fri Apr 17, 2009 4:25 pm
by toninhofwi
Antonio Linares wrote:Toninho,
You are not using SizeofResource() which it is needed.
Also you are not checking for wrong returned values! You are assuming that everything will be found, which can not be the case.
Hi Antonio and Vailton,
It is intentional, only to simplify my code, but lets go, here is the full code, that doesn´t work:
Code: Select all
HB_FUNC( CRESTOSTR )
{
HRSRC hRes = FindResource( ( HINSTANCE ) GetResources(), ( LPCSTR ) ( ISNUM( 1 ) ? ( LPCSTR ) hb_parnl( 1 ) : hb_parc( 1 ) ), ( LPCSTR ) hb_parnl( 2 ) );
HANDLE hglb;
if( hRes )
{
hglb = LoadResource( ( HINSTANCE ) GetResources(), hRes );
if( hglb )
{
hb_retclen( ( LPSTR ) LockResource( hglb ), GlobalSize( hglb ) );
}
else
{
hb_retc( "" );
}
}
else
{
hb_retc( "" );
}
}
To test it:
Code: Select all
? Len( crestostr( 7777, "BITMAP" ) )
This code is the original crestostr from FWH.
What is wrong please?
Regards,
Toninho.
Re: cResToStr help needed
Posted: Fri Apr 17, 2009 7:16 pm
by Antonio Linares
Toninho,
Have you tried SizeofResource() instead of GlobalSize() ?
Re: cResToStr help needed
Posted: Fri Apr 17, 2009 7:45 pm
by toninhofwi
Antonio Linares wrote:Toninho,
Have you tried SizeofResource() instead of GlobalSize() ?
Yes, and no works...
My intention in load a png file from resource. Now, I´m trying with BITMAP because is easy, if works with bitmap, I´ll load png.
I´d tried native FWH crestostr() and no works too.
TIA and best regards,
Toninho.
Re: cResToStr help needed
Posted: Fri Apr 17, 2009 10:45 pm
by Antonio Linares
Toninho,
Please try this:
In your RC file:
test PNG "file.png"
then from your PRG:
MsgInfo( Len( cResToStr( "test", "PNG" ) ) )
Re: cResToStr help needed
Posted: Sat Apr 18, 2009 4:15 pm
by toninhofwi
Hi Antonio,
This is the right code:
Code: Select all
HB_FUNC( CRESTOSTR )
{
HMODULE hMod = GetResources();
HRSRC hRes = FindResource( hMod, MAKEINTRESOURCE( hb_parni( 1 ) ), "PNG" ); // need change to get param 2
LONG size = SizeofResource( hMod, hRes );
HANDLE pt = LoadResource( hMod, hRes );
LPSTR pResult;
pResult = ( LPSTR ) hb_xgrab( size + 1 );
memcpy( pResult, pt, size );
hb_retclen( pResult, size );
hb_xfree( pResult );
}
Regards,
Toninho.
Re: cResToStr working function
Posted: Sat Apr 18, 2009 11:18 pm
by Antonio Linares
very good