CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2013
    Posts
    161

    c++ macro syntax

    I'm learning winapi and I'm going through this source I found online and there is this macro which is making me go...
    here goes the snippet...
    Code:
    #define CWA(dll, api)                 ::api
    and was used like so

    Code:
     CWA(user32, DialogBoxParamW)(currentModule, MAKEINTRESOURCEW(DIALOG_MAIN), NULL, mainDialogProc, NULL);
    but going through my c++ books, this macro syntax does not conform to what is in my books getting me a little bit
    confused, master victorN on this forum linked me a page on stackoverflow which touched on the subject but I still can't get how the syntax look different from that in my c++ books... the double brackets is confusing me..what syntax is this??? CWA()() ; ???
    the general syntax I know of should be like this ...
    thanks
    Code:
    // function macro
    #include <iostream>
    using namespace std;
    
    #define getmax(a,b) ((a)>(b)?(a):(b))
    
    int main()
    {
      int x=5, y;
      y= getmax(x,2);
      cout << y << endl;
      cout << getmax(7,x) << endl;
      return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: c++ macro syntax

    The previous thread is here http://forums.codeguru.com/showthrea...function-calls - so why create a new thread re the same topic - why not reply to the existing thread?

    I thought the link provided by VictorN explained it. However, here's another attempt.

    #define defines a macro. A macro does in-line text replacement. Simply
    Code:
    #define MYMACRO "macro text"
    replaces every occurance of MYMACRO in the code from the point of definition with the string "macro text".

    Code:
    #define BYSIX(a) ((a) * 6)
    replaces BYSIX(n) with ((n) * 6) where n is the paramater to BYSIX which can be anything eg
    Code:
    int a = 3;
    int b = BYSIX(a);
    causes BYSIX(a) to be replaced with
    Code:
    int b = ((a) * 6);
    before the code is compiled.

    You could even have
    Code:
    int asdfg = 2;
    int z = BYSIX(asdfg);
    which would be converted to
    Code:
    int z = ((asdfg) * 6);
    which if asdfg is defined would be compilable code.

    The macro substitution is done in the pre-compile phase before the code is compiled. Thus all the macro substitutions are done during a pre-compile pass at the start before the code is compiled.

    Now looking at
    Code:
    #define CWA(dll, api)                 ::api
    This is a macro. It takes 2 arguments. The first one is not used and the macro substitution replaces CWA(x, y) with ::y

    So in the example usage
    Code:
    CWA(user32, DialogBoxParamW)(currentModule, MAKEINTRESOURCEW(DIALOG_MAIN), NULL, mainDialogProc, NULL);
    CWA(user32, DialogBoxParamW) is replaced with :: DialogBoxParamW. So this statement is replaced by
    Code:
    ::DialogBoxParamW((currentModule, MAKEINTRESOURCEW(DIALOG_MAIN), NULL, mainDialogProc, NULL);
    and then this code is compiled.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2013
    Posts
    161

    Re: c++ macro syntax

    thanks master 2kaud and sorry for opening another thread instead of keeping up with the old one, I was thinking nobody will see the old thread since is old some days and keeps going down on the list, thats why I opened a new one..sorry..


    This is a macro. It takes 2 arguments. The first one is not used and the macro substitution replaces CWA(x, y) with ::y
    good..why is the first argument not used? why is it ignored? when is an argument not used or ignored??? and can you please shed some light on the global variable :: api??? do you think it should be somewhere in the source code or is a winapi global variable?
    thanks for your very quick reply

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: c++ macro syntax

    Quote Originally Posted by TheLionKing View Post
    thanks master 2kaud and sorry for opening another thread instead of keeping up with the old one, I was thinking nobody will see the old thread since is old some days and keeps going down on the list, thats why I opened a new one..sorry..
    The thread will go up after one posts a new message to it. So don't worry about it!
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: c++ macro syntax

    Quote Originally Posted by TheLionKing View Post
    thanks master 2kaud and sorry for opening another thread instead of keeping up with the old one, I was thinking nobody will see the old thread since is old some days and keeps going down on the list, thats why I opened a new one..sorry..



    good..why is the first argument not used? why is it ignored? when is an argument not used or ignored??? and can you please shed some light on the global variable :: api??? do you think it should be somewhere in the source code or is a winapi global variable?
    thanks for your very quick reply
    In the context of this macro, the first argument is for info purposes only - the name of the dll in which the api is located. How/when macro arguments are used in the macro substitution is a matter for the individual macro.

    ::api is NOT a global variable. api is one of the arguments of the CWA macro and is only scoped in the context of that macro for substitution purposes. For the CWA macro, the macro is replaced by ::api
    Code:
    CWA(qwert, asdfg)(a, b, c);
    becomes
    Code:
    ::asdfg(a, b, c);
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jul 2013
    Posts
    161

    Re: c++ macro syntax

    thanks master VictorN
    Last edited by TheLionKing; March 15th, 2016 at 05:41 PM.

  7. #7
    Join Date
    Jul 2013
    Posts
    161

    Re: c++ macro syntax

    ooohh thanks Master 2kaud for all your patience, I understand it now..thanks

  8. #8
    Join Date
    Jul 2013
    Posts
    161

    Re: c++ macro syntax

    thanks again..soo interesting

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: c++ macro syntax

    For a more detailed technical discussion, see https://msdn.microsoft.com/en-us/library/teas0593.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Jul 2013
    Posts
    161

    Re: c++ macro syntax

    thanks master 2kaud!

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: c++ macro syntax

    There is more in play than just that macro. The macro just expands to user32:ialogBoxParamW.

    Most likely there is other code (or macros) that define a global class with static class methods that represent exported functions with a dll.

    The plumbing takes the module instance and the additional parameters for the api. Most likely you'll find additional methods defined, like user32::FindDialog, etc.

    This needs to be done up front. What you are looking at is the code that calls a method once the dll has been loaded with LoadLibrary and a corresponding GetProcAddress call.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured