cResToStr working function

Post Reply
toninhofwi
Posts: 161
Joined: Tue Oct 18, 2005 10:01 am

cResToStr working function

Post 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.
Last edited by toninhofwi on Sat Apr 18, 2009 4:16 pm, edited 1 time in total.
User avatar
vailtom
Posts: 47
Joined: Thu Jan 05, 2006 6:56 pm
Contact:

Re: cResToStr help needed

Post by vailtom »

Because sizeof(int) or sizeof(hResource) always return 4 (integers have 4 bytes in length)....
Vailton Renato
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: cResToStr help needed

Post 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.
regards, saludos

Antonio Linares
www.fivetechsoft.com
toninhofwi
Posts: 161
Joined: Tue Oct 18, 2005 10:01 am

Re: cResToStr help needed

Post 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.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: cResToStr help needed

Post by Antonio Linares »

Toninho,

Have you tried SizeofResource() instead of GlobalSize() ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
toninhofwi
Posts: 161
Joined: Tue Oct 18, 2005 10:01 am

Re: cResToStr help needed

Post 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.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: cResToStr help needed

Post by Antonio Linares »

Toninho,

Please try this:

In your RC file:
test PNG "file.png"

then from your PRG:
MsgInfo( Len( cResToStr( "test", "PNG" ) ) )
regards, saludos

Antonio Linares
www.fivetechsoft.com
toninhofwi
Posts: 161
Joined: Tue Oct 18, 2005 10:01 am

Re: cResToStr help needed

Post 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.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: cResToStr working function

Post by Antonio Linares »

very good :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
Post Reply