Thread: Macro Question - HRESULTS and function names

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Question Macro Question - HRESULTS and function names

    Hi there,

    A quick(ish) question about macros. I managed to get some of the code I've been learning lately to work for myself. I'm just not sure why it works. It contains a syntax I've not used before:

    Code:
    #ifndef getFunctionName
    #define getFunctionName(x)                                              \
    {                                                                       \
        HRESULT hr = (x);                                                   \
        if (hr == E_FAIL)MessageBoxW(NULL, L#x, L"Function Failed", 0);     \
    }
    #endif
    When run like so:

    Code:
    getFunctionName(ReturnFail());
    
    // definition
    HRESULT ReturnFail()
    {
        return E_FAIL;
    }
    It works fine. I'm just unsure as to how exactly the code manages to retrieve an entire function name from a returned HRESULT using the L#x part, and I also don't know why the macro has the single backslashes \ in it for each line. The compiler wouldn't allow compilation without them though.

    Any ideas? Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (hr == E_FAIL)MessageBoxW(NULL, L#x, L"Function Failed", 0);

    Stringification - The C Preprocessor

    Essentially, it takes whatever the string of characters is for parameter x, and slaps double quotes around it to make it a string.


    > and I also don't know why the macro has the single backslashes \ in it for each line.
    Because #defines need to be a single logical source line.
    Backslash followed by a newline causes the two lines to be folded into one before the pre-processor tries to deal with the line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Oh brilliant! Thanks very much! I'll get to reading that link pronto!

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Smile Thanks

    I made a little test macro for myself to try and use the same principles. Does the same job just for a bit more effort:

    Code:
    #ifndef getFunctionName
    #define getFunctionName(x)                                                  \
    {                                                                           \
        if (x == E_FAIL)                                                        \
        {                                                                       \
            const int newSize = strlen(#x) + 1;                                 \
            WCHAR* functionName = new WCHAR[newSize];                           \
            MultiByteToWideChar(CP_UTF8, 0, #x, -1, functionName, newSize);     \
            MessageBoxW(NULL, functionName, L"Function Name", 0);               \
            delete functionName;                                                \
        }                                                                       \
    }
    #endif
    Thanks
    Last edited by shrink_tubing; 01-18-2024 at 01:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macro function
    By freeindy in forum C Programming
    Replies: 6
    Last Post: 11-08-2007, 05:04 AM
  2. Macro has same name as Function?
    By coolclu3 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 04:55 AM
  3. macro function
    By bradleyd in forum C Programming
    Replies: 8
    Last Post: 05-21-2007, 05:18 PM
  4. g++ error: Macro names must be identifiers.
    By eccles in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2005, 03:28 AM
  5. macro or function
    By studentc in forum C Programming
    Replies: 5
    Last Post: 06-02-2004, 01:40 PM

Tags for this Thread