Solved

Using FindExecutable

Posted on 2000-03-20
5
585 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

789 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