Click here to Skip to main content
13,191,642 members (61,962 online)
Click here to Skip to main content
Add your own
alternative version

Stats

36K views
13 bookmarked
Posted 1 Sep 2010

Change WINVER

, 5 Sep 2010
Rate this:
Please Sign up or sign in to vote.
How to change #define WINVER 0x0502 to 0x0601

Introduction

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.

Background

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:

//
// function block that Removes the old stdafx.h file
// and Renames the new stdafx.hpp file stdafx.h.
//

try
{
    fdi.Close();
    fdi.Remove(szFile);
    fdo.Close();
    fdo.Rename(szOutFile, szNewFile);
}
catch (CFileException *pEx)
{
    pEx->ReportError();
    fdi.Abort();
    fdo.Abort();
    pEx->Delete();

    return FALSE;
}
return TRUE;

You have seen on a few program where they show something like: Running... That was easy to do.

//
// 1 second timer displays "Running with marching periods".
//
void CChangeWinverDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	switch(n_periods)
	{
	case 1 : SetDlgItemText(IDC_STATIC_STATUS, "Running");
		break;
	case 2 : SetDlgItemText(IDC_STATIC_STATUS, "Running.");
		break;
	case 3 : SetDlgItemText(IDC_STATIC_STATUS, "Running..");
		break;
	case 4 : SetDlgItemText(IDC_STATIC_STATUS, "Running...");
		break;
	default : SetDlgItemText(IDC_STATIC_STATUS, "Running");
		break;
	}
	n_periods++;
	if(n_periods =5)
		n_periods =;

	CDialogEx::OnTimer(nIDEvent);
}

Here is the complete method (something of interest, the stdafx.h file might look like this):

#ifndef WINVER
#define WINVER 0x0502
#endif

CString.Find(...): If you simple try to find the "WINVER" 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 601.

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 szData;
    CString szError;
    CString szDef = "define";
    CString szWord = "WINVER";
    CString szValue = "0x0";
    CString szNewFile;
    CString szOutFile = szFile;
    szOutFile += "pp";
    CStdioFile fdi;
    CStdioFile fdo;
    SetDlgItemText(IDC_STATIC_DEBUG, szFile);
    CFileException e;
    int n;
    if(!fdi.Open(szFile, CFile::modeRead, &e))
    {
        szError.Format("Could not open: %s for reading", e.m_strFileName);
        AfxMessageBox(szError);
        return FALSE;
    }
    if(!fdo.Open(szOutFile, CFile::modeCreate | 
                 CFile::modeWrite | CFile::shareDenyNone, &e))
    {
        szError.Format("Could not open: %s for writing", e.m_strFileName);
        AfxMessageBox(szError);
        return FALSE;
    }

    while(fdi.ReadString(szData))
    {
        if((szData.Find(szDef, 0) > 0) && (szData.Find(szWord, 0) > 0))
        {
            n = szData.Find(szValue, 0);
            n += 3;
            szData.Delete(n, 3);
            szData.Insert(n, "601");
        }
        szData += "\n";
        fdo.WriteString(szData);
    }

    szNewFile = szOutFile;
    n = szNewFile.Find(".hpp", 0);
    szNewFile.Delete(n, 4);
    szNewFile += ".h";

    try
    {
        fdi.Close();
        fdi.Remove(szFile);
        fdo.Close();
        fdo.Rename(szOutFile, szNewFile);
    }
    catch (CFileException *pEx)
    {
        pEx->ReportError();
        fdi.Abort();
        fdo.Abort();
        pEx->Delete();

        return FALSE;
    }
    return TRUE;
}

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.

History

  • ChangeWinver: V1.0: 09/01/2010
  • ChangeWinver: V1.1: 09/02/2010 Added abort method

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Roger65
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralBetter using a central header file for each solution or over all projects Pin
Martin Richter [MVP C++]19-Sep-10 21:31
memberMartin Richter [MVP C++]19-Sep-10 21:31 
GeneralAlternate to: OnBrowse [modified] Pin
Roger659-Sep-10 7:35
memberRoger659-Sep-10 7:35 
Generalyou can use a dynamic language like Lua do this job Pin
David_LoveCpp6-Sep-10 23:50
memberDavid_LoveCpp6-Sep-10 23:50 
Questionmacro? Pin
prakashr19841-Sep-10 17:16
memberprakashr19841-Sep-10 17:16 
AnswerRe: macro? Pin
PatLeCat5-Sep-10 21:23
memberPatLeCat5-Sep-10 21:23 
GeneralRe: macro? Pin
prakashr19845-Sep-10 21:35
memberprakashr19845-Sep-10 21:35 
GeneralRe: macro? Pin
Roger655-Sep-10 22:42
memberRoger655-Sep-10 22:42 
Questioncan also use Visual Studio find/replace with regular expression Pin
Arthur Dumas1-Sep-10 5:07
memberArthur Dumas1-Sep-10 5:07 
AnswerRe: can also use Visual Studio find/replace with regular expression Pin
Roger651-Sep-10 11:52
memberRoger651-Sep-10 11:52 
GeneralRe: can also use Visual Studio find/replace with regular expression Pin
Arthur Dumas1-Sep-10 17:12
memberArthur Dumas1-Sep-10 17:12 
GeneralRe: can also use Visual Studio find/replace with regular expression Pin
Roger651-Sep-10 21:52
memberRoger651-Sep-10 21:52 
AnswerRe: can also use Visual Studio find/replace with regular expression Pin
Jonathan [Darka]1-Sep-10 23:09
memberJonathan [Darka]1-Sep-10 23:09 
GeneralRe: can also use Visual Studio find/replace with regular expression Pin
Tak2-Sep-10 3:11
memberTak2-Sep-10 3:11 
GeneralRe: can also use Visual Studio find/replace with regular expression Pin
Jonathan [Darka]2-Sep-10 5:24
memberJonathan [Darka]2-Sep-10 5:24 
GeneralRe: can also use Visual Studio find/replace with regular expression Pin
mgpw7-Sep-10 23:31
membermgpw7-Sep-10 23:31 
GeneralRe: can also use Visual Studio find/replace with regular expression Pin
Roger658-Sep-10 0:44
memberRoger658-Sep-10 0:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171017.2 | Last Updated 5 Sep 2010
Article Copyright 2010 by Roger65
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid