Microsoft KB Archive/174050

= How To Determine File Type from Registered CLSID =

Article ID: 174050

Article Last Modified on 7/16/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q174050



SUMMARY
You can use the Windows API to retrieve the CLSID (class ID) associated with a given file and use this value to determine the product and version of its format.



MORE INFORMATION
The CLSID for a product or component can be found in the Windows Registry under HKEY_CLASSES_ROOT. For example:

MyComputer\HKEY_CLASSES_ROOT\Excel.Sheet.8\CLSID

The following example creates a Visual Basic project that shows how to retrieve and use the CLSID associated with a given file. Constants are only defined for two versions of Microsoft Excel and two versions of Microsoft Word to demonstrate this technique. This example can be modified to work for any file format with a unique CLSID associated with it.

NOTE: In order to identify a file on a given system, the appropriate CLSID must be registered on the system running this code. Also, this sample does not include any error trapping, which should be added for any production use of this technique.

Step-by-Step Example
 Start a new Standard EXE project. Form1 is created by default. Add a Command button, a TextBox, and two Labels to Form1.  Copy the following code into the Form's module: Private Sub Command1_Click Dim FileCLSID As String

FileCLSID = GetCLSID(Text1.Text) Label2.Caption = FileCLSID Select Case FileCLSID Case EXCEL97 Label1.Caption = "Excel 97" Case EXCEL95 Label1.Caption = "Excel 95" Case WORD97 Label1.Caption = "Word 97" Case WORD95 Label1.Caption = "Word 95" Case Else Label1.Caption = "Unknown File Version" End Select End Sub

Private Sub Form_Load Label1.AutoSize = True Label2.AutoSize = True End Sub

 Add a Module to the project. In Visual Basic 4, choose "Module" from the "Insert" menu. In later versions of Visual Basic, choose "Add Module" from the "Project" menu.  Copy the following code into Module1: Type GUID B(16) As Byte End Type

Declare Function GetClassFile Lib "Ole32.DLL" (ByVal lpszFileName _     As String, ByRef pclsid As GUID) As Long

Declare Function StringFromGUID2 Lib "Ole32.DLL" (ByRef rguid _     As GUID, ByVal lpsz As String, ByVal cbMax As Long) As Long

Public Const EXCEL97 As String = _ "{00020820-0000-0000-C000-000000000046}" Public Const EXCEL95 As String = _ "{00020810-0000-0000-C000-000000000046}" Public Const WORD97 As String = _ "{00020906-0000-0000-C000-000000000046}" Public Const WORD95 As String = _ "{00020900-0000-0000-C000-000000000046}"

Function GetCLSID(FileName As String) As String Dim g As GUID Dim RetVal As Long Dim strGUID As String

RetVal = GetClassFile(StrConv(FileName, vbUnicode), g)        strGUID = Space(255) RetVal = StringFromGUID2(g, strGUID, 255) strGUID = StrConv(strGUID, vbFromUnicode) If (InStr(strGUID, Chr(0)) > 0) Then strGUID = Left(strGUID, InStr(strGUID, Chr(0)) - 1) End If        GetCLSID = strGUID End Function

 Run the project and enter a path and filename for a Word 97 or Word 95 document, or an Excel 97 or Excel 95 Worksheet into Text1. Click on Command1 and the type of file will be displayed in Label1 and the actual CLSID will be displayed in Label2.</ol>

<div class="references_section">