Geeks With Blogs

News



Microsoft Store

Support This Site


AddThis Social Bookmark Button

Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

Creative Commons License


Scott Dorman Microsoft MVP, Software Architect, Developer, Author

I don't know about you, but I have always felt that Microsoft did a huge injustice to the development community by leaving the Static Code Analysis capability out of Visual Studio 2005 Professional Edition. Just in case you don't know what this is, it is an integrated version of FxCop that allows you to configure Code Analysis and view the results from within Visual Studio.

I'm not going to go into details about why Code Analysis is beneficial (perhaps I'll save that for another post), but having the results integrated with Visual Studio and being able to run the analysis as part of the build is very convenient.

If you have Visual Studio 2005 Professional edition, you were pretty much out of luck. There are a few ways you can do this, each with limitations.

External Tool #1

You can create an external tool to run FxCop and put the output in the Output window. To do this, follow these simple steps:

  1. Go to Tools -> External Tools
  2. Click "Add" and fill in the following details

    Title: FxCop
    Command: C:\Program Files\Microsoft FxCop 1.35\FxCopCmd(.)exe
    Arguments:  /c /f:"$(TargetPath)" /r:"C:\Program Files\Microsoft FxCop 1.35\Rules" /consolexsl:"C:\Program Files\Microsoft FxCop 1.35\Xml\VSConsoleOutput.xsl"
    Initial Directory: C:\Program Files\Microsoft FxCop 1.35
    Check the "Use Output window" checkbox

Apparently, there is a bug in the Metaweblog API that prevents me from putting the correct path to the FxCopCmd executable. In the path shown above, remove the parenthesis characters surrounding the period.

These steps assume that FxCop is installed in the default location. You will also need to download a different XSL stylesheet to create messages that can be double-clicked within the Visual Studio Output window to jump to the source location of the violation.

The drawback to doing this is that it is a separate step you have to remember to run each time and the output is static (you can't click on it to take you to the line). Another drawback is that you can't customize the list of rules.

External Tool #2

A variation on this is to use an external FxCop project file. The project file should be the same name as your solution file with a .fxcop extension. For example, if your solution is named Abc.sln, the FxCop project file should be named Abc.sln.fxcop. The project file must also be located in the same directory as the solution file.

Follow the same steps above, but change the Arguments to:

/c /p:"$(SolutionDir)\$(SolutionFileName).fxcop" /consolexsl:"C:\Program Files\Microsoft FxCop 1.35\Xml\VSConsoleOutput.xsl"

This solves the problem of not being able to customize the rules but introduces another file to keep track of. It does have the benefit of allowing someone without Visual Studio to run FxCop, although I'm not sure how useful that would actually be.

FxCop Addin

There is a very good open source Visual Studio add-in that solves most of the integration problems. Once the add-in is installed, you gain a new menu choice in the Project menu and the Project context menu named "Add Code Analysis Support". This menu option adds a new property entry to the project file that tells MSBuild to run FxCop after the build is complete.

Since this add-in modifies the project file, as soon as you add support to the project, Visual Studio prompts you to reload the project. Once you reload the project, you are presented with a security dialog:

You will receive this dialog the first time the project is opened. You want to select the "Load project normally" option. Fortunately, you should only need to do this once; however, if you send the solution or project to someone else, they will also see this warning.

Behind the scenes, the add-in is running FxCop and translating the output through a stylesheet. You do get a context menu in the error list window that allows you to view more details about the violation, and generate suppression messages to place in your code.

Unfortunately, the add-in also does not provide a way to customize the list of rules, and it also looks like there hasn't been any active development on it since August 2006.

Visual Studio 2005 Team Edition Static Analysis

This is almost like the holy grail of static analysis solutions. Everything I have found on the Internet has pretty much said that it wasn't possible to enable the same type of static analysis that the Visual Studio Team editions have in Visual Studio Professional. In one sense, they are correct. However, it is possible to get 99% of the functionality working.

Disclaimer:

This is neither a supported nor sanctioned solution by Microsoft and involves registry changes as well as additional Microsoft DLLs that are not part of Visual Studio 2005 Professional.

 Since I'm not 100% sure on the redistributable concerns of these files, I am not going to make them available as part of this post. I will give step-by-step instructions on how to get this working under the assumption that you already have these files from some other means. All of the files required for this solution are part of Visual Studio Team Edition for Software Developers (and probably part of any of the Team Edition versions).

  • Copy the C:\Program Files\MSBuild\Microsoft\VisualStudio\v8.0\CodeAnalysis folder.
  • Copy the C:\Program Files\Microsoft Visual Studio 8\Team Tools\Static Analysis Tools folder.

These folders should be copied to the same locations on your Visual Studio 2005 Professional system. The simplicity of this approach works because the Microsoft.Common.targets file already includes all of the necessary logic to include the Code Analysis targets file if it exists.

Unfortunately, this only gets us half way there. To complete the picture, we need to make some registry changes. The registry changes are what tell Visual Studio to load the Static Analysis Tools package. It is this package that governs the interaction between Visual Studio and the build process.

The easiest way is to copy the following code into Notepad and save it with a .reg extension. Then you can merge the file into the registry.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Packages\{72391CE3-743A-4a55-8927-4217541F6517}]
@="StanPackage"
"InprocServer32"="C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\stanpackage.dll"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Packages\{72391CE3-743A-4a55-8927-4217541F6517}\SatelliteDll]
@=""
"DllName"="StanPackageUI.dll"
"Path"="C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\CLSID\{72391CE3-743A-4a55-8927-4217541F6517}]
@="StanPackage class"
"InprocServer32"="C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Apartment"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\CLSID\{960d6d3a-0bd3-4afa-a0a6-31f7f2fe3a8e}]
@="VCRuleSelectionPage class"
"InprocServer32"="C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Apartment"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\CLSID\{984ae51a-4b21-44e7-822c-dd5e046893ef}]
@="VSRuleSelectionPage class"
"InprocServer32"="C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Apartment"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Services\{dfb40067-f0c3-425f-9936-951a7900a0ac}]
@="{72391CE3-743A-4a55-8927-4217541F6517}"
"Name"="SStaticAnalysisService"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Services\{AEBE2A87-9063-4B11-95C0-14E48CEC4439}]
@="{72391CE3-743A-4a55-8927-4217541F6517}"
"Name"="SFxCopLoggerService"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\Setup\EDev]
"StanDir"="C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\"
"FxCopDir"="C:\\Program Files\\Microsoft Visual Studio 8\\Team Tools\\Static Analysis Tools\\FxCop\\"
"CodeAnalysisErrorListViolationLimit"=dword:000000c8

Open Visual Studio (if you had Visual Studio open, you will need to restart it) and you will now have the "Code Analysis" tab in your project properties. The only thing this solution does also provide is the ability to select a message in the error list window and create the supression messages for it.

The next steps...

Now that I have proven to myself (and hopefully to you as well) that this works, I see two follow up steps. The first one is that everyone should start petitioning Microsoft to include this capability in the Visual Studio "Orcas" Professional release. I think it is too important for it to be limited to only the more expensive editions of Visual Studio.

The second step is to recreate the Microsoft DLLs so that we are able to have the same level of integration without worrying about redistribution issues. Hopefully, step one will negate the need for this, but just in case it doesn't a backup plan is always useful.

Posted on Sunday, February 18, 2007 3:28 PM .NET (General) | Back to top


Comments on this post: Visual Studio 2005 Professional Edition and FxCop Integration

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
lovely tip :)
Left by Nripin Babu on Apr 19, 2007 6:37 AM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
Folks interested in FxCop should have a glance at the tool NDepend:
http://www.NDepend.com

NDepend analyses source code and .NET assemblies. It allows controlling the complexity, the internal dependencies and the quality of .NET code.

NDepend provides a language (CQL Code Query Language) dedicated to query and constraint a codebase.

It also comes from with advanced code visualization (Dependencies Matrix, Metric treemap, Box and Arrows graph...), more than 60 metrics, facilities to generate reports and to be integrated with mainstream build technologies and development tools.

NDepend also allows to compare precisely different versions of your codebase.
Left by Patrick Smacchia on May 03, 2007 7:59 AM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
Why does viewing this page redirect me to a Passport sign-in screen? I was only able to comment here by pressing "STOP!"
Left by AdamV on Nov 29, 2007 12:19 PM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
Well, it looks like step #1 didn't work...

