Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 636
  • Last Modified:

Using FindExecutable

I'm using the function FindExecutable to determine the associated application of a file.
But sometimes FindExecutable truncates the result after the first space if the associated application is in a directory that has a long file name.

I've read the article in the Knowledge Base that you have to replace the NULL Characters in the file name by spaces.
But it doesn't seem to work. I still only get the file name up to the first space.
0
rainerHOLZ
Asked:
rainerHOLZ
1 Solution
 
vbWayneCommented:

'found this sometime back perhaps it will help

'find the executable of a given file


Private Declare Function FindExecutableA Lib "shell32.dll" _
(ByVal lpFile As String, ByVal lpdirectory As String, _
ByVal lpResult As String) As Long

Private Const MAX_FILENAME_LEN = 256

'  Finds the executable associated with a file
'
'  Returns "" if no file is found.
'
Public Function FindExecutable(s As String) As String
   Dim i As Integer
   Dim s2 As String
   
   s2 = String(MAX_FILENAME_LEN, 32) & Chr$(0)
   
   i = FindExecutableA(s & Chr$(0), vbNullString, s2)
   
   If i > 32 Then
      FindExecutable = Left$(s2, InStr(s2, Chr$(0)) - 1)
   Else
      FindExecutable = ""
   End If
   
End Function

Private Sub Command1_Click()
MsgBox FindExecutable("c:\my documents\try.txt")
End Sub
0
 
VbmasterCommented:
Another solution would be to simply read the registry with the Registry API...
0
 
Erick37Commented:
This seems to work:

Private Sub Command1_Click()

    Dim success As Long
    Dim sResult As String
    Dim msg As String
    Dim i As Long

    sResult = Space$(MAX_PATH)
   
    success = FindExecutable("test.aaa", "c:\", sResult)

    Select Case success
    Case ERROR_FILE_NO_ASSOCIATION: msg = "no association"
    Case ERROR_FILE_NOT_FOUND: msg = "file not found"
    Case ERROR_PATH_NOT_FOUND: msg = "path not found"
    Case ERROR_BAD_FORMAT:     msg = "bad format"
    Case Is >= ERROR_FILE_SUCCESS:
        For i = 1 To Len(sResult)
            If Mid(sResult, i, 1) = Chr(0) Then
                Mid(sResult, i, 1) = " "
            End If
        Next
        msg = Trim(sResult)
   End Select
   MsgBox msg
End Sub
0
 
pclementCommented:
Rather than rewrite the Registry code, I used the following functions in the code below from:

http://www.thescarms.com/VBasic/Registry.htm

Private Sub Command1_Click()

Dim sCommandLine As String

sCommandLine = GetProgCommandLine(".doc")

End Sub

Function GetProgCommandLine(sFileExt As String) As String

Dim lVal As Long
Dim sSetting As String
Dim sShellKey As String

'Get ProgID for file extension
lVal = fReadValue("HKCR", sFileExt, "", "S", "", sSetting)

If sSetting <> "" Then
   
    sShellKey = sSetting & "\Shell\Open\Command"
    lVal = fReadValue("HKCR", sShellKey, "", "S", "", sSetting)
    If sSetting <> "" Then
        GetProgCommandLine = sSetting
    Else
        'Shell command line not found
    End If
Else
    'file extension not found
End If

End Function
0
 
rainerHOLZAuthor Commented:
Sorry that you had to wait so long time for your points but my mail server didn't work for several weeks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now