| [home] |
Visual Basic 6 / .NET
How to Associate a File Extension to Your Newly Created Application
- Create an empty Windows application.
- Add the following code to the General Declarations section your
Form.
Option Explicit
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _ "RegCreateKeyA" (ByVal hKey As Long, _ ByVal lpSubKey As String, _ phkResult As Long) As Long Private Declare Function RegSetValue Lib "advapi32.dll" Alias _ "RegSetValueA" (ByVal hKey As Long, _ ByVal lpSubKey As String, _ ByVal dwType As Long, _ ByVal lpData As String, _ ByVal cbData As Long) As Long
' Return codes from Registration functions. Const ERROR_SUCCESS = 0& Const ERROR_BADDB = 1& Const ERROR_BADKEY = 2& Const ERROR_CANTOPEN = 3& Const ERROR_CANTREAD = 4& Const ERROR_CANTWRITE = 5& Const ERROR_OUTOFMEMORY = 6& Const ERROR_INVALID_PARAMETER = 7& Const ERROR_ACCESS_DENIED = 8&
Private Const HKEY_CLASSES_ROOT = &H80000000 Private Const MAX_PATH = 260& Private Const REG_SZ = 1
Private Sub Form_Click()
Dim sKeyName As String 'Holds Key Name in registry. Dim sKeyValue As String 'Holds Key Value in registry. Dim ret& 'Holds error status if any from API calls. Dim lphKey& 'Holds created key handle from RegCreateKey.
'This creates a Root entry called "MyApp". sKeyName = "MyApp" sKeyValue = "My Application" ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&) ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
'This creates a Root entry called .BAR associated with "MyApp". sKeyName = ".BAR" sKeyValue = "MyApp" ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&) ret& = RegSetValue&(lphKey&, "", REG_SZ, sKeyValue, 0&)
'This sets the command line for "MyApp". sKeyName = "MyApp" sKeyValue = "c:\mydir\my.exe %1" ret& = RegCreateKey&(HKEY_CLASSES_ROOT, sKeyName, lphKey&) ret& = RegSetValue&(lphKey&, "shell\open\command", REG_SZ, _ sKeyValue, MAX_PATH) End Sub
Compile and now you have a program that associates ".BAR"
with an application called "MyApp" that the registry expects to be
located at "C:\mydir\my.exe"
Of course that isn't what you wanted to do. Just do
a find and replace on the three items listed in part (3).
The above material is from Microsoft Knowledge Base Article #185453.
|
|