Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

C++
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

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.
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900