tags:

views:

183

answers:

1

I have the following line of code in my application:

CString strAppName = AfxGetAppName();

Sometimes it fills strAppName up with garbage characters, and I can't figure out why.

Anyone have any ideas?

TIA.

+3  A: 

That is possible if you change m_pszAppName manually.

At the very beginning of application initialization, AfxWinInit calls CWinApp::SetCurrentHandles, which caches the current value of the m_pszAppName pointer as follows:
pModuleState->m_lpszCurrentAppName = m_pszAppName;

That is, the module state struct holds a copy of the m_pszAppName pointer. If you change m_pszAppName in InitInstance as adviced in MSDN, you still have the old pointer value in pModuleState->m_lpszCurrentAppName. The AfxGetAppName() function returns AfxGetModuleState()->m_lpszCurrentAppName.

You could add data breakpoint on m_lpszCurrentAppName and on m_pszAppName. It is initialized in <...>\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\appinit.cpp file. You'll see what is going on with that variable and who's trying to change it.

Kirill V. Lyadvinsky
My application does not change `m_pszAppName`. It changes some registry keys though, could that be the issue?
Justin
I don't think that changing registry keys could implicitly change `m_pszAppName`.
Kirill V. Lyadvinsky