How To Determine File Type from Registered CLSID

This article was previously published under Q174050
This article has been archived. It is offered "as is" and will no longer be updated.
SUMMARY
You can use the Windows API to retrieve the CLSID (class ID) associatedwith a given file and use this value to determine the product and versionof its format.
MORE INFORMATION
The CLSID for a product or component can be found in the Windows Registryunder HKEY_CLASSES_ROOT. For example:
MyComputer\HKEY_CLASSES_ROOT\Excel.Sheet.8\CLSID

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

NOTE: In order to identify a file on a given system, the appropriate CLSIDmust be registered on the system running this code. Also, this sample doesnot include any error trapping, which should be added for any productionuse of this technique.

Step-by-Step Example

  1. Start a new Standard EXE project. Form1 is created by default.
  2. Add a Command button, a TextBox, and two Labels to Form1.
  3. 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						
  4. 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.
  5. 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						
  6. 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.
REFERENCES
For more information, please Search on the following topics in either theWin32 Programmer's Reference or The Microsoft Developer Network (MSDN)Library CD:
  • GetClassFile
  • StringFromGUID2
  • CLSID (class ID)
kbVBp kbdsd kbDSupport kbRegistry kbVBp400 kbVBp500 kbVBp600 kbAPI
Properties

Article ID: 174050 - Last Review: 12/04/2015 17:52:45 - Revision: 3.2

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

  • kbnosurvey kbarchive kbhowto KB174050
Feedback