Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API-Function OpenProcess always returns 0 1

Status
Not open for further replies.

inf33323

Technical User
Apr 24, 2001
26
DE
Hi there,

I need to call a .exe in the DOS-Command Window and display the return codes. I have the following code for this:

Private Const WIN32_API_INFINITE = -1&
Private Const WIN32_API_SYNCHRONIZE = &H100000
Private Const WIN32_API_PROCESS_QUERY_INFORMATION = &H400
Private Const WIN32_API_WAIT_OBJECT_0 = 0
Private Const WIN32_API_WAIT_TIMEOUT = &H102

Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hHandle As Long, ByRef nExitCode As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long

Public Function SyncShellCmd(CmdStr As String, ApplicationFocus As Long) As Long
'Run the application by the CmdStr, and exit when the program terminated, Application focus specific
'the focus of the application. e.g vbNormalFocus
Dim lPid As Long 'The task ID of the started program
Dim lHnd As Long ' handle to object to wait for
Dim lRet As Long 'Return value of WaitForSingleObject()
Dim nCode As Long

SyncShellCmd = -1
If CmdStr = "" Then Exit Function
lPid = Shell(CmdStr, ApplicationFocus)
If lPid <> 0 Then
'Get a handle to the shelled process.
lHnd = OpenProcess(WIN32_API_SYNCHRONIZE Or WIN32_API_PROCESS_QUERY_INFORMATION, 0, lPid)
'If successful, wait for the application to end and close the handle.
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, WIN32_API_INFINITE) 'no time out
If GetExitCodeProcess(lHnd, nCode) <> 0 Then SyncShellCmd = nCode
CloseHandle (lHnd)
End If
End If
End Function

Private Sub Command1_Click()
Dim nRes As Long
nRes = SyncShellCmd("c:\errLevel.exe", vbHide)
Debug.Print nRes
End Sub

The mehtod OpenProcess always returns 0. If I try to call notepad.exe or calc.exe, it's working fine.

Does anyone have an idea? I would appreciate any hint.
 
You are misusing the Shell function. This function returns the task id which is not the same as process Id needed by the OpenProcess.

Instead of:
Shell
OpenProcess
WaitForSingleObject

You should use:
hProcess = CreateProcess(...)
WaitForSingleObject(..hProcess)

HTH,

PS.You can find a lot of samples in MSDN. Just look for CreateProcess near Visual Basic


s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
The Shell/OpenProcess/WaitForSingleObject combination works only on NT-based systems. It does not work with Windows 98 and Me.
On the other hand, the CreateProcess/WaitForSingleObject combination works on Windows 98 as well.

See thread222-859655 for a discussion on both methods.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top