PDA

View Full Version : Resources [Non-VCL]



Mrwb
25-06-2004, 06:01 PM
I've been wondering about this for a while now, is there a way to read resources (*.res) without using the VCL classes? I cleaned up my demosystem today, and I found out that it would save me 20-25kb to do the resource stuff without the VCL, and since I'm doing 64kb's, thats a lot.. An alternative way of including MIDI files in a tight way would also be much appriciated..

Paulius
25-06-2004, 06:16 PM
Look up "Resource Functions" in windows sdk help

Mrwb
25-06-2004, 07:40 PM
..and there it was. Thanks as usual Paulius ;)
I have to sart using the Win SDK help files more.. I kinda forget they excist :oops:

Mrwb
27-06-2004, 02:00 PM
The SDK is driving me insane! I did a proc to find and save a resource to file. The code looks like this:


//Finds and saves a resource to specified file.
function SaveResource(Filename,ResID,ResType: PChar):boolean;
var
SoundRes: cardinal;
SoundResHandle: cardinal;
NumBytesWritten: cardinal;
SoundResSize: cardinal;
FileHandle: cardinal;
Locked: pointer;
begin
SoundResHandle:=FindResource(0,ResID,ResType);
SoundRes:=LoadResource(0,SoundResHandle);
if SoundRes=0 then
begin
MessageBox(0,PChar('Error loading '+ResID+' resource'+#13+'Error code '+inttostr(GetLastError)),'Error!',0);
Result:=false;
exit;
end;
Locked:=LockResource(SoundRes);
SoundResSize:=SizeofResource(0,SoundResHandle);
FileHandle:=CreateFile(Filename, GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,0);

if WriteFile(FileHandle,SoundRes,SoundResSize,NumByte sWritten,0)=false then
begin
MessageBox(0,PChar('Failed to write data to '+Filename+#13+'Error code '+inttostr(GetLastError)),'Error!',0);
DeleteFile(Filename);
Result:=false;
exit;
end;
CloseHandle(Filehandle);
Result:=true;
end;


The resource is found, the size of the resource is the correct one, SoundRes is really pointing to the memory block it's supposed to, yet, it won't write content to the output file. I figured the error must be in WriteFile(FileHandle,SoundRes,SoundResSize,NumByte sWritten,0) so I have tried every possible changes to it without luck. I have tried using Locked and SoundResHandle instead of SoundRes and I have tried using the OVERLAPPED structure. But I can't get it right. I get the custom buffer error when doing WriteFile();

I have read the SDK documentation so many times now, that I had nightmares about the evil structures comming to get me.. Any help greatly appriciated..

Paulius
27-06-2004, 04:39 PM
WriteFile wants a pointer not a handle so you should be passing "Locked" instead of "SoundRes", do you get the same error using it?

Mrwb
27-06-2004, 04:48 PM
Yeah, get the same error...

I noticed something earlier though, I thought I'd experiment with WriteFile so I wrote a constant number at the BytesToWrite. When the number was <3000 bytes, the file was filled with data, but when it was over, it pushed an error. This isn't excactly logical, since the pointer pointed to a place in memory containing 26322 bytes.

Paulius
27-06-2004, 06:01 PM
Damn lying documentation, it doesn't want a pointer, so it actually should be Locked^

Mrwb
27-06-2004, 06:19 PM
You're brilliant! :D it works like a charm now! Boy I would have been struggling with this for weeks. ;) Thanks a lot! :)

btw; to compare the changes from using TResourceStreem to this:
With TResourceStreem my exe containing a 24kb MIDI was 74kb compressed with UPX. When using this, the exe is 65 UNCOMPRESSED and 21kb (!!) compressed with a 24kb MIDI. This file also containes the playback engine for my demosystem, AND a datafile for the intro :)