translate #defines

Posted on 2006-03-29
Last Modified: 2013-11-20

Is there anyway to convert a #define from a string into it's numeric value:


for instance should be intrepreted as whatever ms defined as its real numeric value:


I'm just trying to write a small interpreter, and I don't want to have a function to translate them like:

    int TranslateDefs(CString str)
        if (str == "SS_BLACKFRAME") {
            return 0x07L;
        // etc...

Thank you
Question by:minnirok
    LVL 22

    Expert Comment

    whats exactly you are looking for ?
    #define is Preprocessor directive that means compile time your values will be changes with whatever you define it as.

    LVL 7

    Author Comment

    yeah ms must have this someplace, probably in windows.h or something:

        #define SS_BLACKFRAME 0x07L

    Now I'm reading a text file that has this on a line:


    I want to be able to store the defined value of SS_BLACKFRAME in a DWORD:

        DWORD dwStyle;

        dwStyle = SomeFunkyThingToTurnTheTextRepresentationIntoTheNumericVal("SS_BLACKFRAME");

    LVL 86

    Accepted Solution

    Use a map:

    #include <map>
    usinag namespace std;

    map<char*,DWORD> mapNameToId;



    #define ADD_ENTRY(x) mapNameToId[#x] = x;

    DWORD SomeFunkyThingToTurnTheTextRepresentationIntoTheNumericVal(char* x) {

        return mapNameToId[x];
    LVL 7

    Author Comment

    Ok just wanted to see if there was something to already translate. This should work ok though.


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now