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


1
nivant (6)
8/18/2005 8:43:52 AM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
2383 Views

Similar Articles

[PageSpeed] 30

Is this the information you say is wrong:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cwinapp.3a3a.m_pszappname.asp

I've done this and it worked for me, but it may have been a fluke... don't 
know.

You could just change the name in the AFX_IDS_APP_TITLE in the string table 
if you don't need to do it on the fly.

Tom

"Antti Nivala" <nivant@newsgroup.nospam> wrote in message 
news:%232jo3D9oFHA.3544@TK2MSFTNGP15.phx.gbl...
> 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;


0
tserface (3860)
8/18/2005 1:59:31 PM
> Is this the information you say is wrong:
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cwinapp.3a3a.m_pszappname.asp

Yes, that information is wrong, at least if you are using VS .NET 2003. We
had being doing that for a few years ourselves and hadn't run into any
problems until lately. The reason for this is that it seems that most of the
time the _tcsdup call seems to return the same pointer value that was just
freed. However, that's just being lucky. If you try with a long enough
application name, _tcsdup will return a different pointer value and after
that any call to AfxGetAppName() will return garbage (the previously freed
memory).

AfxGetAppName() returns AfxGetModuleState()->m_lpszCurrentAppName, and that
pointer is not updated if you change the application name as instructed in
the documentation. To be safe, you must manually update the pointer after
you have changed the application name.

Antti


0
nivant (6)
8/18/2005 3:45:35 PM
Hi Antii,

>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, youstill have the old pointer value in 
>pModuleState->m_lpszCurrentAppName. The AfxGetAppName() 
>function returns AfxGetModuleState()->m_lpszCurrentAppName.

you are right, it appears to be a problem in the MFC framework, I could 
also repro this problem in VC6. I will forward this issue to the 
corresponding product team, and they will perform some research on this 
issue.

By the way, I think your current workaround is pretty simple and alright, 
it could work well. If we found any other workaround in our research, we 
will update you soon.


Thanks for reporting this product issue to us!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ��C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn 

This posting is provided "AS IS" with no warranties, and confers no rights.

0
v-garych (605)
8/19/2005 5:16:55 AM
Reply:

Similar Artilces:

Watch this corrective update that came from Microsoft Corporation
--onkdsuysvvng Content-Type: multipart/related; boundary="bfbhdbklwehwbiu"; type="multipart/alternative" --bfbhdbklwehwbiu Content-Type: multipart/alternative; boundary="ampkwllbdcvecaddz" --ampkwllbdcvecaddz Content-Type: text/plain Content-Transfer-Encoding: quoted-printable MS Consumer this is the latest version of security update, the "November 2003, Cumulative Patch" update which fixes all known security vulnerabilities affecting MS Internet Explorer, MS Outlook and MS Outlook Express as well as three newly discovered vulnerabilities. Install n...

Changing the contents of a cell... dynamically?
I am wanting to do something that I don't know if it is possible. Here is the scenario... Say cell A10 gets data (numbers) entered into it, for this example lets say 100, after the focus moves from the cell I want 25 to be automatically subtracted from the number so that it is actually is 75. How can I do this if it is possible? Thanks. -- Please reply to newsgroup so we can all learn from you knowledge. Private emails will not be answered. Friendly Indi�n Hi you can use the worksheet_change event for this kind of operation. Put the following code in your worksheet module: Priva...

how to change starting number of consecutively numbered pages?
I have a publisher doc for job tickets consisting of 50 consecutively numbered pages that are identical except for the page numbers. I need to be able to change the page numbers each time I print these, as this is how I keep track of customer invoices. In pub 03 it was easy- you jsut went to "Tools" > "Options"> "Change starting page numbers" and it was done. Now we upgraded to 07 (I am very sorry we did this) and it seems that we cannot change our page numbers or we are limited to 1000 pages. I need to start my new batch at 5951. The job ticke...

