?
Solved

Using FindExecutable

Posted on 2000-03-20
5
Medium Priority
?
618 Views
Last Modified: 2008-03-03
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
Comment
Question by:rainerHOLZ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
vbWayne earned 400 total points
ID: 2636726

'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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2636765
Another solution would be to simply read the registry with the Registry API...
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2636898
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
 
LVL 1

Expert Comment

by:pclement
ID: 2637087
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
 

Author Comment

by:rainerHOLZ
ID: 2722294
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question