How do I get the default action of a specific file type?

Thread Solved

Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

How do I get the default action of a specific file type?

 
0
  #1
Nov 6th, 2005
Hi,

I am programming an app that I will use to launch different applications depending on the file type specified.

I have found, and know how to use, ShellExecute from VB and all I now need to know is how to get the default action specified by the user when double-clicking on that file type. I.e. the default action when double-clicking a .mp3 file is Open but I want to use MY default action of Enqueue.

Does anyone know how to get this info from the computer?

Also, if you happen to know how to get a list of possible actions, similar to right-clicking the file, that would be a bonus.

Thanks

Yomet
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: How do I get the default action of a specific file type?

 
0
  #2
Nov 6th, 2005
Hi again,

I found out how to do it and it's very simple. Just set the Operation argument to vbNullString and ShellExecuteA will do the defult action... Oh well, who'd 'a thunk...

Now, if someone still wants to give me a hint about how to get a list of available actions I am all ears.

Thanks again

Yomet
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How do I get the default action of a specific file type?

 
0
  #3
Nov 6th, 2005
what do you mean by a list of possible actions? Something like the "open with" menu, or something like the properties menu?
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: How do I get the default action of a specific file type?

 
0
  #4
Nov 6th, 2005
If, in Win XP or Win 2K, you right click a file you will have a few choices as the first two, three or four lines in the pop-up menu, just above the Open with... option. For example, if I right-click a .doc file I have:
Open
New
Print
Open with

These choices come from the Tools/Folder Options... menu in Explorer and then chosing the File Types tab. I have a bunch of custom choices for different files and I would like to reproduce them in my app.

I hope this makes sense.

Thx Comatose

Yomet
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How do I get the default action of a specific file type?

 
0
  #5
Nov 6th, 2005
If You Are Wanting To Retrieve A List of those options, for a given file, you are going to need to know a lot about the registry (a lot more than I can explain here). If you are already registry proficient, then, for a .doc file, for example, you might start by looking at: HKEY_CLASSES_ROOT\.doc, the "default" value for that, is a pointer, to another key in HKCR, in my case, Word.Document.8. So, you would then have to navigate to: HKEY_CLASSES_ROOT\Word.Document.8, and then check out the "shell" key. At this point, you'll have to "enumerate" all of the subkeys of the shell subkey, to give you a list of menu options. If you open one of the subkeys, you'll see that the tree nests even further, and if you open, say "New" you'll see a "command" subkey, which will reference the path and EXE to the file to launch in case of a click to "New".

Truth is, it's a real pain to do this, mostly because of the enumeration of the registry. This isn't the easiest thing in the world to do, but you would need the API Calls:
RegEnumKey, RegEnumKeyEx, and RegEnumValue (For The Values In The Keys). You Might Need RegOpenKeyEx, and RegCloseKey Too (Since I Think They Have To Be Opened Before You Can Enumerate Them). I Wrote A Program That Does This As A Part of it's functionality, but the Project in it's entirety isn't fully functional, So I won't be posting it. Let me know if this helps any, though.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: How do I get the default action of a specific file type?

 
0
  #6
Nov 6th, 2005
Comatose,

Thanks for the reply and, yes, it helps me a lot.

Now to say that I will go through all the trouble - I'm not so sure. But at least I know where to start and, thanks to you, how to go about digging through the registry.

For now I have almost everything I wanted by being able to launch with the default application and action but if I decide to do it and it works I'll make a stand-alone module and I'll send it to you...

Thanks Again

Yomet
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: How do I get the default action of a specific file type?

 
0
  #7
Nov 6th, 2005
Aw Crap! Here's Part of it:

