CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    Associating File Types

    How do you associate file certain file types with a program? I.E., when you install RealPlayer it asks you what file types should be automatically opened by it when you double-click it. How do you do this?


  2. #2
    Join Date
    Feb 2000
    Location
    America
    Posts
    130

    Re: Associating File Types

    This can be done using the Registry. Go to start/run/regedit to view the registry. in HKEY_CLASSES_ROOT you'll see file exstentsions which are "keys". these keys will have "default" values, which refer to other keys. This is how you associate a file to a program. You make a program a "key" and tell all the file type keys' default value to that key name. Now for doing it using vb...

    Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    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
    Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long

    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&
    Const HKEY_CLASSES_ROOT = &H80000000
    Const MAX_PATH = 260&
    Const REG_SZ = 1

    These are the functions and Consts you need to change registry keys and values. Below is an example on how to use them:

    ret& = RegCreateKey&(HKEY_CLASSES_ROOT, "MyProgram.File", lphKey&)
    ret& = RegSetValue&(lphKey&, "", REG_SZ, "This Type Of File", 0&)
    '/ This will create ur program's registry key

    ret& = RegCreateKey&(HKEY_CLASSES_ROOT, "MyProgram.File\DefaultIcon", lphKey&)
    ret& = RegSetValue&(lphKey&, "", REG_SZ, App.Path & "\MyProgram.exe", 0&)
    '/ This will set the default icon for you're programs registry key, and all associated files will have this icon.

    ret& = RegCreateKey&(HKEY_CLASSES_ROOT, "MyProgram.File\Shell\Open\Command", lphKey&)
    ret& = RegSetValue&(lphKey&, "", REG_SZ, App.Path & "\MyProgram.exe /Command %1", 0&)
    '/ This will create the "Shell" part of ur registry. This is what you use to tell the registry key that this is the program you want all associations to be associated with. When you open a file that's associated to this key, it will load this file, and give it a message. The message will be "/Command" and the location of the file (%1). To read the message, on load up have ur program check the variable Command$, it will hold the location of the file that was loaded (I. E. %1) and the commands you listed (I. E. /Command). you then say stuff like "if instr(Command$,"/play") then PlaySong Mid(Command$,Instr(Command$,"/play")+1,len(command$). This will, as it apperas, get the file that was loaded, get the command that you have for it, and use it in a function, however you wish. Below are the commands to associate the keys to ur registry key:

    ret& = RegCreateKey&(HKEY_CLASSES_ROOT, ".???", lphKey&)
    ret& = RegSetValue&(lphKey&, "", REG_SZ, "MyProgram.File", 0&)

    I hope this helps,
    Atlantis




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured