Click here to Skip to main content
13,900,697 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
A macro LogTxt is defined as follows:

#define LogTxt(...) \
	BOOL bRet1 = SetEvent(hEvt);     \
	ATLTRACE(__VA_ARGS__)


In the middle of a source, LogTxt use GetLastError as an argument.
...
HANDLE hEvt;
HWND hWnd;
BOOL bRet2 = ShowWindow(hWnd, 1);
LogTxt("GetLastError=%d", GetLastError());
...

I am expecting LogTxt prints the error number for ShowWindow, but the result is the error number for SetEvent.
__VA_ARGS_ macro is not safe to use with GetLastError as an argument.

What I have tried:

I tried

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow)
{
	HANDLE hEvt;
	HWND hWnd;
	BOOL bRet2 = ShowWindow(hWnd, 1);
	LogTxt("GetLastError=%d", GetLastError());
	return 0;
}


and the expected result was 1400, but the actual result was 6

GetLastError=6

=> So, __VA_ARGS__ is not safe to use GetLastError as an argument.
Posted
Updated 14-Mar-19 13:33pm
v5
Comments
Mohibur Rashid 14-Mar-19 16:48pm
   
GetLastError returns int, i dont see any issue, in this context looks no problem.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Note, in the code posted, the defined macro is LogTxt while macro calls use LogFTxt.

In any case, your problem is not the variadic macro. It is, instead, the SetEvent function call in the macro, issued before the GetLastError one. That is GetLastError reports about the SetEvent execution.
   
Comments
Maciej Los 15-Mar-19 6:21am
   
5ed!
CPallini 15-Mar-19 7:27am
   
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190306.1 | Last Updated 14 Mar 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100