VB projects - Get Content Type
Description: search Registry for content type for the selected file
Minimum requirements: VB6
Download: source code
Screenshot:

Project: EXE
Controls: TextBox txtContentType, CommandButton cmdBrowse (Caption = "Browse"), TextBox txtFile, CommonDialog dlg
Additional references: none
Code:
Option Explicit
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const KEY_QUERY_VALUE = &H1
Private Const REG_SZ = 1
Private Const ERROR_SUCCESS = 0&
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _
ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Sub cmdBrowse_Click()
dlg.ShowOpen
txtFile = dlg.FileName
If InStr(txtFile, ".") > 0 Then
txtContentType = GetContentType(Mid(txtFile, InStrRev(txtFile, ".")))
Else
txtContentType = "application/octet-stream"
End If
End Sub
Private Function GetContentType(ByVal s As String) As String
Dim hKey As Long
Dim lpSubKey As String
Dim lpValueName As String
Dim lpType As Long
Dim lpData As String
Dim lpcbData As Long
GetContentType = "application/octet-stream"
lpSubKey = "Content Type"
If RegOpenKeyEx(HKEY_CLASSES_ROOT, s, 0, KEY_QUERY_VALUE, hKey) = ERROR_SUCCESS Then
RegQueryValueEx hKey, lpSubKey, 0, lpType, Chr(0), lpcbData
If lpType = REG_SZ Then
lpData = Space(lpcbData)
If RegQueryValueEx(hKey, lpSubKey, 0, lpType, ByVal lpData, lpcbData) = ERROR_SUCCESS Then
GetContentType = Left(lpData, lpcbData)
End If
End If
RegCloseKey hKey
End If
End Function
|