Code sample to access a resource

Share your advanced PureBasic knowledge/code with the community.
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Code sample to access a resource

Post 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.
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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?
quidquid Latine dictum sit altum videtur
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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. :?
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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 ? :?
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

What "trash" part are you refering to?
quidquid Latine dictum sit altum videtur
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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).
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post 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,
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post 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.
Mat
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

Why no making it possible to compile it along?
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post 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.
Mat
Post Reply