7

I've got a compiled resource file (.res). I need to read it, in C#, so that I can modify it programatically. note that this is not a .resx file or .rc file; the file is compiled, not text-based.

So far I tried looking at LoadLibrary, LoadResource, etc. in the Win32 API, but it seems that these functions only work on executables (.exe, .dll), and not resource files.

I've tried loading the file with BinaryReader, but of course I can't make much sense of the resultant byte array. I thought of trying to use Marshal.PtrToStructure but I haven't got an idea what the structure of a res file is. I've got the structure of the RESOURCEHEADER, but I couldn't understand how to use it (I admit I've got very little native-code experience).

Could anybody please help me figure out how to successfully read and update the version info in the .res file?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Assaf Stone
  • 6,309
  • 1
  • 34
  • 43
  • You can link a `.res` file to create a resource-only DLL. But that's obviously geared toward the use case of actually using the resources, not modifying the `.res` file programmatically. – Kaz May 21 '21 at 00:25

1 Answers1

3

Very good question; I couldn't find a good answer using existing functions either. Fortunately, it seems that the RES file format is relatively simple, and is documented here:

http://msdn.microsoft.com/en-us/library/ms648007(VS.85).aspx

You should be able to scan the RES file for the version resource, and then update the appropriate fields. Remember that the resource header is DWORD-aligned.

My only other suggestions would be to use LoadLibraryEx and see if you can load the RES file as a datafile somehow. But it sounds like you've already tried that. If you did succeed though, you might find this topic interesting - it's an example of how to copy resources between two modules:

http://msdn.microsoft.com/en-us/library/ms648008(VS.85).aspx

I doubt it will work though. I loaded RC.EXE in Dependency Walker to see if it was using any interesting APIs for building RES files. I did not find any, so I can only assume that RC.EXE directly writes out the RES files.

James Johnston
  • 9,264
  • 9
  • 48
  • 76
  • Thanks for the quick answer. I saw this page already, but didn't know how to make sense of it. How can I get at the version portion though? I know that it isn't at the start, but can't figure out how to locate it. – Assaf Stone Nov 02 '11 at 15:08
  • I would suggest opening up a RES file in a hex editor and try to decipher it by hand to make sure you're understanding the format. It sounds to me that the resources are just concatenated one after the other. Look at the beginning of the file and you should see a RESOURCEHEADER structure. Practice interpreting it and learn how to go from one RESOURCEHEADER to the next one. Eventually you should find one that defines a version resource. Then you can work with the version resource structures. – James Johnston Nov 02 '11 at 16:44