Page 1 of 1

Code sample to access a resource

Posted: Thu Sep 04, 2003 7:40 am
by Seldon
Code updated for 5.20+

I thought it could be useful to alls. Here it is a way to access a RAW Data resource. I use it to store compressed data (it's better than IncludeBinary as a resource is loaded into memory just when needed). As you can see, I pass the buffer to UnPackMemory and it works well.

Code: Select all

nris.l=1 ;-> number of resource
hr=FindResource_(#Null,nris.l,#RT_RCDATA)
If hr
  hg=LoadResource_(#Null, hr)
  If hg
    lpData=LockResource_(hg)
    If lpData
      *Buffer = AllocateMemory(8000) ;-> your needed size
      ;decomlu=UncompressMemory(lpData,*Buffer)
      ;...
      ;...
    EndIf
  EndIf
EndIf
To compile (by Microsoft's RC) the RES file, I use this source:

Code: Select all

1 RCDATA
BEGIN
#include "test.txt"
END
Test.txt is actually a binary file converted using bin2txt.exe command. Here it is the MAKE I use (it's a .BAT file).

Code: Select all

@echo off
bin2txt test.pack test.txt
rc yourprogram.rc
pause
test.pack is a compressed binary file. The final yourprogram.res file must be added to yourprogram.exe using a program like ResHacker. As I said this way should be better than IncludeBinary, as the data isn't stored in the DATA section of your program and it isn't loaded soon into memory.

Posted: Tue Aug 31, 2004 10:18 am
by Seldon
After some time, I was going to use this code and I noticed it doesn't work anymore with 3.91 (FindResource() always returns 0). I'm 100% sure it worked and I use it under C and BlitzBasic as well. That's very weird, I checked alla parameters and the resource DOES exist in the .EXE . I sincerely don't know what to do.

Fred, have you some idea why this code doesn't work anymore ? Thank you in advance.

Posted: Tue Aug 31, 2004 12:34 pm
by freak
Maybe the recource number is not right anymore?
I'm no expert in this, but since now the XP manifest file is added as
a recource now, wouldn't that cause the other recources numbers to change?

Posted: Tue Aug 31, 2004 4:26 pm
by Seldon
Thanks for reply. No, each resource type has its own ID number. RC_DATA has number 10, while XP_Manifest has number 24. I sincerely have no idea why it isn't working anymore. The resource is added as RC_DATA and the number (not the type) of resource is right. :?

Posted: Fri Sep 03, 2004 5:57 pm
by Seldon
No luck so far. Such code DID work perfectly with 3.51 *for* *sure*. And that code isn't buggy as it works perfectly in C and BlitzBasic. Maybe that "trash" part added to EXE mentioned in another message can cause problems to resource API ? :?

Posted: Fri Sep 03, 2004 8:03 pm
by freak
What "trash" part are you refering to?

Posted: Fri Sep 03, 2004 8:31 pm
by Seldon

Posted: Wed Sep 15, 2004 7:18 am
by Seldon
A bit update: the code DOES work if I compile it as a DLL (still using PB compiler of course). So there could be something wrong in the layout of EXE files (I think that's what "resource" functions check).

Posted: Thu Feb 03, 2005 10:32 am
by Edwin Knoppert
I have currently problems with adding resources also.

This was still working for v390 or 391.
Something must have been changed???

/XP does insert the resource but adding my own resource results in a bad win32 app.
I strongly wish a commandline command or so that accepts .rc or maybe even better .res files.

Thanks,

Posted: Thu Feb 03, 2005 4:42 pm
by MrMat
I tried adding a resource using Japbe, PBCoffee and Resource Hacker and the resource was found (using the code posted above) in all three cases so i don't know why it isn't working for you guys :? That was using 3.92.

Posted: Thu Feb 03, 2005 4:44 pm
by Edwin Knoppert
Why no making it possible to compile it along?

Posted: Thu Feb 03, 2005 4:47 pm
by MrMat
You mean using a command line switch? Somewhere on these forums i think i read Fred said he'd add a PBCompiler parameter that could be passed to the linker, or maybe i dreamt that :lol: That would be really handy for adding resources and doing other stuff too.