[comp.windows.ms] FindResource doesn't find resources

prc@erbe.se (Robert Claeson) (05/19/89)

I'm trying to use FindResource to find a string resource.
Then I call SizeofResource to get the size of the resource.
Using it, I do a LocalAlloc to allocate a buffer.
Finally, I read the string into the buffer using LoadString.

The point with all this is to dynamically allocate memory for
strings, based on their length at run time rather than a good
guess determined at compile time. This is especially nice in
the application I'm writing, since there can be many different
versions of the resource floating around and I don't want to
update the source code when I'm just changing a few strings
in the resource.

The problem is, it doesn't work. FindResource doesn't find
the resource I ask it to find, but LoadString loads the same
strings quite nicely.

According to my manual, FindResource accepts three arguments;
the instance, an LPSTR to a string holding the name of the resource
and an LPSTR to a string that holds the name of the resource type.
The manual also mentions that if the  high-order word of any of these
two LPSTR arguments is zero, the low-order word is assumed to contain
the integer ID of the resource or the integer ID of the resource type.

I've tried to pass both integer and string ID's in all possible
combinations and with all possible (I think) casts, but the damn
function still doesn't find the resources for me.

If there's anyone or anything out there who knows what's going on,
*please* enlighten me. This problem has kept me awake the last nights.

Thanks in advance,

	Robert

-- 
          Robert Claeson      E-mail: rclaeson@erbe.se
	  ERBE DATA AB

bturner@hpcvlx.HP.COM (Bill Turner) (05/19/89)

> I'm trying to use FindResource to find a string resource.
> Then I call SizeofResource to get the size of the resource.
> Using it, I do a LocalAlloc to allocate a buffer.
> Finally, I read the string into the buffer using LoadString.

Ah, the problem is that string resources aren't like the other
resources.  All other resources are stored individually accessable,
but string resources are packed together into 16 resource segments.

I believe that what is done is that all strings are packed into the
segment based on their string ID modulo 16.

FindResource et al are used to get the segment resources.  LoadString
goes one step further and pulls the individual string out of the
resource segment.

There is no way that I know of* to find the length of the string
resource before loading it.  One possible way is to malloc a string,
and if LoadString returns the maximum size, assume the string wasn't
long enough; increase and try again.  Once the entire string is read,
realloc to the required size.  (Yech!).

--Bill Turner (bturner@hp-pcd.hp.com)

*There is no supported way, but the format of the string resource
segments is reverse engineerable...

prc@erbe.se (Robert Claeson) (05/21/89)

I wrote:

> I'm trying to use FindResource to find a string resource.
> Then I call SizeofResource to get the size of the resource.
> Using it, I do a LocalAlloc to allocate a buffer.
> Finally, I read the string into the buffer using LoadString.

To which bturner@hpcvlx.HP.COM (Bill Turner)
in article <106580034@hpcvlx.HP.COM> responded:

>Ah, the problem is that string resources aren't like the other
>resources.  All other resources are stored individually accessable,
>but string resources are packed together into 16 resource segments.
>
>I believe that what is done is that all strings are packed into the
>segment based on their string ID modulo 16.
.....

Microsoft has once more had me spending expensive time on an unsolvable
problem, just because they didn't document that FindResource can't be used
with string resources. Argh!

-- 
          Robert Claeson      E-mail: rclaeson@erbe.se
	  ERBE DATA AB