Archive for the ‘C++’ Category

Fix: Breakpoints in wrong lines (=offset) in Visual Studio 2012

Friday, December 11th, 2015

Just had a very weird issue in VS2012 which cost me about an hour, so maybe this helps others.

When I placed breakpoints in a certain file, they more in fact displayed one line *below*. Sometimes even in blank lines. And often they didn’t work. This happened only in one file and only after a certain function. I tried a lot of things regarding the function, but that didn’t help.

It turned out that somehow the newlines codes for wrong here. If you look at the file in a hex editor, newline codes are normally:
OD 0A

At the problem location it was only:
0D

And that seemed to cause headaches to VS. Copying the entire text into an alternative editor and then back again fixed the file.

C++ weirdness

Tuesday, August 26th, 2014

 

I’m not sure if it’s an official C++ weirdness or just Microsoft’s version.

But this seems to be legal C++ code as I discovered by accident (=bug).

long x=0;
CString s = x[“test”];  // No compiler warning. Result is “t”

Storing crash relevant information

Saturday, June 22nd, 2013

Sometimes it’s helpful to store debug information on a regular basis somewhere and recover it if the program is crashed and restarted to get info about the cause of the crash. Unfortunately, there’s no perfect way for storing this data in Windows. And you don’t want to slow down your program to much. So I tried a benchmark and compared RegSetValueEx against SetFilePos(0) + WriteFile. (In both cases the handle was opened just once then used for writing 100.000 times.

And it turned out that WriteFile is 10x faster. More interesting facts:

– FILE_ATTRIBUTE_TEMPORARY didn’t change a thing.

– My SSD is fast (298ms), but my hard disk is faster (268ms, don’t ask me why) and my Ramdisk faster than both (206ms).

Dangerous MakeLower

Thursday, May 2nd, 2013

Got weird crashes at customers with foreign languages? Microsoft’s MFC’s MakeLower could be the culprit. Seems to be poorly written because it can crash if it doesn’t like the string. I could even reproduce it here by switching the Windows-Locale for non-unicode apps the Japanese and process some strings contains German umlauts. Bottom-line? Don’t use, write your own… 🙁

Using Visual Studio 2012… Safely…

Thursday, January 10th, 2013

Microsoft Visual Studio 2012 isn’t so bad. 😉 I actually even like new dark looks. But (for C++) you shouldn’t use the default settings, our you’ll get problems with your older customers.

 

1. Making it run with XP

Go to “configuration properties > general” and switch the “Platform toolset” to “Visual Studio 2012 – Windows XP (v110_xp)”.

 

2. Making it run on older systems

This caused me hours of work. By default VS2012 will “optimize” fp-operations. But it doesn’t work on all systems. Don’t know if it’s the older processors but I had at least 2 customers where a simple statement as “myfpvar=1” caused the program to terminate without error message. To fix this go to “C++> Code generation” and set “Enable enhanced instruction set” to “No Enhanced Instructions (/arch:IA32)”.

Weird TextOut Unicode problems

Friday, October 26th, 2012

After porting Easy2Sync for Files to Unicode I experienced some really weird problems with some foreign characters. Unicode in general worked, Japanese characters were displayed nicely (not that I could read them). But Cyrillic characters failed when printed with TextOut and were displayed as small black rectangles.

Even weirder: Adding just 1 Japanese character to a string with Cyrillic characters caused the entire string to be printed right.

It seems that TextOut is only 50% Unicode compatible (of course Microsoft doesn’t mention this). So one has to use DrawText instead, which doesn’t seem to have these limitations. Since DrawText behaves differently regarding the position and background drawing, here’s a wrapper which fixes this and makes it behave like ExtTextOut:

void FixedExtTextOut (CDC *pDC, int x, int y, UINT nOptions, LPCRECT lpRect, LPCTSTR lpszString, UINT nCount, LPINT lpDxWidths)
{
 CRect rr(x, y, lpRect->right, lpRect->bottom);

 pDC->FillSolidRect(lpRect, pDC->GetBkColor());
 pDC->DrawText(lpszString, nCount, &rr, 0);
}

You’re not gonna catch this…

Thursday, June 14th, 2012

You're not gonna catch this... (LIttle League baseball, May 2009 - 03 by Ed Yourdon; CC BY-SA 2.0)

C++ exceptions can be surprising. This works easily:

try
{
throw(1);
}
catch(…) //Catch all
{
//caught
}

But if you replace “throw(1);” with “throw;”, it suddenly doesn’t work anymore. Code compiles, exception is thrown, but it’s not caught. The program is terminated

MFC: Redirecting OnMouseWheel messages to the window under cursor

Wednesday, June 6th, 2012

By default MFC applies the MouseWheel message to the window that has the focus. Even though most users expect it to apply to the window under the cursor. (And even Microsoft’s style guide recommends that.)

To fix this, override the program’s PreTranslateMessage  function and add this:

 if (pMsg->message==WM_MOUSEWHEEL)
 {
    CPoint point;
    GetCursorPos (&point);

    HWND hWnd = ::WindowFromPoint (point);

    if (hWnd && pMsg->hwnd!=hWnd)
    {
       ::PostMessage(hWnd, pMsg->message, pMsg->wParam, pMsg->lParam);
       return(true);
    }
 }

Please note that you have to add this  before calling the default implementation of PreTranslateMessage.

Turning off floating point exceptions against crashes

Friday, June 1st, 2012

It sounds quite mad, but either of these lines can crash a program: (C++, Visual Studio 2005)

long x = long(my_float_var);
float f = exp(my_float_var);

If the number is too large/small, the program is terminated. (No, try/catch doesn’t help.) And to make it even stranger, this happens only on some computers. And of course it happened on the customer’s computer and not on mine…

To fix this, it helps to turn off the floating point exceptions:
control87(_EM_INVALID|_EM_DENORMAL|_EM_ZERODIVIDE|_EM_OVERFLOW|_EM_UNDERFLOW|_EM_INEXACT, _MCW_EM);

Of course the problem of the large number still exists, but now the program at least continues to run, and I have a chance to handle it.

EnumResourceNames + RT_STRING

Friday, May 4th, 2012

It sounds pretty straightforward to enumerate the resources  in a file. But it turned out to be rather a lot of work (and waste of time), since the MS doesn’t tell you, that it doesn’t enumerate the strings, but blocks of strings in a binary format that you have to process yourself.

Here’s how to do it:

BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
{
 switch ((int)lpszType)
 {
 case RT_STRING:
 {
 HRSRC hRes = FindResourceEx(hModule, RT_STRING, MAKEINTRESOURCE(lpszName), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));

 if (hRes)
 {
 HGLOBAL hGlo = LoadResource(hModule, hRes);
 LPCWSTR lpStr = (LPCWSTR)LockResource(hGlo);
 DWORD dwsize = SizeofResource(hModule, hRes);

 long count=0;
 for (int i=0; i < (int)dwsize; i++)
 {
 count++;
 if (count>=16)
 break;

 if (lpStr[i])
 {
 WORD *pString = lpStr+i+1;
 long lString = lpStr[i];

 //Process string here

 i += lpStr[i];
 }
 }
 }
 }
 break;
 }

 return(TRUE);
}

EnumResourceNames(hModule, RT_STRING, &EnumResNameProc, NULL);