in a Module (you'll also need to declare the registry API's, But There Are Too many API'S in my project to sift through):
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Const REG_SZ = 1
  2. Public Const REG_EXPAND_SZ = 2
  3. Public Const REG_BINARY = 3
  4. Public Const REG_DWORD = 4
  5. Public Const REG_MULTI_SZ = 7
  6. Public Const ERROR_MORE_DATA = 234
  7. Public Const KEY_READ = &H20019
  8. Public Const HKEY_CLASSES_ROOT = &H80000000
  9. Public Const HKEY_CURRENT_CONFIG = &H80000005
  10. Public Const HKEY_CURRENT_USER = &H80000001
  11. Public Const HKEY_LOCAL_MACHINE = &H80000002
  12. Public Const HKEY_USERS = &H80000003
  13.  
  14. public Function EnumRegistryKeys(ByVal hKey As Long, ByVal KeyName As String) As Collection
  15. Dim Handle As Long
  16. Dim length As Long
  17. Dim index As Long
  18. Dim subkeyName As String
  19.  
  20. ' initialize the result collection
  21. Set EnumRegistryKeys = New Collection
  22.  
  23. ' Open the key, exit if not found
  24. If Len(KeyName) Then
  25. If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, Handle) Then Exit Function
  26. ' in all case the subsequent functions use hKey
  27. hKey = Handle
  28. End If
  29.  
  30. Do
  31. ' this is the max length for a key name
  32. length = 260
  33. subkeyName = Space$(length)
  34. ' get the N-th key, exit the loop if not found
  35. If RegEnumKey(hKey, index, subkeyName, length) Then Exit Do
  36.  
  37. ' add to the result collection
  38. subkeyName = Left$(subkeyName, InStr(subkeyName, vbNullChar) - 1)
  39. EnumRegistryKeys.Add subkeyName, subkeyName
  40. ' prepare to query for next key
  41. index = index + 1
  42. Loop
  43.  
  44. ' Close the key, if it was actually opened
  45. If Handle Then RegCloseKey Handle
  46.  
  47. End Function
  48.  
  49. Public Function EnumRegistryValues(ByVal hKey As Long, ByVal KeyName As String) As Collection
  50.  
  51. Dim Handle As Long
  52. Dim index As Long
  53. Dim valueType As Long
  54. Dim name As String
  55. Dim nameLen As Long
  56. Dim resLong As Long
  57. Dim resString As String
  58. Dim dataLen As Long
  59. Dim valueInfo(0 To 1) As Variant
  60. Dim retVal As Long
  61.  
  62. ' initialize the result
  63. Set EnumRegistryValues = New Collection
  64.  
  65. ' Open the key, exit if not found.
  66. If Len(KeyName) Then
  67. If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, Handle) Then Exit Function
  68. ' in all cases, subsequent functions use hKey
  69. hKey = Handle
  70. End If
  71.  
  72. Do
  73. ' this is the max length for a key name
  74. nameLen = 260
  75. name = Space$(nameLen)
  76. ' prepare the receiving buffer for the value
  77. dataLen = 4096
  78. ReDim resBinary(0 To dataLen - 1) As Byte
  79.  
  80. ' read the value's name and data
  81. ' exit the loop if not found
  82. retVal = RegEnumValue(hKey, index, name, nameLen, ByVal 0&, valueType, _
  83. resBinary(0), dataLen)
  84.  
  85. ' enlarge the buffer if you need more space
  86. If retVal = ERROR_MORE_DATA Then
  87. ReDim resBinary(0 To dataLen - 1) As Byte
  88. retVal = RegEnumValue(hKey, index, name, nameLen, ByVal 0&, _
  89. valueType, resBinary(0), dataLen)
  90. End If
  91. ' exit the loop if any other error (typically, no more values)
  92. If retVal Then Exit Do
  93.  
  94. ' retrieve the value's name
  95. valueInfo(0) = Left$(name, nameLen)
  96.  
  97. ' return a value corresponding to the value type
  98. Select Case valueType
  99. Case REG_DWORD
  100. CopyMemory resLong, resBinary(0), 4
  101. valueInfo(1) = resLong
  102. Case REG_SZ, REG_EXPAND_SZ
  103. ' copy everything but the trailing null char
  104. resString = Space$(dataLen - 1)
  105. CopyMemory ByVal resString, resBinary(0), dataLen - 1
  106. valueInfo(1) = resString
  107. Case REG_BINARY
  108. ' shrink the buffer if necessary
  109. If dataLen < UBound(resBinary) + 1 Then
  110. ReDim Preserve resBinary(0 To dataLen - 1) As Byte
  111. End If
  112. valueInfo(1) = resBinary()
  113. Case REG_MULTI_SZ
  114. ' copy everything but the 2 trailing null chars
  115. resString = Space$(dataLen - 2)
  116. CopyMemory ByVal resString, resBinary(0), dataLen - 2
  117. valueInfo(1) = resString
  118. Case Else
  119. ' Unsupported value type - do nothing
  120. End Select
  121.  
  122. ' add the array to the result collection
  123. ' the element's key is the value's name
  124. 'EnumRegistryValues.Add valueInfo, valueInfo(0)
  125. EnumRegistryValues.Add valueInfo(0)
  126. EnumRegistryValues.Add valueInfo(1)
  127.  
  128. index = index + 1
  129. Loop
  130.  
  131. ' Close the key, if it was actually opened
  132. If Handle Then RegCloseKey Handle
  133.  
  134. End Function

As you can see, if you have all of the API's Needed Declared, These 2 functions return a collection (sort of like an array, but more like perl's associative array (hash)). You can loop through the collection (or enum each part of the collection in a loop, but then you'll have crazy nested collections and stuff, or an Array of Collections :eek: ). This, however, is how I call the functions:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim RetCol, TColl As New Collection
  2. Set RetCol = EnumRegistryValues(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run")

Here I used EnumRegistryValues, But It Is The Same Idea For Registry Keys. It's Uh, A Lot of code to be certain, and it's nothing trivial, but this way you at least have the functions, and can play with them if you decide to add it to your project.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: How do I get the default action of a specific file type?

 
0
  #8
Nov 6th, 2005
Thanks a million Comatose,

Not only do you point me in the right direction - you give me code to, almost, solve my whole problem. Great!

Now I'll start looking through your code and how to incorporate it into my little app - I feel obliged to...

Thanks again and I'll give you an update when I'm finished.

Yomet
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC