The format of data and custom resources


Continuing the highly-sporadic series of Win32 resource formats, today we’ll look at the format of RT_DATA resources, which are declared in resource files as RCDATA. Also the format of custom resources, which are declared in resource files by just giving the custom resource name or ordinal as the second word on the declaration.

The format is very simple: It’s just raw binary data. If you ask for a 16-bit integer followed by an ANSI string, then you get a 16-bit integer followed by an ANSI string.

If you ask for the data to be inserted from a file, then the contents of the file become the resource data. No processing is done on the data. You get what you get.

In particular (and this is what catches some people), if your file is a text file, the resource compiler will just treat your text file as binary data and dump it raw into the resources. It doesn’t know that it’s a text file; it’s just a file. This means that if you want a null terminator at the end of your text file, you need to put a null terminator at the end of the text file. Nobody’s going to magically do it for you.

(The Size­Of­Resource function may come in handy here.)

Comments (14)
  1. Joshua says:

    Well this certainly could take the place of my custom resource format, assuming the loader isn't going to do something stupid and map megabytes and megabytes of resources into RAM.

  2. Koro says:

    Joshua: It will. Resources are mapped as part of the executable. Actually, LoadResource/LockResource just returns a pointer to it.

    [It gets mapped into the process address space, but it doesn't show up in RAM until you access it. (And then if you stop accessing it, it will get discarded.) -Raymond]
  3. Anonymous says:

    Just to mention SizeofResource is not so handy as it rounds up to alignment size, so the actual size of raw

    binary data can not be found with this call.

    One must implement his own way like EOF marker or store real size at other place.

    See support.microsoft.com/…/57808

    [That KB article applies to 16-bit Windows and to Windows 95-derived versions of Windows. It does not apply to Windows NT. -Raymond]
  4. Joshua says:

    [It gets mapped into the process address space, but it doesn't show up in RAM until you access it. (And then if you stop accessing it, it will get discarded.) -Raymond]

    Since if you have to ask how much is too much you already have too much, I'm better off not.

  5. yet harder says:

    if (nt)

     use SizeofResource

    else

     use own code

  6. Marc K says:

    if (nt)

     use SizeOfResource();

    else

     throw new OperatingSystemNotSupportedException("Are you kidding me?");

  7. Marc K says:

    [That KB article applies to 16-bit Windows and to Windows 95-derived versions of Windows. It does not apply to Windows NT.]

    That's rough.  I know KB57808 lists Applies To as "Microsoft Platform Software Development Kit-January 2000 Edition", but there are tons of KB articles where the Applies To lists an older version of the OS and it does apply to the current release.  It is reasonable to make the assumption that it still applies given lack of documentation to the contrary.

  8. Is there any documentation that describes the resource definition file format (.rc)?

    Thank you

  9. dave says:

    >Is there any documentation that describes the resource definition file format (.rc)?

    Yes, it's a documented language; MS calls it a 'script' though I'd say it doesn't qualify for that term since it is really a data declaration language.

    Type "resource definition file format" into Google (no quotes).

  10. Rick C says:

    @Mark K it can be pretty easily verified that SizeofResource works as Raymond describes:

    SizeofResource.rc

    1 rcdata

    {

    "test

    }

    SizeofResource.c

    #include <windows.h>

    #include <stdio.h>

    int main(void)

    {

       HRSRC h = FindResource(NULL, MAKEINTRESOURCE(1), MAKEINTRESOURCE(10));

       HGLOBAL hg = LoadResource(NULL, h);

       DWORD d = SizeofResource(0, h);

       printf("There are %d bytes.n", d);

       return 0;

    }

    rc SizeofResource.rc

    cl SizeofResource.c SizeofResource.res kernel32.lib

    Run the program.  Does it return 5 bytes or a larger number?

  11. @Dave,

    Thanks, after I wrote my comment I did hat you suggested and found this

    msdn.microsoft.com/…/aa380599(v=vs.85).aspx

    It seems it.

  12. user0 says:

    This website appears to be broken when using the newest version of Firefox (23).

    Very annoying :-(

  13. David T says:

    @user0 it's broken if you attempt to access it over https (as it loads active content over http, and firefox now blocks that by default).  You can either click on the shield each time and enable it, or just use http.

  14. km007 says:

    There doesn't seem like there's an advantage of using RT_DATA over a custom resource type since they're handled the same way.

Comments are closed.