I've been using this function for about 3 years and it's been fine on W98, ME, NT4 and W2000 but fails on XP Pro:
The call to AssocQueryString returns -2147024894 on XP Pro. I've searched the latest MSDN CD for this error but there's nothing relevant to AssocQueryString.
Paul Bent
Northwind IT Systems
Code:
Const ASSOCF_INIT_NOREMAPCLSID = &H1 'do not remap clsids to progids
Const ASSOCF_INIT_BYEXENAME = &H2 'executable is being passed in
Const ASSOCF_OPEN_BYEXENAME = &H2 'executable is being passed in
Const ASSOCF_INIT_DEFAULTTOSTAR = &H4 'treat "*" as the BaseClass
Const ASSOCF_INIT_DEFAULTTOFOLDER = &H8 'treat "Folder" as the BaseClass
Const ASSOCF_NOUSERSETTINGS = &H10 'dont use HKCU
Const ASSOCF_NOTRUNCATE = &H20 'dont truncate the return string
Const ASSOCF_VERIFY = &H40 'verify data is accurate (DISK HITS)
Const ASSOCF_REMAPRUNDLL = &H80 'actually gets info about rundlls target if applicable
Const ASSOCF_NOFIXUPS = &H100 'attempt to fix errors if found
Const ASSOCF_IGNOREBASECLASS = &H200 'dont recurse into the baseclass
Const ASSOCSTR_COMMAND = 1 'return the shell\verb\command string
Const ASSOCSTR_EXECUTABLE = 2 'return the the executable part of command string
Const E_POINTER = &H80004003
Declare Function AssocQueryString Lib "shlwapi.dll" Alias "AssocQueryStringA" _
(ByVal flags As Long, ByVal lstr As Long, ByVal pszAssoc As String, ByVal pszExtra As String, _
ByVal pszOut As String, ByVal pcchOut As Long) As Long
'_________________________
Public Function fGetAppPath(Byval strExt As String) As String
'--- Given a file extension, returns the path to the file's executable
'--- Parameters
' [In]
' strExt: the file extension
'--- Return value
' the path to an executable or an empty string if an error occurs
Dim lngRtn As Long 'API function return value
Dim lngBuffLen As Long 'Length of buffer to receive the exe name
Dim strAppPath As String 'Buffer to receive the exe name
Dim lngFlags As Long 'Parameters for API function
'Check strTgt isn't empty
If strExt = vbNullString Then
fGetAppPath = vbNullString
Exit Function
End If
'Set the flags
lngFlags = ASSOCF_NOTRUNCATE Or ASSOCF_REMAPRUNDLL
'Init the file extension
strExt = "." & strExt & Chr$(0)
'Size the buffer for the return value
strAppPath = Space(255)
lngBuffLen = 255
'Get the exe path
lngRtn = AssocQueryString(lngFlags, ASSOCSTR_EXECUTABLE, strExt, "", strAppPath, lngBuffLen)
'Check the result
Select Case lngRtn
Case 0
'Success, do nothing
Case E_POINTER
'Buffer was too small resize it
strAppPath = Space(lngBuffLen)
lngRtn = AssocQueryString(lngFlags, ASSOCSTR_EXECUTABLE, strExt, "", strAppPath, lngBuffLen)
Case Else
'An error occurred - exit
fGetAppPath = ""
Exit Function
End Select
'Strip the terminating null char and remaining spaces
fGetAppPath = Left$(strAppPath, Instr(1, strAppPath, Chr$(0)) -1)
End Function
The call to AssocQueryString returns -2147024894 on XP Pro. I've searched the latest MSDN CD for this error but there's nothing relevant to AssocQueryString.
Paul Bent
Northwind IT Systems