Microsoft KB Archive/170918: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - "&" to "&")
 
(2 intermediate revisions by the same user not shown)
Line 62: Line 62:
The ShellExecute function opens or prints the specified file. Following is the declaration to use when calling this function from Visual Basic:<br />
The ShellExecute function opens or prints the specified file. Following is the declaration to use when calling this function from Visual Basic:<br />


<pre class="codesample">  Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; _
<pre class="codesample">  Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                   (ByVal hwnd As Long, ByVal lpszOp As String, _
                   (ByVal hwnd As Long, ByVal lpszOp As String, _
                     ByVal lpszFile As String, ByVal lpszParams As String, _
                     ByVal lpszFile As String, ByVal lpszParams As String, _
Line 78: Line 78:


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


lpszFile      Points to a null-terminated string specifying the file
lpszFile      Points to a null-terminated string specifying the file
Line 107: Line 107:
<pre class="codesample">      Option Explicit
<pre class="codesample">      Option Explicit


       Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias _
       Private Declare Function ShellExecute Lib "shell32.dll" Alias _
       &quot;ShellExecuteA&quot; (ByVal hwnd As Long, ByVal lpszOp As _
       "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
       String, ByVal lpszFile As String, ByVal lpszParams As String, _
       String, ByVal lpszFile As String, ByVal lpszParams As String, _
       ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
       ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long


       Private Declare Function GetDesktopWindow Lib &quot;user32&quot; () As Long
       Private Declare Function GetDesktopWindow Lib "user32" () As Long


       Const SW_SHOWNORMAL = 1
       Const SW_SHOWNORMAL = 1


       Const SE_ERR_FNF = 2&amp;
       Const SE_ERR_FNF = 2&
       Const SE_ERR_PNF = 3&amp;
       Const SE_ERR_PNF = 3&
       Const SE_ERR_ACCESSDENIED = 5&amp;
       Const SE_ERR_ACCESSDENIED = 5&
       Const SE_ERR_OOM = 8&amp;
       Const SE_ERR_OOM = 8&
       Const SE_ERR_DLLNOTFOUND = 32&amp;
       Const SE_ERR_DLLNOTFOUND = 32&
       Const SE_ERR_SHARE = 26&amp;
       Const SE_ERR_SHARE = 26&
       Const SE_ERR_ASSOCINCOMPLETE = 27&amp;
       Const SE_ERR_ASSOCINCOMPLETE = 27&
       Const SE_ERR_DDETIMEOUT = 28&amp;
       Const SE_ERR_DDETIMEOUT = 28&
       Const SE_ERR_DDEFAIL = 29&amp;
       Const SE_ERR_DDEFAIL = 29&
       Const SE_ERR_DDEBUSY = 30&amp;
       Const SE_ERR_DDEBUSY = 30&
       Const SE_ERR_NOASSOC = 31&amp;
       Const SE_ERR_NOASSOC = 31&
       Const ERROR_BAD_FORMAT = 11&amp;
       Const ERROR_BAD_FORMAT = 11&


       Function StartDoc(DocName As String) As Long
       Function StartDoc(DocName As String) As Long
           Dim Scr_hDC As Long
           Dim Scr_hDC As Long
           Scr_hDC = GetDesktopWindow()
           Scr_hDC = GetDesktopWindow()
           StartDoc = ShellExecute(Scr_hDC, &quot;Open&quot;, DocName, _
           StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
           &quot;&quot;, &quot;C:\&quot;, SW_SHOWNORMAL)
           "", "C:\", SW_SHOWNORMAL)
       End Function
       End Function


       Private Sub Form_Click()
       Private Sub Form_Click()
           Dim r As Long, msg As String
           Dim r As Long, msg As String
           r = StartDoc(&quot;C:\WINDOWS\ARCADE.BMP&quot;)
           r = StartDoc("C:\WINDOWS\ARCADE.BMP")
           If r &lt;= 32 Then
           If r <= 32 Then
               'There was an error
               'There was an error
               Select Case r
               Select Case r
                   Case SE_ERR_FNF
                   Case SE_ERR_FNF
                       msg = &quot;File not found&quot;
                       msg = "File not found"
                   Case SE_ERR_PNF
                   Case SE_ERR_PNF
                       msg = &quot;Path not found&quot;
                       msg = "Path not found"
                   Case SE_ERR_ACCESSDENIED
                   Case SE_ERR_ACCESSDENIED
                       msg = &quot;Access denied&quot;
                       msg = "Access denied"
                   Case SE_ERR_OOM
                   Case SE_ERR_OOM
                       msg = &quot;Out of memory&quot;
                       msg = "Out of memory"
                   Case SE_ERR_DLLNOTFOUND
                   Case SE_ERR_DLLNOTFOUND
                       msg = &quot;DLL not found&quot;
                       msg = "DLL not found"
                   Case SE_ERR_SHARE
                   Case SE_ERR_SHARE
                       msg = &quot;A sharing violation occurred&quot;
                       msg = "A sharing violation occurred"
                   Case SE_ERR_ASSOCINCOMPLETE
                   Case SE_ERR_ASSOCINCOMPLETE
                       msg = &quot;Incomplete or invalid file association&quot;
                       msg = "Incomplete or invalid file association"
                   Case SE_ERR_DDETIMEOUT
                   Case SE_ERR_DDETIMEOUT
                       msg = &quot;DDE Time out&quot;
                       msg = "DDE Time out"
                   Case SE_ERR_DDEFAIL
                   Case SE_ERR_DDEFAIL
                       msg = &quot;DDE transaction failed&quot;
                       msg = "DDE transaction failed"
                   Case SE_ERR_DDEBUSY
                   Case SE_ERR_DDEBUSY
                       msg = &quot;DDE busy&quot;
                       msg = "DDE busy"
                   Case SE_ERR_NOASSOC
                   Case SE_ERR_NOASSOC
                       msg = &quot;No association for file extension&quot;
                       msg = "No association for file extension"
                   Case ERROR_BAD_FORMAT
                   Case ERROR_BAD_FORMAT
                       msg = &quot;Invalid EXE file or error in EXE image&quot;
                       msg = "Invalid EXE file or error in EXE image"
                   Case Else
                   Case Else
                       msg = &quot;Unknown error&quot;
                       msg = "Unknown error"
               End Select
               End Select
               MsgBox msg
               MsgBox msg
Line 181: Line 181:
If the function succeeds, the return value is the instance handle of the application that was run. If there was an error, the return value is less than or equal to 32.<br />
If the function succeeds, the return value is the instance handle of the application that was run. If there was an error, the return value is less than or equal to 32.<br />
<br />
<br />
The file specified by the lpszFile parameter can be a document file or an executable file. If it is a document file, this function opens or prints it depending on the value of the lpszOp parameter. If it is an executable file, this function opens it even if the lpszOp specifies &quot;PRINT.&quot;
The file specified by the lpszFile parameter can be a document file or an executable file. If it is a document file, this function opens or prints it depending on the value of the lpszOp parameter. If it is an executable file, this function opens it even if the lpszOp specifies "PRINT."


</div>
</div>
Line 188: Line 188:
== REFERENCES ==
== REFERENCES ==


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


</div>
</div>

Latest revision as of 12:29, 21 July 2020

Article ID: 170918

Article Last Modified on 8/30/2004



APPLIES TO

  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition



This article was previously published under Q170918

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. Following is the declaration to use when calling this function from Visual Basic:

   Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                   (ByVal hwnd As Long, ByVal lpszOp As String, _
                    ByVal lpszFile As String, ByVal lpszParams As String, _
                    ByVal LpszDir As String, ByVal FsShowCmd As Long) _
                    As Long
                


The following table provides 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.
                


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.

  1. Start a New project in Visual Basic. Form1 is created by default.
  2. Add the following code to the General Declarations section of Form1:

          Option Explicit
    
          Private Declare Function ShellExecute Lib "shell32.dll" Alias _
          "ShellExecuteA" (ByVal hwnd As Long, ByVal lpszOp As _
          String, ByVal lpszFile As String, ByVal lpszParams As String, _
          ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
    
          Private Declare Function GetDesktopWindow Lib "user32" () As Long
    
          Const SW_SHOWNORMAL = 1
    
          Const SE_ERR_FNF = 2&
          Const SE_ERR_PNF = 3&
          Const SE_ERR_ACCESSDENIED = 5&
          Const SE_ERR_OOM = 8&
          Const SE_ERR_DLLNOTFOUND = 32&
          Const SE_ERR_SHARE = 26&
          Const SE_ERR_ASSOCINCOMPLETE = 27&
          Const SE_ERR_DDETIMEOUT = 28&
          Const SE_ERR_DDEFAIL = 29&
          Const SE_ERR_DDEBUSY = 30&
          Const SE_ERR_NOASSOC = 31&
          Const ERROR_BAD_FORMAT = 11&
    
          Function StartDoc(DocName As String) As Long
              Dim Scr_hDC As Long
              Scr_hDC = GetDesktopWindow()
              StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
              "", "C:\", SW_SHOWNORMAL)
          End Function
    
          Private Sub Form_Click()
              Dim r As Long, msg As String
              r = StartDoc("C:\WINDOWS\ARCADE.BMP")
              If r <= 32 Then
                  'There was an error
                  Select Case r
                      Case SE_ERR_FNF
                          msg = "File not found"
                      Case SE_ERR_PNF
                          msg = "Path not found"
                      Case SE_ERR_ACCESSDENIED
                          msg = "Access denied"
                      Case SE_ERR_OOM
                          msg = "Out of memory"
                      Case SE_ERR_DLLNOTFOUND
                          msg = "DLL not found"
                      Case SE_ERR_SHARE
                          msg = "A sharing violation occurred"
                      Case SE_ERR_ASSOCINCOMPLETE
                          msg = "Incomplete or invalid file association"
                      Case SE_ERR_DDETIMEOUT
                          msg = "DDE Time out"
                      Case SE_ERR_DDEFAIL
                          msg = "DDE transaction failed"
                      Case SE_ERR_DDEBUSY
                          msg = "DDE busy"
                      Case SE_ERR_NOASSOC
                          msg = "No association for file extension"
                      Case ERROR_BAD_FORMAT
                          msg = "Invalid EXE file or error in EXE image"
                      Case Else
                          msg = "Unknown error"
                  End Select
                  MsgBox msg
              End If
          End Sub
                            
  3. Run the project and click the form.


General Information About the Process

If the function succeeds, the return value is the instance handle of the application that was run. If there was an error, the return value is less than or equal to 32.

The file specified by the lpszFile parameter can be a document file or an executable file. If it is a document file, this function opens or prints it depending on the value of the lpszOp parameter. If it is an executable file, this function opens it even if the lpszOp specifies "PRINT."

REFERENCES

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


Additional query words: kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport kbVBp400 KBWIN32SDK KBAPI

Keywords: kbhowto KB170918