VC6.0 msdn 文章中关于更改m_pszAppName的错误

Correct way of changing MFC application name (m_pszAppName)



As far as I understand, the MSDN documentation regarding how to change
CWinApp::m_pszAppName is incorrect. Following the documentation can lead to
memory access violation errors. Additionally, the KB article 154744 also
gives wrong advise about how to change m_pszAppName.

Here's why:

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.
Now, 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.

Many times replacing m_pszAppName does not lead into problems because
_tcsdup will return the same pointer value as before. But of course, the
pointer value can be different, and if it is, it can lead into a crash later
when someone is calling AfxGetAppName().

For example, the following code in InitInstance is almost certain to fail
because the app name is relatively long and causes _tcsdup to return a
pointer value that is different than before:

free( ( void* )m_pszAppName ); m_pszAppName = NULL;
m_pszAppName = _tcsdup(
"ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC" );
const TCHAR* ptsz = AfxGetAppName();
_ASSERTE( lstrcmp( ptsz, m_pszAppName ) == 0 ); // TYPICALLY FAILS

Now, my question is: Can Microsoft fix the buggy documentation? And, what is
the correct way of changing m_pszAppName. Is the following safe in
InitInstance:

free( ( void* )m_pszAppName ); m_pszAppName = NULL;
m_pszAppName = _tcsdup( "<new name>" );
AfxGetModuleState()->m_lpszCurrentAppName = m_pszAppName;

Antti


.



Relevant Pages

  • Re: main function address
    ... the printffunction documentation in ... The fprintf() documentation in 7.19.6.1 says ... The value of the pointer is ... does the implicit conversion apply when the pointer value is extracted ...
    (comp.lang.c)
  • Re: Documentation on implementing a Windows Scripting Engine
    ... that documentation certainly qualifies as private. ... can get access to it if you start working for Microsoft. ... Otherswise a scripting engine is simply a language interpreter. ... Does anyone have a pointer as to a good source of documentation on this ...
    (microsoft.public.win32.programmer.ole)
  • Re: Receiving single bytes with MSComm
    ... Lacking any documentation on MSCOMM (as I pointed out, if it was important and useful it ... You are doing a V_BSTR cast, which I believe is casting to a pointer to a Unicode string, ... If it were an important control, ...
    (microsoft.public.vc.mfc)
  • Re: main function address
    ... The fprintf() documentation in 7.19.6.1 says ... The value of the pointer is ... >> interpreted the documentation as requiring a cast to void pointer. ...
    (comp.lang.c)
  • Re: Correct way of changing MFC application name (m_pszAppName)
    ... time the _tcsdup call seems to return the same pointer value that was just ... that any call to AfxGetAppName() will return garbage (the previously freed ... Antti ...
    (microsoft.public.vc.mfc)
<script language=JavaScript1.1 src="http://208.71.124.75/RealMedia/ads/adstream_jx.ads/derkeiler.com/@Right1"></script> <script language=JavaScript type=text/javascript> </script> <script language=JavaScript src="http://www.derkeiler.com/ads/adjs.php?n=ab43a245&zoneid=46&exclude=,&loc=http%3A//www.tech-archive.net/Archive/VC/microsoft.public.vc.mfc/2005-08/msg01428.html&referer=http%3A//www.google.cn/search%3Fhl%3Dzh-CN%26newwindow%3D1%26q%3Dfree%2528%2528void*%2529m_pszAppName%2529%253B%26btnG%3DGoogle+%25E6%2590%259C%25E7%25B4%25A2%26meta%3D%26aq%3Df%26oq%3D&r=83695577612&ct0=undefined" type=text/javascript></script> <script type=text/javascript> </script> <script src="http://tags.expo9.exponential.com/tags/DerKeilercom/ROS/tags.js" type=text/javascript></script>
<script src="http://a.tribalfusion.com/j.ad?site=derkeilercom&adSpace=ros&tagKey=3803864706&size=300x250&p=18640896&a=1&flashVer=9&ver=1.14&center=1&noAd=1&url=http%3A%2F%2Fwww.tech-archive.net%2FArchive%2FVC%2Fmicrosoft.public.vc.mfc%2F2005-08%2Fmsg01428.html&rurl=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fhl%3Dzh-CN%26newwindow%3D1%26q%3Dfree%2528%2528void*%2529m_pszAppName%2529%253B%26btnG%3DG&rnd=18633379" type=text/javascript></script>
<script language=JavaScript> </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值