I am trying to call a C++ program for a Windows Forms App and to subsequently wait until the exe finishes. So far here is what I have:

Dim ProcessID As Integer
        Dim ProcessHandle As Integer

        ProcessID = Shell(filepath_exe, AppWinStyle.NormalFocus)

        ' Check the Shell command worked
        If ProcessID <> 0 Then

            ' Get a process handle for the PID
            ProcessHandle = OpenProcess(SYNCHRONIZE, 0, ProcessID)

            ' Wait for the program to finish using the process handle
            If ProcessHandle <> 0 Then
                If WaitForSingleObject(ProcessHandle, INFINITE) = WAIT_OBJECT_0 Then
                    ShellandWait = True
                Else
                    ShellandWait = False
                End If
            Else
                ' Failed to get process handle therefore flag error
                ShellandWait = False
            End If
        Else
            ShellandWait = False
        End If

When I run this, the exe executes correctly but the 'waiting part' of this function is not working. Specifically, the following error message appears after the exe has opened (corresponding to line 9 of the code above):

Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'F:\...\Debug\PCA2011_GUI.vshost.exe'.
Additional Information: A call to PInvoke function 'PCA2011_GUI!PCA2011_GUI.GUI::OpenProcess' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

PS I am using Visual Studio to compile and debug this program

Assuming for the moment that you have all the function declarations and constants included that you need for the Win32 function calls, you may want to check the actual contents of the return value (as in what datatype) from the Shell command and make sure it isn't conflicting with your ProcessID variable (which is an Integer).
I believe that the Shell function returns a Variant which contains a Double datatype. You may need to convert it with CInt (or CLng, depending) to use it with the OpenProcess Win32 function call, as that function wants a HANDLE (an "any" pointer, so can be integer or long integer).

Hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.