Click here to Skip to main content
Licence CPOL
First Posted 1 Sep 2010
Views 13,598
Downloads 52
Bookmarked 12 times

Change WINVER

By Roger65 | 5 Sep 2010
How to change #define WINVER 0x0502 to 0x0601

1

2

3
3 votes, 75.0%
4
1 vote, 25.0%
5
4.40/5 - 4 votes
μ 4.40, σa 0.88 [?]

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)

About the Author

Roger65



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBetter using a central header file for each solution or over all projects PinmemberMartin Richter [MVP C++]22:31 19 Sep '10  
GeneralAlternate to: OnBrowse [modified] PinmemberRoger658:35 9 Sep '10  
Generalyou can use a dynamic language like Lua do this job PinmemberDavid_LoveCpp0:50 7 Sep '10  
Questionmacro? Pinmemberprakashr198418:16 1 Sep '10  
AnswerRe: macro? PinmemberPatLeCat22:23 5 Sep '10  
GeneralRe: macro? Pinmemberprakashr198422:35 5 Sep '10  
GeneralRe: macro? PinmemberRoger6523:42 5 Sep '10  
Questioncan also use Visual Studio find/replace with regular expression PinPopularmemberArthur Dumas6:07 1 Sep '10  
AnswerRe: can also use Visual Studio find/replace with regular expression PinmemberRoger6512:52 1 Sep '10  
GeneralRe: can also use Visual Studio find/replace with regular expression PinmemberArthur Dumas18:12 1 Sep '10  
GeneralRe: can also use Visual Studio find/replace with regular expression PinmemberRoger6522:52 1 Sep '10  
AnswerRe: can also use Visual Studio find/replace with regular expression PinmemberJonathan [Darka]0:09 2 Sep '10  
GeneralRe: can also use Visual Studio find/replace with regular expression PinmemberTak4:11 2 Sep '10  
GeneralRe: can also use Visual Studio find/replace with regular expression PinmemberJonathan [Darka]6:24 2 Sep '10  
GeneralRe: can also use Visual Studio find/replace with regular expression Pinmembermgpw0:31 8 Sep '10  
GeneralRe: can also use Visual Studio find/replace with regular expression PinmemberRoger651:44 8 Sep '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120209.1 | Last Updated 5 Sep 2010
Article Copyright 2010 by Roger65
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid