How to programmatically obtain the installation state of Visual Studio .NET or Visual Studio 2005 (884468)



The information in this article applies to:

  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

INTRODUCTION

This article describes how to programmatically obtain the installation state of Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.

Typically, you can examine a file or a registry entry to determine whether you have a version of Microsoft Visual Studio .NET or a version of Microsoft Visual Studio 2005, or of any program, already installed on your computer. You can also programmatically determine the version information by using Microsoft Windows Installer functions such as the MsiQueryProductState function. The MsiQueryProductState function takes the product code as the input parameter and then returns the installation state of the program.

MORE INFORMATION

To obtain the installation state of Visual Studio .NET or of Visual Studio 2005 on your computer, follow these steps:
  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project. The New Project dialog box appears.
  3. Under Project Types, click Visual Basic Projects or click Visual C# Projects.

    Note In Visual Studio 2005, click Visual Basic or Visual C#.
  4. Under Templates, click Windows Application, and then click OK. By default, the following behavior occurs:
    • If you are using Microsoft Visual Basic .NET, the Form1 form and the Form1.vb file are created.
    • If you are using Microsoft Visual C# .NET, the Form1 form and the Form1.cs file are created.
  5. Add a TextBox control and a Button control to the Form1 form.
  6. In Solution Explorer, right-click Form1.vb or Form1.cs, and then click View Code.
  7. Add the following code at the top of the file.

    Microsoft Visual Basic .NET or Microsoft Visual Basic 2005
    Imports System.Runtime.InteropServices
    Visual C# .NET or Visual C# 2005
    using System.Runtime.InteropServices;
    
  8. Add the following code to the declarations section of the Form1 class.

    Visual Basic .NET or Visual Basic 2005
    <DllImport("msi.dll")> _
    Public Shared Function MsiQueryProductState(ByVal szProduct As String) As Int32
    End Function
    Visual C# .NET or Visual C# 2005
    [DllImport("msi.dll")]
    public static extern Int32 MsiQueryProductState(string szProduct);
    
  9. In Design view of the Form1 form, double-click Button1, and then add the following code to the Button1_Click event procedure.

    Visual Basic .NET or Visual Basic 2005
    ' szProdCode is the product code.
    ' Include the product code in braces ({}).
    Dim szProdCode As String = TextBox1.Text.ToString().Trim()
    Dim iErrorReturnQuerryState As Integer
    iErrorReturnQuerryState = MsiQueryProductState(szProdCode)
    MessageBox.Show(iErrorReturnQuerryState.ToString())
    Visual C# .NET or Visual C# 2005
    // szProdCode is the product code.
    //Include the product code in braces ({}).
    string szProdCode = textBox1.Text.ToString().Trim();
    int iErrorReturnQuerryState=MsiQueryProductState(szProdCode);
    MessageBox.Show(iErrorReturnQuerryState.ToString());
  10. On the Build menu, click Build Solution.
  11. On the Debug menu, click Start. The Form1 form appears.
  12. Type the product code in the text box. For example, type {005F0409-6759-11D5-A54F-0090278A1BB8}. This is the product code for Microsoft Visual Studio .NET Enterprise Architect - English.
  13. Click Button1.
You can obtain the installation state by comparing the return integer value with the following values.
Installation stateReturn integer valueMeaning
INSTALLSTATE_INVALIDARG-2An invalid parameter was passed to the function.
INSTALLSTATE_UNKNOWN-1The product is not advertised or installed.
INSTALLSTATE_ADVERTISED1The product is advertised but not installed.
INSTALLSTATE_ABSENT2The product is installed for a different user.
INSTALLSTATE_DEFAULT5The product is installed for the current user.

REFERENCES

For more information about the MsiQueryProductState function, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:2/28/2006
Keywords:kbvs2005applies kbvs2005swept kbhowto KB884468 kbAudDeveloper