2014-02-09 35 views
0

我中定義的宏來DbgPrint到打印消息時_DEBUG定義DbgPrint輸出的不同形成的宏輸出到DbgPrint

#define MYDBGPRINT(X) #ifdef _DEBUG \ 
    DbgPrint(X) \ 
#endif 

但輸出是從DbgPrint不同例如

ULONG id=0; 
MYDBGPRINT("the value of the id : %u ",id) //outputs garbage 
DbgPrint("the value of the id : %u ",id) //outputs the correct value of id 

回答

0

您正試圖使用​​預處理器來創建預處理器定義。我從來沒有嘗試過自己,但它似乎是一個壞主意,可能無法正常工作。相反,你可以試試:

#ifdef _DEBUG 
#define MYDBGPRINT(X) DbgPrint(X) 
#else 
#define MYDBGPRINT(X) 
#endif 

或具有可變數量的參數,以宏的情況:

#ifdef DEBUG 
#define MYDBGPRINT(...) DbgPrint(__VA_ARGS__) 
#else 
#define MYDBGPRINT(...) 
#endif 
+0

注意,__VA_ARGS__方法可能是編譯器特定的。我已經使用它與GCC,但我不確定它是否與微軟的編譯器一起工作。 – jduck

+0

第二種解決方案是可以的。我必須使用可變宏http://en.wikipedia.org/wiki/Variadic_macro – zaki