Does anyone have a solution for integrating FxCop with VS2008?
Left by Ian Lee on Dec 26, 2007 3:08 PM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
Not all versions of Team Edition include Code Analysis Tools; the Architect and Tester Editions do not have it, but can be added via the instructions given by Scott. They work great.
Left by BG on Feb 08, 2008 4:20 PM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
The link to custom stylesheet is no longer valid. Do you have an alternative link?
Left by Mikhail Diatchenko on Jul 24, 2008 2:20 AM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
The custom stylesheet is available here: http://blogs.msdn.com/fxcop/attachment/1738665.ashx
Left by Scott on Jul 24, 2008 10:57 AM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
I agree with Ian Lee. A solution for VS2008 would be great. I'm working on it and I'll post what I find.
Left by David Sutherland on Nov 21, 2008 11:34 AM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
The integration for VS2008:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\CLSID\{9102D392-9DE6-4CF0-80A5-2AE3012F61C1}]
@="PSFactoryBuffer"

[HKEY_CLASSES_ROOT\CLSID\{9102D392-9DE6-4CF0-80A5-2AE3012F61C1}\InProcServer32]
@="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Both"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{9102D392-9DE6-4CF0-80A5-2AE3012F61C1}]
@="PSFactoryBuffer"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{9102D392-9DE6-4CF0-80A5-2AE3012F61C1}\InProcServer32]
@="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Both"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\CLSID\{72391CE3-743A-4a55-8927-4217541F6517}]
@="StanPackage class"
"InprocServer32"="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Apartment"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\CLSID\{960d6d3a-0bd3-4afa-a0a6-31f7f2fe3a8e}]
@="VCRuleSelectionPage class"
"InprocServer32"="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Apartment"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\CLSID\{984ae51a-4b21-44e7-822c-dd5e046893ef}]
@="VSRuleSelectionPage class"
"InprocServer32"="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\stanpackage.dll"
"ThreadingModel"="Apartment"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Packages\{72391CE3-743A-4a55-8927-4217541F6517}]
@="StanPackage"
"InprocServer32"="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\stanpackage.dll"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Packages\{72391CE3-743A-4a55-8927-4217541F6517}\SatelliteDll]
@=""
"DllName"="StanPackageUI.dll"
"Path"="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Setup\EDev]
"StanDir"="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\"
"FxCopDir"="C:\\Programme\\Microsoft Visual Studio 9.0\\Team Tools\\Static Analysis Tools\\FxCop\\"
"CodeAnalysisErrorListViolationLimit"=dword:000000c8

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Services\{AEBE2A87-9063-4B11-95C0-14E48CEC4439}]
@="{72391CE3-743A-4a55-8927-4217541F6517}"
"Name"="SFxCopLoggerService"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\Services\{dfb40067-f0c3-425f-9936-951a7900a0ac}]
@="{72391CE3-743A-4a55-8927-4217541F6517}"
"Name"="SStaticAnalysisService"


HTH

Left by JN on Dec 22, 2008 3:24 AM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
For what it's worth, we usually create an FX Cop file as a solution item. You can associate the application with the extension by right-clicking on the file and specifying "Open With...". After the first time, you can open the FX Cop client directly by double-clicking on the file. Works pretty well for us.
Left by GLASSSD on Apr 24, 2009 3:50 PM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
Don't run that registry file if your on a 64-bit os... I tried self-modifying it to use the Wow64 nodes and (x86) directories.... Bad idea. I can no longer open the properties to a solution, or project. Worse yet I can't build anything anymore... Tried a repair to no avail of my exising VS installation -- going to try a full uninstall/reinstall.
Left by Bob on Jul 10, 2009 11:40 PM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
i want download free full version of .net 5
Left by VEENA on Oct 28, 2010 2:25 AM

# re: Visual Studio 2005 Professional Edition and FxCop Integration
Requesting Gravatar...
The tigris.org domain contains no project named fxcopaddin

How can I download it ?
Left by kiquenet on Oct 17, 2012 3:21 AM

Your comment:
 (will show your gravatar)
 


Copyright © Scott Dorman | Powered by: GeeksWithBlogs.net | Join free