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

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

MacFromOK

Thread Starter
Joined
Mar 18, 2001
Messages
1,741
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 :)
 
Joined
Feb 11, 2002
Messages
1,405
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.
 
Joined
Jan 20, 2002
Messages
433
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.
 

MacFromOK

Thread Starter
Joined
Mar 18, 2001
Messages
1,741
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 :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top