This program finds all the
#define WINVER 0x0502 in your Visual Studio 2005 Projects folder. All projects there have a stdafx.h file that may contain this
#define WINVER 0x0502 or some other value (e.g., 420, 440, 502, etc.). My program finds these
#defines and changes the (Windows 7) value to 0x0601.
I had close to 100 projects in my Visual Studio 2005 Projects folder and didn't want to manually change all the stdafx.h files.
Using the Code
While developing this program, I was getting an error: "Encountered a sharing violation while accessing path\StdAfx.h". At first, I thought it meant the My Documents folder should have "file sharing" set. No, that wasn't it. After looking at the
Cfile::Remove() method, it said that it would cause a
CFileException if the file was still open for reading.
fdi.Close() closes the file and deletes the object, and I didn't know the file descriptor
fdi would still be valid. Well it is. The following code fragment is the correct way of handling
fdi.Remove(Oldfile) and the
fdo file descriptor
fdo.Rename(Oldfile, NewFile) method.
Here is the code fragment:
catch (CFileException *pEx)
You have seen on a few program where they show something like: Running... That was easy to do.
void CChangeWinverDlg::OnTimer(UINT_PTR nIDEvent)
case 1 : SetDlgItemText(IDC_STATIC_STATUS, "Running");
case 2 : SetDlgItemText(IDC_STATIC_STATUS, "Running.");
case 3 : SetDlgItemText(IDC_STATIC_STATUS, "Running..");
case 4 : SetDlgItemText(IDC_STATIC_STATUS, "Running...");
default : SetDlgItemText(IDC_STATIC_STATUS, "Running");
Here is the complete method (something of interest, the stdafx.h file might look like this):
#define WINVER 0x0502
CString.Find(...): If you simple try to find the "
string and assume the "
0x0" follows, it wraps back to the previous line "
#ifndef WINVER" and replaces the "
ifndef" with "
#i601ef". You must first find the
#define then the
WINVER; next, the
0x0 string before replacing what follows with
Also, you can't read and write to the same file, so I read the old stdafx.h file and created a new file stdafx.hpp to copy the records into. Then, at the end of my method, I removed the old file (stdafx.h) and renamed the new (stdafx.hpp) to (stdafx.h).
BOOL CChangeWinverDlg::EditFile(CString& szFile)
CString szDef = "define";
CString szWord = "WINVER";
CString szValue = "0x0";
CString szOutFile = szFile;
szOutFile += "pp";
if(!fdi.Open(szFile, CFile::modeRead, &e))
szError.Format("Could not open: %s for reading", e.m_strFileName);
if(!fdo.Open(szOutFile, CFile::modeCreate |
CFile::modeWrite | CFile::shareDenyNone, &e))
szError.Format("Could not open: %s for writing", e.m_strFileName);
if((szData.Find(szDef, 0) > 0) && (szData.Find(szWord, 0) > 0))
n = szData.Find(szValue, 0);
n += 3;
szData += "\n";
szNewFile = szOutFile;
n = szNewFile.Find(".hpp", 0);
szNewFile += ".h";
catch (CFileException *pEx)
Points of Interest
Note: When I bought this system, it had OEM Windows Vista; at the time, I didn't know that VS2005 doesn't work with Vista. Every day, Windows Update tried to install VS2005 SP1 (KB947738) and failed every time. I could debug console apps, but not MFC apps. When I upgraded to Windows 7, it stopped trying to update the Service Pack, and trying to use VS2005 on Windows 7 became a nightmare.
It is always a good idea to make a backup of your files before running an untested program like this one; during the development stage, weird things might happen. Once I had it bug free, I compiled several of them using Visual Studio 2010.
ChangeWinver: V1.0: 09/01/2010
ChangeWinver: V1.1: 09/02/2010 Added