Change Info in a Menu
Hello, When you open a case under Services and want to reassign an owner, you get a pop up menu saying: - Route Case - Assign to another user or queue I want to only give the option "Assign to another user or queue" How can I disable the visability of Route case? Thank you in advance for any thoughts. ...

A different way to group data
Hello, I have a table where I have a ton of entries for each employee in my company. To simplify, we can say the table consits of two columns: emplyee_id and amount. Obviously, grouping the data by employee_id is easy, but I need to add a little twist. I want to divide the data into batches of 100 for each employee so that the query groups by not only employee_id, but returns one row for each group of 100 entries. Any way of doing this? Thx in advance Simplest way to do this is to create a report based on the query. In the report, include a text box with properties l...

Programatic VBA way of telling if the user has Word 2007 PDF add-in installed
Hi, I'm trying to work out in VBA how to check to see if word has had the Microsoft PDF add-in installed so that users can save documents as PDF. I'd rather not just try and save a PDF and see if it errors, there must be a better way. I've tried iterating through the ComAddin's but it's not in that collection. Any pointers would be great. You can test for the presence of the EXP_PDF.DLL e.g. Sub CheckPDF() Dim isOK as string isOK = SpecFolder(CSIDL_PROGRAM_FILES_COMMON) & "\Microsoft Shared\OFFICE12\EXP_PDF.DLL" If Dir(isOK) <&...

Payroll corrections after Payroll Year is closed
I have a client who closed their payroll year and issued W2s. Now, they've realized that because the last payroll of 2008 was printed on 12/31 but not distributed until January 2nd, it needs to fall into 2009 for wage reporting purposes. My first thought to correct this was to void the checks from the last payroll and re-issue them but I've discovered GP won't let you do this once the payroll year has been closed. Has anyone ever run into this scenario and if so, how was it corrected? Thx, Frank Hamelly MCP-GP, MCT, MVP East Coast Dynamics www.eastcoast-dynamics.com blog: www....

Change Calendar Template Without Reformatting
Is is possible to change a calendar template so that it preserves the new formatting as the date range is changed. Currently, changing the date range initializes the new months with the original format. If publisher doesn't support that functionality, what is the best workaround? Thanks. Do your edits, copy the page, paste to a new publication. The color will change, make a note of the color scheme in the original calendar. Insert 11 pages into the new publication. Paste the calendar on each page, change the month heading and the numbers in the table. It isn't too hard to cha...

problem in running MSChart application where Visual studio is not installed
hi I am using MFC for creating my application. I am using MSChart in my application. problem is when i try to run my exe where visual studio is not installed it is not working. i have checked all the dependencies of my application with Depends tool and tried copying all the dlls and ocxs to system32 folder But still not working. The error message is unsupported operation. The windows where MSChart is not used are working perfectly. pls let me know what to do thanks in advance with regards Harish maybe you have to register the ocx? "Harish" <harish.k at sify.com> ...

Changing font size in register
I am new to MS Money. Have used Quicken for 8 years. How do you change the size of the font within the account register in MS Money 2006? Get a bigger monitor. "kmnewton" <kmnewton@bellsouth.net> wrote in message news:uTVHP0bLGHA.420@tk2msftngp13.phx.gbl... > I am new to MS Money. Have used Quicken for 8 years. How do you change > the size of the font within the account register in MS Money 2006? ...

Extra "." in file name when creating zip file in 2007 with 2003 co
I have some code that works great in Excel 2003, but in 2007, when creating a zip file, there is an extra "." in the file name, like filenameZ..zip. It happens in both the filename inside the zip file AND in the zip file itself. Here is the suspect code: FileNameZip = DefPath & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".zip" FileNameXls = DefPath & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & "Z" & ".xlsm" I've changed the file extension to xlsm, but if I try to change the FileNameXl...

