Microsoft KB Archive/126220

{|
 * width="100%"|

VB3 Launch an App Based on File Extension Using ShellExecute
'Article ID: Q126220

Creation Date: 16-FEB-1995

Revision Date: 09-JAN-1997' The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

Windows, versions 2.0 and 3.0 SUMMARY

You can use the Windows API ShellExecute function to start the application associated with a given document extension without knowing the name of the associated application. For example, you could start the Paintbrush program by passing the filename ARCADE.BMP to the ShellExecute function.

MORE INFORMATION

The ShellExecute function opens or prints the specified file. Here is the declaration to use when calling this function from Visual Basic:

' Enter the following three lines as one, single line: Declare Function ShellExecute Lib "SHELL" (ByVal hwnd%, ByVal lpszOp$,     ByVal lpszFile$, ByVal lpszParams$, ByVal lpszDir$,      ByVal fsShowCmd%) As Integer Here is a table providing descriptions for each parameter: Parameter  Description hwnd%      Identifies the parent window. This window receives any message boxes an application produces (for example, for error           reporting).

lpszOp$    Points to a null-terminated string specifying the operation to            perform. This string can be "open" or "print." If this parameter is NULL, "open" is the default value.

lpszFile$  Points to a null-terminated string specifying the file to open. lpszParams$ Points to a null-terminated string specifying parameters passed to the application when the lpszFile parameter specifies an executable file. If lpszFile points to a           string specifying a document file, this parameter is NULL.

lpszDir$   Points to a null-terminated string specifying the default directory. fsShowCmd% Specifies whether the application window is to be shown when the application is opened. This parameter can be one of the values described in the API ShowWindow. Step-by-Step Example

The following example demonstrates how to start an application or load a document into its associated application. The Windows API ShellExecute function is different from the Visual Basic Shell function in that you can pass the ShellExecute function the name of a document and it will launch the associated application, and then pass the filename to the application.

 Start a New Project in Visual Basic (ALT, F, N), Form1 is created by default.  Add the following code to the general declarations section of Form1: Option Explicit Const SW_SHOWNORMAL = 1 ' Enter the following declaration on one single line Declare Function ShellExecute Lib "SHELL" (ByVal hwnd%, ByVal lpszOp$,      ByVal lpszFile$, ByVal lpszParams$, ByVal lpszDir$,      ByVal fsShowCmd%) As Integer Declare Function GetDesktopWindow Lib "USER" As Integer   Add the following procedure to the general declarations section of Form1: Function StartDoc (DocName As String) As Integer Dim Scr_hDC As Integer Scr_hDC = GetDesktopWindow StartDoc=ShellExecute(Scr_hDC,"Open",DocName,"","C:\",SW_SHOWNORMAL) End Function   Place the following code in the Form_Click event of Form1: Sub Form_Click Dim r As Integer r = StartDoc("C:\WINDOWS\ARCADE.BMP") End Sub 

General Information About the Process

The return value for the StartDoc function is the same as for the Shell function. It is the Windows instance handle of the application that was started.

The ShellExecute function returns the value 31 if there is no association for the specified file type or if there is no association for the specified action within the file type. Other error values are:

Error    Meaning 0       System was out of memory or executable file was corrupt.

2       The file was not found.

3       The path was not found.

5       Attempt was made to link to a task dynamically, or there was a sharing or network-protection error.

6       Library required separate data segments for each task.

8       There was insufficient memory to start the application.

10       The Windows version was incorrect.

11       The executable file was invalid. Either it was not a Windows- based application or there was an error in the .EXE image.

12       Application was designed for a different operating system.

13       Application was designed for MS-DOS version 4.0.

14       Type of executable file was unknown.

15       Attempt was made to load a real-mode application that was developed for an earlier version of Windows.

16       Attempt was made to load a second instance of an executable file containing multiple data segments not marked read-only.

19       Attempt was made to load a compressed executable file. The file must be decompressed before it can be loaded.

20       Dynamic-link library (DLL) file was invalid. One of         the DLLs required to run this application was corrupt.

21       Application requires Microsoft Windows 32-bit extensions. REFERENCES

"Programmer's Reference, Volume 2: Functions" of the Microsoft Windows Software Development Kit (SDK), pages 901-904.

ShellExecute topic of the Windows 3.1 SDK Help file.
 * }

-

"THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED 'AS IS' WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY." '' ©1997 Microsoft Corporation. All rights reserved. Legal Notices.

''

KBCategory: kbprg

KBSubcategory: APrgWindow

Additional reference words: 2.00 3.00 vb3only