Thread: Hi there! I need some help with a macro definition thanks

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

    Exclamation Hi there! I need some help with a macro definition thanks

    Hi there!

    As the good old title says! Check this out:

    Code:
    #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
    I sorta get the first bit. the IID_PPV_ARGS(ppType) thing gets defined as a GUID thing (in this case a COM pointer to a DirectX12 Command Queue) and expects an argument of type pointer to a pointer with argument name ppType.

    I have no clue what the next bit is doing, and I'm even really sure I understand the first bit properly.

    As if it couldn't get any stranger... it does - see what the book I'm reading (which this is from) says:

    The IID_PPV_ARGS_Helper function esstentially casts ppType to a void**.
    Really? I didn't see any of that in the definition. I could just proceed past this but I don't want to I'd rather take the time to understand it properly before proceeding.

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    I have no clue what the next bit is doing
    You can't recognize a simple function call?

    Really? I didn't see any of that in the definition.
    Since you haven't shown us the definition of IID_PPV_ARGS_Helper we can't help you with that problem.

    We might be able to help you more if you show us how the IID_PPV_ARGS macro function is used.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    115
    That's what's got me puzzled John, this is all the author mentions about the helper function. Was confusing to me to see it in the same line as the definition but seemingly rather lacking in description. I'll give an example of where at least one of the things in this definition is used:

    Code:
    MicroSoft::WRL::ComPtr<ID3D12CommandQueue> mCommandQueue;
    D3D12_COMMAND_QUEUE_DESC queueDesc = {};
    
    //fills out some struct variables....
    
    ThrowIfFailed(md3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&mCommandQueue)));
    So I can see the IID_PPV_ARGS part gets passed to the function, and the __uuidof part of the definition means the &mCommandQueue argument resolves to the GUID of the COM interface ID3D12CommandQueue. I'm mostly ok with that. I just can't fathom the second part of the definition, seems like there's just no description of what it does. I'll have a look through some source code and see if it's mentioned there but this seems awfully sparse on description but that is exactly how it's presented in the book so far.



    Edit: This was quite interesting:
    IID_PPV_ARGS_Helper Function | Microsoft Docs

    Seems the IID_PPV_ARGS_Helper function isn't even meant to be called directly, MicroSoft recommend just using the IID_PPV_ARGS instead. I think that's why I've struggled so much to understand the macro.
    Last edited by shrink_tubing; 06-03-2022 at 08:29 PM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,466
    Recall that a "function-like macro" is not a function; it just looks like one.
    IID_PPV_ARGS is not "passed" to CreateCommandQueue,
    nor is its "output", since it is not a function.
    Instead, a macro is just textual replacement.
    This is a bit of an unusual macro that is meant to turn one parameter into two parameters (hence the comma).

    C/C++ code is compiled in two phases:
    1. preprocessing
    In which includes, defines, and other preprocessor "directives" are processed and removed.
    2. compiling
    The resulting program text is compiled, with all the included stuff (possibly thousands of lines), all the defines replaced with their textual replacements, etc.

    Consider:
    Code:
    #include <iostream>
     
    #define FUNCLIKE_MACRO(x) f(x), g(x)
     
    int f(int x) { return 2 * x; }
    int g(int x) { return 3 * x; }
     
    void func(int a, int b, int c) {  // needs 3 params
        std::cout << a << ',' << b << ',' << c << '\n';
    }
     
    int main() {
        func(1, FUNCLIKE_MACRO(2)); // kind of looks like we're only passing 2 params
     
        // the macro textually expands to:
        func(1, f(2), g(2));
        // now we're passing the constant 1, the output of f(2) and the output of g(2)
    }
    Apparently IID_PPV_ARGS_Helper just returns its input as a void**, except that it ensures that the input (sans pointer levels) derives from IUnknown.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    115
    Hi there! You've broadened my wisdom once again John thanks very much I understand what you mean and thus what the original macro does. I'm going to do some practising with the sample code you wrote in my compiler to help cement the knowledge.

    I'll also checkout the definition of the CreateCommandQueue function too to help me better understand it.

    Thanks very much John

  6. #6
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    If it helps any, you might want to try applying the stand-alone preprocessor to some sample code to see what it does.

    What you want is called cpp for the GCC compiler suite; there is an option in GCC to access it as well, gcc -E. See this MSDN page and this Stack Overflow thread for more details on the Visual Studio equivalent.
    Last edited by Schol-R-LEA-2; 06-04-2022 at 03:48 PM.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    115

    Thumbs up Solved!

    Thanks Schol I'll check that out, although it might be a bit over my head atm

    I checked out the definition of CreateCommandQueue and sure enough it takes THREE arguments, just like you said John. So the macro becomes two arguments (hence the comma!).

    This for me is now solved, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is this macro style definition?
    By Nikosant03 in forum C Programming
    Replies: 5
    Last Post: 12-20-2021, 04:32 PM
  2. Macro definition
    By coder222 in forum C Programming
    Replies: 3
    Last Post: 08-27-2015, 03:45 PM
  3. Matrix product using macro definition
    By ElNoob in forum C Programming
    Replies: 5
    Last Post: 12-08-2012, 09:44 PM
  4. what is wrong with the macro definition....
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 04-28-2010, 01:26 AM
  5. Macro definition
    By aayu09 in forum C Programming
    Replies: 11
    Last Post: 04-12-2010, 02:33 PM

Tags for this Thread