Posts

Showing posts with the label macros

Conversion macros\inline functions

sometimes you might need to convert (safely) between different Windows data types. for example, consider this: you have a dialog window procedure and you want to handle WM_CTLCOLOREDIT message to change the background color of the edit control. BOOL CALLBACK DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch(uMsg) { case WM_CTLCOLOREDIT: return (BOOL)(GetStockObject(DKGRAY_BRUSH)); } } I followed the instructions from MSDN for WM_CTLCOLOREDIT: "If a dialog box procedure handles this message, it should cast the desired return value to a BOOL and return the value directly." the problem is it works but it gives a warning: Compiler Warning (level 1) C4311. "This warning detects 64-bit portability issues. For example, if code is compiled on a 64-bit platform, the value of a pointer (64 bits) will be truncated if it is assigned to an int (32 bits). This warning is only issued when /Wp64 is used. " I am using VisualC++ 2003 compiler so it seems /w...

watch out for those macros.. "macros are evil !"

macros are evil ! i want to set an item text in a listview control representing a date ListView_SetItemText(hWnd, iIndex, 1, log.GetTimeString().GetBuffer(0)); the log object is like this: class CLog : public CObject { ... CString GetDateString() { return m_datetime.Format(_T("%d/%m/%Y")); } CString GetTimeString() { return m_datetime.Format(_T("%H:%M:%S")); } ... } ListView_SetItemText(hWnd, iIndex, 1, log.GetTimeString().GetBuffer(0)); is bad because the CString object returned by the GetTimeString is temporary (and valid only in that line) but the ListView_SetItemtext is a macro ! looking inside it, it looks like this: #define ListView_SetItemText(hwndLV, i, iSubItem_, pszText_) { LV_ITEM _ms_lvi;_ms_lvi.iSubItem = iSubItem_;_ms_lvi.pszText = pszText_;SNDMSG((hwndLV), LVM_SETITEMTEXT, (WPARAM)(i), (LPARAM)(LV_ITEM *)&_ms_lvi);} so once the buffer is set to _ms_lvi.pszText the CString object is destroyed and the SNDMSG will send an invalid buffer