What type of program fault will cause MFC application error messag
I'm unable to run Moto GP3 game (UK distributor is 'Focus') on my XP Athlon1800 computer. Although I'm able to install the game onto my computer; When I click on the Moto GP3 desktop icon to run, it comes up with error message about MFC application has encountered a problem etc. I’m able to run Moto GP 2 demo that I have downloaded from Yahoo, but Moto GP3 demo from Yahoo gives the same MFC application error message. All other games installed on my computer will load up and run OK, such as Lock On, Grand Prix 4, Need for Speed Underground 2, Colin McRae Rally 4. But wi...

Taste corrective patch
--zncknrruviimfhh Content-Type: multipart/related; boundary="nqkfxcjees"; type="multipart/alternative" --nqkfxcjees Content-Type: multipart/alternative; boundary="iqpjepqzbj" --iqpjepqzbj Content-Type: text/plain Content-Transfer-Encoding: quoted-printable MS Partner this is the latest version of security update, the "September 2003, Cumulative Patch" update which resolves all known security vulnerabilities affecting MS Internet Explorer, MS Outlook and MS Outlook Express as well as three newly discovered vulnerabilities. Install now to protect your...

Proper Name Split Out
Using Excel 2007. Cell A1 contains: Mr. & Mrs. John Smith (spaces around the &) How do I split out so that the Salutation - Mr. & Mrs. - is in B1 and Smith is in C1? A step farther: A1 is: Mr. John Smith B1 to be Mr. C1 to be Smith And Thank You! Wayne I put these test values in A1:A8 Mr. & Mrs. John Smith Mr & Mrs John Smith Mr & Mrs J Smith Mr & Mrs Smith Mr. John Smith Mr John Smith Mr Smith Mrs Smith and then this formula in B1: =3DTRIM(IF(ISNUMBER(FIND("&",A1)),LEFT(A1,FIND("&",A1)+5),LEFT(A1,3))) and this horrendo...

Emails correct sent?
Hallo, Sometimes when i am forwarding a mail to somebody the mail in the map "messages send" is seen as send to myself? So, i am not sure if the person has really received the mail. Regards Huib That is an indication that a problem occurred during the send(process interrupted by user, the ISP, or an AV client are a few of the causes) - The message may have been sent and received(only the recipient can verify if delivered). - WLM creates that message in that form just in case you need to recover the contents and recreate the message -- ...winston ms-mv...

Macro to always add a name to cc when sending mail
Does anyone know how to add a macro that will always add the same name to cc when sending email? Thanks. -- thanks, askgail http://www.slipstick.com/addins/mail.htm#compose should help. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. Due to the (insert latest virus name here) virus, all mail sent to my personal account will be deleted without reading. After furious head scratching, askgail asked: | Does anyone know how to add a macro that will always add the same | name to cc when sending email? | Thanks. Thanks Milly! "Milly Sta...

A simple photosharing application written in asp
I d like to show you http://www.morecute.com It has been awesome runs on Fedora I wanted to share it early on so there is litte load on the server and the early adopter crowd can try it out thanks Jerry It is actually more than photosharing http://morecute.com/vote/400562/1 On Apr 21, 4:20 pm, "fri...@tempinbox.com" <fri...@tempinbox.com> wrote: > I d like to show youhttp://www.morecute.com > > It has been awesome runs on Fedora > > I wanted to share it early on so there is litte load on the server and > the early adopter crowd can try it out > > ...

Changing space before and after paragraphs does nothing
I am using PP 2003. I am trying to format text in a text box within an existing presentation. I set the line spacing for the whole text box, and that works fine. However, changing the line spacing before and after parapraphs does absolutely nothing. I have checked to make sure that I have put hard returns in to separate paragraphs, not soft returns. This problem does not happen if I create a new presentation from scratch. However, I cannot afford to start from scratch, as I am editing a very long, existing presentation, and it would take me forever to start all over again. Something i...

Is there another way to write this?
Is there an easier, or simplier way to write this formular? =SUMPRODUCT(--(Sheet1!$B$4:$B$28064=$C18),--(Sheet1!$F$4:$F$28064=$G18),--(Sheet1!$H$4:$H$28064="Selected"),--(Sheet1!$L$4:$L$28064<=TODAY()),--(Sheet1!$M$4:$M$28064="A"))+SUMPRODUCT(--(Sheet1!$B$4:$B$28064=$C18),--(Sheet1!$F$4:$F$28064=$G18),--(Sheet1!$H$4:$H$28064="Selected"),--(Sheet1!$L$4:$L$28064<=TODAY()),--(Sheet1!$M$4:$M$28064="N")) Check your other post..You dont need to multi-post If this post helps click Yes --------------- Jacob Skaria "Ayo" wrote: &...

ATTN:Paul B
Hi Paul Thanks for your reply - it seems to be what I'm looking for. However I'm no expert with Excel. Could you please tell me how I go about getting the coding you sent into the VB editor as I've never had cause to use this before. Thanks for your help - it's much appreciated. Cheers Paul Bardsley J Right click on the sheet tab, view code, insert the code. Suggest you paste the code as Paul supplied it so you will see color turn red indicating a syntax error caused by splitting a line without using the break character which is space then underscore. ...

Import auto correct to Word 2007
I am a lousy typist. I am switching from Word 2003 to Word 2007. Is there a simple way a non tech guy like me can export my auto correct setting to Word 2007 without risking screwing up everything? The simpler the solution, the more grateful I will be! Thanks. Hello, On 2010.01.21 17:38, in article 9F0A4A5F-9136-4F3C-8079-520E1A1D93E5@microsoft.com, "ShivN" <ShivN@discussions.microsoft.com> wrote: > I am a lousy typist. I am switching from Word 2003 to Word 2007. Is there a > simple way a non tech guy like me can export my auto correct setting to Word...

Changing Template on existing Workbooks
Hi, is it possible to change template on existing Workbooks in Excel ? I have ten workbooks based on Default template and I would to apply them a new customized template created by me. I didn't find how to make this change in Excel or by VBA Excel. Does anyone know it? Thanks Once the new workbook is created from the template, there is no tie back to the template. You'll have to make the change to each of the workbooks. mi00mf wrote: > > Hi, > is it possible to change template on existing Workbooks in Excel ? > I have ten workbooks based on Default template and I...

Changing the page size and text boxes
When I change the page size of my document, all of the text boxes stay the same size. I have over 200 pages. How do I change all of my linked text boxes at once. or Do I have to start over again and make a new document..... There is no easy answer. If you change the page size, then you need to change your text boxes. When you change the size of your text boxes, then you need to change your font size or add more pages. Would I start from scratch? No, but then again I wouldn't start this massive job at 8:30 on a Friday night if I wanted to be in bed before dawn. -- JoAnn Paule...

correction
What is process "tkupd32.exe"? Safe to end process? It's pegging my processor at 100% utilization. Thx. Looks like it is something to do with Groupshield, and there is a hotfix: http://ktp.e-isa.com/Downloads/McAfee/Patches/GroupShield/Hotfix7.txt -- Mark Fugatt Microsoft Exchange MVP www.exchangetrainer.com www.msexchange.org "jallen" <anonymous@discussions.microsoft.com> wrote in message news:08e501c3c963$a59beaf0$a401280a@phx.gbl... > What is process "tkupd32.exe"? Safe to end process? It's > pegging my processor at 100% utilization. ...

Is there a way to generate a printed form from a spreadsheet?
I have a simple spreadsheet that keeps track of items requested of one of our suppliers. Works fairly well, it does a few calculations but is mostly just to keep track. Unfortunately, after I enter the row of data in excel, I still have to fill out a pre-printed paper form and fax the request to the vendor. Is there a way that I can design the same form and have the key data that I entered in excel to be placed in the proper places on the form so I can print and fax or better yet, email? Thanks? Sure, It usually takes a bit of time to duplicate the form but then you can fill in ...