Using __argv in the MFC application (VS2005) can cause a crash
Posted by AnwarJun 6
An MFC application generated using the Project Wizard of the VS2005 by default uses a Unicode character set for the project. If you are using __argv to access the command line arguments this might cause an application crash.
The following code illustrates the problem:
// Get the full path of the executable char szAppPath[MAX_PATH]; strcpy_s(szAppPath, MAX_PATH, __argv[0]);
In the Unicode configuration __argv is set to NULL. Therefore using __argv[0] causes a crash. Actually, __argv is a pointer to a table of command line arguments of regular char type.
In order to access the command line arguments in argv style in Unicode applications, you should use __wargv – a wide character equivalent of __argv.
So your code may look like this:
wchar_t wszAppPath[MAX_PATH]; wcscpy(wszAppPath, __wargv[0]);
If you don’t need the Unicode encoding in your project, just change the Character Set to Not Set in the Configuration Properties | General.
You can also use GetModuleFileName Win API function to retrieve the fully-qualified path of the application.
3 comments
Pingback by Anwar Shafiev » ИÑпользование __argv в MFC приложении (VS2005) может вызвать Ñбой on June 6, 2007 at 2:25 pm
[…] ПоÑÑ‚ о возможной проблеме иÑÐ¿Ð¾Ð»ÑŒÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ __argv Ð´Ð»Ñ Ð¿Ð¾Ð»ÑƒÑ‡ÐµÐ½Ð¸Ñ Ð°Ñ€Ð³ÑƒÐ¼ÐµÐ½Ñ‚Ð¾Ð² командной Ñтроки в MFC приложении в Unicode кодировке опубликован на английÑком Ñзыке. […]
Comment by Snow on October 28, 2008 at 7:13 pm
This is great info to know.
Comment by UseHymn on January 3, 2009 at 1:52 am
Да все понятно, Спасибочки за пост.