Solved

Using FindExecutable

Posted on 2000-03-20
5
527 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
5 Comments
 
LVL 1

Accepted Solution

by:
vbWayne earned 100 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now