Tag Archives: Visual Studio 2008

Unexpected precompiled header error C1859 on Windows 7

I’m one of the few developers at my company that is using Windows 7.  Most of the projects in the solution I am working with are C#, but some are C++.  Periodically I get the following error building every single source file in the C++ project on my machine:

fatal error C1859: 'Debug\MyProject.pch' unexpected precompiled header error, simply rerunning the compiler might fix this problem

Nothing I did seemed to solve the problem short of rebooting, which seems to be a common experience (see here and here).  Today I stumbled upon a post on the Visual C++ Team Blog that explains what is going on.

It turns out that the anti-malware improvements in Windows 7 are the culprit.  PCH files are implemented basically as a memory dump, including pointers.  This worked fine on previous versions of Windows, but Windows 7 upped the ante making this approach unworkable.

The new behavior that fixes this problem is available in Visual Studio 2010.  The Visual C++ team has also released a hotfix for Visual Studio 2008 SP1 with the fix.


Removing trailing whitespace in Visual Studio

A pet peeve of mine is whitespace (spaces and tabs) at the end of lines in source code.  I used the SourceInsight editor when I worked at Microsoft which has a built-in feature (among other terrific features) to strip trailing whitespace when you save.  Visual Studio has plenty of other features that I can’t live without, so it was time to figure out how to add it there.

A decent solution is to add a subroutine to the EnvironmentEvents Module.  The articles I read made the assumption that anyone reading it would know where to find that.  I didn’t, so it took me some extra time to implement the feature.

Find EnvironmentEvents Module

The EnvironmentEvents Module is a macro module that is part of your Visual Studio environment and is accessible regardless of which project is loaded.  To edit that module, you need to bring up the Macros IDE, which can be launched at Tools » Macros » Macros IDE, or by pressing Alt+F11.

Macros IDE

In the Macros IDE you will see two entries in the Project Explorer (unless you’ve been here before and have been adding macros on your own):  MyMacros and Samples.  Double-click on MyMacros.  This will show you three entries:  References, EnvironmentEvents, and Module1.

Macros IDE Project Explorer - EnvironmentEvents

Double click on EnvironmentEvents.  You will now see the current contents of the EnvironmentEvents module.

Default EnvironmentEvents

DocumentEvents_DocumentSaved Subroutine

Now we come to the meat of the solution.  The idea is to implement the DocumentEvents_DocumentSaved subroutine to remove trailing whitespace.  This subroutine is called whenever a request is made to save a document.

Private Sub DocumentEvents_Documents(ByVal document As EnvDTE.Document) _
                                     Handles DocumentEvents.DocumentSaved
        ' Remove all the trailing whitespaces.
        If vsFindResult.vsFindResultReplaced = _
            DTE.Find.FindReplace( _
                            vsFindAction.vsFindActionReplaceAll, _
                            “{:b}+$”, _
                            vsFindOptions.vsFindOptionsRegularExpression, _
                            String.Empty, _
                            vsFindTarget.vsFindTargetFiles, _
                            document.FullName, , _
                            vsFindResultsLocation.vsFindResultsNone) Then
        End If
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.OkOnly, “Trim Whitespace exception”)
    End Try
End Sub

Once you’ve saved this, it will be immediately available to Visual Studio, whether it is open or not.

This subroutine searches for all instances of trailing whitespace (spaces and tabs) and replaces it with the empty string.  It works when you save an individual file and when you do a Save All.  If an error occurs in the FindReplace method, a message box will be displayed.

Thanks to Michael Urman, who posted this solution on StackOverflow.com (May 19 response).