Tech Support Guy banner

C/C++ using #define values...

2084 Views 3 Replies 3 Participants Last post by  MacFromOK
Hiya, :)

I'm trying to send some window messages from user input.
For example: BM_CLICK

Now I know BM_CLICK is defined, but I can't figure a way
to use the input string "BM_CLICK" and get it's defined
value (or any other #define) without using something
like if(lstrcmpi, Arg, "BM_CLICK" == 0) and manually
assigning the value, or hardcoding BM_CLICK in
SendMessage.

Any ideas on how to convert a "BM_CLICK" string to a
BM_CLICK message without using IF?

Thanks, Mac :)
Status
Not open for further replies.
1 - 4 of 4 Posts
You're quite right -- you can't really use BM_CLICK or any other #define at run-time unless you refer to it directly, because a #define is just a text substitution as far as the compiler is concerned (i.e., it happens at compile time, and any knowledge about the #define is lost). I've used a table and a lookup function to map the input text ("BM_CLICK") to the corresponding value; it's not particularly elegant, but it solves the problem.

Hope this helps.
There isn't an automatic way to do this. I would suggest creating a table of strings and values, and then scanning the table for the specified string. You'll still need an 'if', but you'll be doing this inside a loop. To add new values, you just update the table, not the code.

You could use a hash map (if your library supports it), database or some other mechanism, but someone, somewhere, is going to be doing the equivalent of what I just described (although they might use an index or hash, instead of scanning), and you will still need to create and maintain the table.
Thanks guys, :)

Was afraid of that. I'll prolly use an array if I don't hardcode
in the values, or I could just furnish the user with a table of
#define numbers... ;)

Thanks again, Mac :)
1 - 4 of 4 Posts
Status
Not open for further replies.
Top