Online Book Reader

Home Category

Access Cookbook - Ken Getz [234]

By Root 2100 0
the entries in the registry when looking through REGEDIT.

In this solution, you use the FindExecutable function to get the name of the executable file associated with a selected data file. You also use the ShellExecute function to run the executable file, with the selected data file opened and ready to edit.

Load and run frmTestExecute, shown in Figure 11-9. To use this form, select a path (it defaults to your Windows directory when it first loads). Once the list box fills with all the files in the specified directory, click on one with the mouse. If there's an active file association for the selected file, the form will display that executable filename in a text box. If there's an associated executable file, you can run it and load your chosen file by double-clicking on the list box or clicking on the checkmark button.

Figure 11-9. The sample form, frmTestExecute, from 11-07.MDB

To use this functionality in your own applications, follow these steps:

Import the module basShellAPI from 11-07.MDB into your application.

To find the executable file associated with a given document, use the FindExecutable API function (aliased as acb_apiFindExecutable in the code). Call it with the three parameters described in Table 11-5.

Table 11-5. Parameters for the FindExecutable API function

Parameter

Type

Description

strFile

String

The filename that has an association in the registration database

strDir

String

The drive letter and path for the default directory (you can use "." to indicate the current directory)

strResult

String

A buffer to contain the returned executable name

The FindExecutable function returns an integer error code. If the value is greater than 32, the function has succeeded. Otherwise, it returns one of the error codes in Table 11-6 (note that these error codes are shared by several functions). If the function succeeded, strResult will be a null-terminated string containing the associated executable file. You'll need to trim off that trailing null character. One easy way to do this is by using the TrimNull function in basShellAPI, as follows:

Private Function TrimNull(strValue As String)

' Trim strValue at the first

' null character you find.

Dim intPos As Integer

intPos = InStr(strValue, vbNullChar)

If intPos > 0 Then

TrimNull = Left$(strValue, intPos - 1)

Else

TrimNull = strValue

End If

End Function

Table 11-6. Some shared error codes for FindExecutable and ShellExecute

Value

Meaning

0

System error occurred

2

File not found

3

Path not found

5

Sharing violation occurred

8

Not enough memory to start the task

27

Association incomplete

31

No association in the Registration Database for the file extension

32

DLL not found

For example, the following code will find the executable file associated with MyFile.OOG:

Dim strBuffer As String

Dim strResult As String

strBuffer = Space(128)

strResult = ""

intRetval = acb_apiFindExecutable("MyFile.OOG", ".", strBuffer)

If intRetval > acbcHinstanceErr Then

' Use the TrimNull function in basShellAPI

' to remove the trailing null character.

strResult = TrimNull(strBuffer)

End If

' Now, strResult holds either "" or the name

' of the executable you need.

To make this simpler, basShellAPI includes the acbFindExecutable function. This function requires the same parameters and returns the same values as acb_apiFindExecutable, but it handles the details of initializing the string buffer and trimming off the trailing null character for you. You'll want to use this function instead of calling the Windows API directly, as it will ensure that you use the correct methods for sending and receiving strings.

Once you know the name of the executable file associated with the selected document, you'll want to execute it with the ShellExecute API function. You could, of course, use the Shell command, but ShellExecute gives you a bit more flexibility, as a comparison of the two shows:

ShellExecute returns an error code if something goes wrong, but Shell requires that you write error-handling

Return Main Page Previous Page Next Page

®Online Book Reader