Toon posts:

[VB6] msdos programma starten

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik wil vanuit een vb6 applicatie een msdos programma starten. De 'normale' [shell] methode werkt niet, slaat vast :-((

Wat te doen? 8)7

  • .oisyn
  • Registratie: September 2000
  • Nu online

.oisyn

Moderator Devschuur®

Demotivational Speaker

CreateProcess ()
hoewel ik me afvraag of dat wel gaat werken als shell niet werkt (want shell roept intern vast ook CreateProcess aan)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • robjanssen
  • Registratie: September 2001
  • Laatst online: 02-08 16:10

robjanssen

Software Developer

ShellExecute

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Option Explicit

      Public 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

      Public Declare Function GetDesktopWindow Lib "user32" () As Long

' hWnd = Window handle to a parent window.
' This window receives any message boxes that an application produces.
'
' lpszOp = Address of a null-terminated string that specifies the operation to perform.
' The following operation strings are valid:
' open, print, explore
' This parameter can be NULL. In that case, the function opens the file specified
' by lpFile.
'
' lpFile = Address of a null-terminated string that specifies the file to open
' or print or the folder to open or explore. The function can open an executable
' file or a document file. The function can print a document file.
'
' lpParameters = If the lpFile parameter specifies an executable file,
' lpParameters is an address to a null-terminated string that specifies
' the parameters to be passed to the application.
' If lpFile specifies a document file, lpParameters should be NULL.
'
' lpDirectory = Address of a null-terminated string that specifies
' the default directory.
'
' nShowCmd = If lpFile specifies an executable file, nShowCmd specifies
' how the application is to be shown when it is opened.
' This parameter can be one of the following values:

    Const SW_HIDE = 0                '  Hides the window and activates another window.
    Const SW_MAXIMIZE = 3            '  Maximizes the specified window.
    Const SW_MINIMIZE = 6            '  Minimizes the specified window and activates the next top-level window in the z-order.
    Const SW_RESTORE = 9             '  Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
    Const SW_SHOW = 5                '  Activates the window and displays it in its current size and position.
    Const SW_SHOWDEFAULT = 10        '  Sets the show state based on the SW_ flag specified in theSTARTUPINFO structure passed to theCreateProcess function by the program that started the application. An application should callShowWindow with this flag to set the initial show state of its main window.
    Const SW_SHOWMAXIMIZED = 3       '  Activates the window and displays it as a maximized window.
    Const SW_SHOWMINIMIZED = 2       '  Activates the window and displays it as a minimized window.
    Const SW_SHOWMINNOACTIVE = 7     '  Displays the window as a minimized window. The active window remains active.
    Const SW_SHOWNA = 8              '  Displays the window in its current state. The active window remains active.
    Const SW_SHOWNOACTIVATE = 4      '  Displays a window in its most recent size and position. The active window remains active.
    Const SW_SHOWNORMAL = 1          '  Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

' If lpFile specifies a document file, nShowCmd should be zero.
' You can use this function to open or explore a shell folder.
' To open a folder, use either
'
' ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
' or
' ShellExecute(handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL);
'
' To explore a folder, use the following call:
'
' ShellExecute(handle, "explore", path_to_folder, NULL, NULL, SW_SHOWNORMAL);
'
' If lpOperation is NULL, the function opens the file specified by lpFile. If lpOperation is "open" or "explore", the function will attempt to open or explore the folder.
'
' To obtain information about the application that is launched as a result of calling
'
' Returns a value greater than 32 if successful, or an error value that is less
' than or equal to 32 otherwise. The following table lists the error values.
' The return value is cast as an HINSTANCE for backward compatibility with 16-bit
' Microsoft® Windows® applications. It is not a true HINSTANCE, however.
' The only thing that can be done with the returned HINSTANCE is to cast it to an
' integer and compare it with the value 32 or one of the error codes below.

    Const SE_ERR_FNF = 2                 '  File not found
    Const SE_ERR_PNF = 3                 '  Path not found
    Const SE_ERR_ACCESSDENIED = 5        '  Access denied
    Const SE_ERR_OOM = 8                 '  Out of memory
    Const SE_ERR_DLLNOTFOUND = 32        '  DLL not found
    Const SE_ERR_SHARE = 26              '  A sharing violation occurred
    Const SE_ERR_ASSOCINCOMPLETE = 27    '  Incomplete or invalid file association
    Const SE_ERR_DDETIMEOUT = 28         '  DDE Time out
    Const SE_ERR_DDEFAIL = 29            '  DDE transaction failed
    Const SE_ERR_DDEBUSY = 30            '  DDE busy
    Const SE_ERR_NOASSOC = 31            '  No association for file extension
    Const ERROR_BAD_FORMAT = 11&         '  Invalid EXE file or error in EXE image
    Const ERROR_FILE_NOT_FOUND = 2&      '  The specified file was not found.
    Const ERROR_PATH_NOT_FOUND = 3&      '  The specified path was not found.
    Const ERROR_BAD_EXE_FORMAT = 193&    '  The .exe file is invalid (non-Win32® .exe or error in .exe image).


Public Function ShellExecLaunchFile(ByVal strPathFile As String, ByVal strOpenInPath As String, ByVal strArguments As String) As Long

    Dim Scr_hDC As Long
    
    'Get the Desktop handle
    Scr_hDC = GetDesktopWindow()
    
    'Launch File
    ShellExecLaunchFile = ShellExecute(Scr_hDC, "Open", strPathFile, "", strOpenInPath, SW_SHOWNORMAL)

End Function


Public Function ShellExecLaunchErr(ByVal lngErrorNumber As Long, ByVal blnRaiseMsg As Boolean) As String
    
    Dim msg As VbMsgBoxResult
    Dim strErrorMessage As String
    
    If lngErrorNumber < 33 Then
        'There was an error
        Select Case lngErrorNumber
            Case SE_ERR_FNF
                strErrorMessage = "File not found"
            Case SE_ERR_PNF
                strErrorMessage = "Path not found"
            Case SE_ERR_ACCESSDENIED
                strErrorMessage = "Access denied"
            Case SE_ERR_OOM
                strErrorMessage = "Out of memory"
            Case SE_ERR_DLLNOTFOUND
                strErrorMessage = "DLL not found"
            Case SE_ERR_SHARE
                strErrorMessage = "A sharing violation occurred"
            Case SE_ERR_ASSOCINCOMPLETE
                strErrorMessage = "Incomplete or invalid file association"
            Case SE_ERR_DDETIMEOUT
                strErrorMessage = "DDE Time out"
            Case SE_ERR_DDEFAIL
                strErrorMessage = "DDE transaction failed"
            Case SE_ERR_DDEBUSY
                strErrorMessage = "DDE busy"
            Case SE_ERR_NOASSOC
                strErrorMessage = "No association for file extension"
            Case ERROR_BAD_FORMAT
                strErrorMessage = "Invalid EXE file or error in EXE image"
            Case ERROR_FILE_NOT_FOUND
                strErrorMessage = "The specified file was not found."
            Case ERROR_PATH_NOT_FOUND
                strErrorMessage = "The specified path was not found."
            Case ERROR_BAD_EXE_FORMAT
                strErrorMessage = "The .exe file is invalid (non-Win32® .exe or error in .exe image)."
            Case Else
                strErrorMessage = "Unknown error"
        End Select
        
        'If the blnRaiseMsg = True then raise a MsgBox with error
        If blnRaiseMsg = True Then msg = MsgBox(strErrorMessage, vbCritical, "Error:")
        
        'Return Error string
        ShellExecLaunchErr = blnRaiseMsg
    
    End If
    
End Function


' So the way to use all this is:
'
'    Dim lngReturnNumber As Long
'
'    lngReturnNumber = ShellExecLaunchFile(txtPathFile.Text, txtStartPath.Text, txtArguments.Text)
'    If lngReturnNumber < 33 Then
'        Call ShellExecLaunchErr(lngReturnNumber, True)
'        Exit Sub
'    End If
'
'==================================================================================
'
'
'Use the following runRegEntry Function to Silently Run .reg Files
'
Public Function runRegEntry(strPathFile As String) As Boolean
    On Error GoTo Command1Err
    
    Dim dblTemp As Double
    dblTemp = Shell("regedit.exe /s " & strPathFile, vbHide)
    
    runRegEntry = True
    
    Exit Function
    
Command1Err:
    Dim msg As VbMsgBoxResult

    msg = MsgBox("Error # " & CStr(Err.Number) & " " & Err.Description & vbNewLine & "With: " & strPathFile, vbCritical, "Error:")
    Err.Clear    ' Clear the error.
    runRegEntry = False
    
End Function

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 28-08 12:00

Janoz

Moderator Devschuur®

!litemod

Verwijderd schreef op 23 september 2002 @ 21:10:
De 'normale' [shell] methode werkt niet, slaat vast :-((

Al eens geprobeert te bedenken waarom shell vastslaat?

Je programmatje gaat pas verder als de in de shell aangeroepen opdracht klaar is toch?

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


  • WimB
  • Registratie: Juli 2001
  • Laatst online: 30-03-2024
Janoz schreef op 23 september 2002 @ 21:54:

[...]

Al eens geprobeert te bedenken waarom shell vastslaat?

Je programmatje gaat pas verder als de in de shell aangeroepen opdracht klaar is toch?
Normaalgezien niet. In Visual Basic wordt het programma opgeroepen en gaat hij direct verder met het uitvoeren van de rest van het script. Als ik mij toch niet vergis...

Verwijderd

WimB schreef op 23 september 2002 @ 21:56:
[...]

Normaalgezien niet. In Visual Basic wordt het programma opgeroepen en gaat hij direct verder met het uitvoeren van de rest van het script. Als ik mij toch niet vergis...
Klopt, behalve als je het aanroept als volgt:

Visual Basic .NET:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
Public Type STARTUPINFO
   cb As Long
   lpReserved As String
   lpDesktop As String
   lpTitle As String
   dwX As Long
   dwY As Long
   dwXSize As Long
   dwYSize As Long
   dwXCountChars As Long
   dwYCountChars As Long
   dwFillAttribute As Long
   dwFlags As Long
   wShowWindow As Integer
   cbReserved2 As Integer
   lpReserved2 As Long
   hStdInput As Long
   hStdOutput As Long
   hStdError As Long
End Type
 
Public Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessID As Long
   dwThreadID As Long
End Type

Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_MAXIMIZE = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Const SW_MAX = 10


Public Const NORMAL_PRIORITY_CLASS = &H20&
Public Const INFINITE = -1&

Public Declare Function CreateProcessA Lib "kernel32" (ByVal _
   lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
   lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
   ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
   ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
   lpStartupInfo As STARTUPINFO, lpProcessInformation As _
   PROCESS_INFORMATION) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
   hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Declare Function GetExitCodeProcess Lib "kernel32" _
   (ByVal hProcess As Long, lpExitCode As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" (ByVal _
   hObject As Long) As Long

Public Function ShellAndWait(ByVal strPath As String, _
   ByVal iWindowStyle As Integer, ByRef lreturnCode As Long, _
   Optional sWinTitle As String = "", _
   Optional sDirectoryPath As String = "") _
   As Boolean
 
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
 
On Error GoTo ShellAndWaiterr
 

   ' Initialize the STARTUPINFO structure:
   start.cb = Len(start) ' you must set the size
   start.dwFlags = &H1& ' STARTF_USESHOWWINDOW Use Show Window
   start.wShowWindow = iWindowStyle
   If Not IsMissing(sWinTitle) Then
      ' if there is a title set the window title
      start.lpTitle = sWinTitle
   End If
    
   ' Start the shelled application:
   ret = CreateProcessA(0&, strPath, 0&, 0&, 1&, _
   NORMAL_PRIORITY_CLASS, 0&, _
   sDirectoryPath, start, _
   proc)
    
   ' Wait for the shelled application to finish:
   ret = WaitForSingleObject(proc.hProcess, 100&)
   Do While ret <> 0
      If ret < 0 Then
          ShellAndWait = False
          Exit Function
      End If
    
      DoEvents
    
      ret = WaitForSingleObject(proc.hProcess, _
          100&)
   Loop
    
   'get the return code
   ret = GetExitCodeProcess(proc.hProcess, _
   lreturnCode)
    
   'close the process handles
   ret = CloseHandle(proc.hProcess)
   ShellAndWait = True
Exit Function
 
ShellAndWaiterr:
   ShellAndWait = False
   Exit Function
   Resume
End Function

Verwijderd

lig eraan wat je wil starten...
een ping kan je wel met shell of shellexecute starten...

Verwijderd

Verwijderd schreef op 24 september 2002 @ 09:46:
lig eraan wat je wil starten...
een ping kan je wel met shell of shellexecute starten...
ping is dan ook geen msdos programma

Verwijderd

Lukt het verder wel met windows-programma"s?, want als dat zo is, moet je eens kijken of je gewoon ook de dosprogramma's kunt opstarten. Het kan btw ook liggen aan de windows versie die je hebt, bij win 95 bijvoorbeeld moet je eerst op ja drukken voordat windows het dos-programma opstart...

Verwijderd

Verwijderd schreef op 24 september 2002 @ 09:58:
[...]


ping is dan ook geen msdos programma
Wat is het dan? :/

  • Sponge
  • Registratie: Januari 2002
  • Laatst online: 28-08 17:06

Sponge

Serious Game Developer

Win32 console programma. :) Als je ping.exe (tracert.exe, etc) in DOS probeert krijg je waarschijnlijk "Windows/win32 is required to run this program."

Edit:

of "This program cannot run in DOS mode"

Verwijderd

ja?, nooit geprobeert om alleen in dos te starten die sjit :)

  • Sponge
  • Registratie: Januari 2002
  • Laatst online: 28-08 17:06

Sponge

Serious Game Developer

Ach, ik dacht er nu eigenlijk ook pas aan :).
Pagina: 1