-
March 15th, 2016, 04:39 PM
#1
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;
}
-
March 15th, 2016, 05:06 PM
#2
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
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)
-
March 15th, 2016, 05:20 PM
#3
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
-
March 15th, 2016, 05:26 PM
#4
Re: c++ macro syntax
Originally Posted by TheLionKing
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
-
March 15th, 2016, 05:37 PM
#5
Re: c++ macro syntax
Originally Posted by TheLionKing
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
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)
-
March 15th, 2016, 05:39 PM
#6
Re: c++ macro syntax
Last edited by TheLionKing; March 15th, 2016 at 05:41 PM.
-
March 15th, 2016, 05:40 PM
#7
Re: c++ macro syntax
ooohh thanks Master 2kaud for all your patience, I understand it now..thanks
-
March 15th, 2016, 05:42 PM
#8
Re: c++ macro syntax
thanks again..soo interesting
-
March 15th, 2016, 05:43 PM
#9
Re: c++ macro syntax
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)
-
March 15th, 2016, 05:47 PM
#10
Re: c++ macro syntax
thanks master 2kaud!
-
March 15th, 2016, 06:12 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|