From: raja on
can we able to identify the resource id from the handle of the
resource, something like opposite to getting the resource id from the
resource name using MAKEINTRESOURCE, is there any macro , or any code
snippet to resolve this..
thanks
From: Joseph M. Newcomer on
If you have an HRSRC, the only way you could get it was if you had the ID. So there is no
need to go the other way, because you have to have it already.

There are APIs to iterate over all the resources and look at the IDs and types and values.
For example, see EnumResourceTypes and EnumResourceNames.

If you could be a little more specific about what you need, I might be able to suggest
some approaches.

Note that MAKEINTRESOURCE does *not* convert a resource name to a resource ID; it takes an
integer resource ID and lets you pass it as a string argument. The way LoadResource works
is if the argument is in the range 1..65535, it looks up the resource ID by doing integer
compare with the value; if it is >65535, it assumes it is a string pointer and looks up
the resource ID by doing a string compare with the value pointed to. So it doesn't
actually "convert" anything.

The macro IS_INTRESOURCE is used to detect if the value is a resource or a pointer; it is

#define IS_INTRESOURCE(_r) (((ULONG_PTR)(_r) >> 16) == 0)

so you can do this yourself if you want:

void SomeFunctionOfMine(LPTSTR p)
{
if(IS_INTRESOURCE(p))
{ /* int */
int value = (int)p;
... do things with the integer
} /* int */
else
{ /* string */
LPCTSTR value = p;
.. do things with string
} /* string */

Now suppose you have, in your resource.h file, something of the form:

#define IDC_WHATEVER 211

appears in resource.h; if you want to load the resource, you have to do

HRSRC FindResource(module, MAKEINTRESOURCE(211), type)

but you don't actually write
MAKEINTRESOURCE(211)
you write
MAKEINTRESOURCE(IDC_WHATEVER)
which the *preprocessor* converts to
MAKEINTRESOURCE(211)

The types work the same way. There are predefined types like RC_DIALOG, where RC_DIALOG
is described as

#define RT_DIALOG MAKEINTRESOURCE(5)

MAKEINTRESOURCE itself is a trivial macro, simplistically,

#define MAKEINTRESOURCE(x) ((LPTSTR)(ULONG_PTR)(WORD)(x))

However, there is no *possible* way to say, for example

CString ResourceToString(LPCTSTR p)
{
if(IS_INTRESOURCE(p))
return ...something that evaluates to "IDC_WHATEVER";
else
return CString(p);
}

so that if you were to call it with
ResourceToString(MAKEINTRESOURCE(IDC_WHATEVER))
that would return "IDC_WHATEVER" as a string value.

That said, you can do certain things like

#define ResourceToString(x) _T(#x)

which means if you write

ResourceToString(IDC_WHATEVER)

you get the string "IDC_WHATEVER"

but that's because all we did was say "convert the input argument to a string and return
that string". So suppose we had

#define IDC_WHATEVER 211
#define IDC_THING 212

if you called the above macro

ResourceToString(IDC_WHATEVER + 1)

you would *not* get "IDC_THING", you would get "IDC_WHATEVER + 1".

Does any of this help?
joe


On Tue, 24 Nov 2009 09:40:40 -0800 (PST), raja <rajasegar.c(a)gmail.com> wrote:

>can we able to identify the resource id from the handle of the
>resource, something like opposite to getting the resource id from the
>resource name using MAKEINTRESOURCE, is there any macro , or any code
>snippet to resolve this..
>thanks
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm