LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

LaunchExecutableEx does not return valid handle

My call to LaunchexecutableEx launches the executable successfully, and returns a status code of 0 (success). However, the handle returned is also 0, which is an invalid handle when I later call ExecutableHasTerminated. I can't seem to get anything but 0 back for the handle, which I am passing by reference. Has anyone have any ideas about this problem? Thanks!
0 Kudos
Message 1 of 10
(5,855 Views)
Hello hjb,

Regarding your questions, you can have a valid handle returned from LaunchExecutableEx that is a value of 0. 

In particular, if you run the following code, the LaunchExecutableEx function returns a handle of 0, and a return value of 0, which indicates success.
Then, when you pass the handle of 0 into the ExecutableHasTerminated function, it works as expected.  Please let me know if you do not see the same behavior, with the simple example I have attached.

#include <utility.h>
#include <cvirte.h>
static int handle;
int returnVal;

int main (int argc, char *argv[])
{
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;    /* out of memory */
   
    returnVal = LaunchExecutableEx ("c:\\WINDOWS\\system32\\notepad.exe", LE_SHOWNORMAL, &handle);
    returnVal = ExecutableHasTerminated (handle);
    return 0;
}

If you do see the same behavior, please let me know if you are able to try out the CVI executable functions with other Windows executables and if you see correct or incorrect behavior. Also, if you use the test application above and just enter the path to your executable, does it exhibit incorrect behavior.

Let me know how this goes.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 10
(5,822 Views)
Hi Wendy, Thanks so much for your reply. Yes, your code works as advertised. However, it won't solve my problem. What I neglected to say in my original post is that the my call to LaunchExecutableEx and my call to ExecutableHasTerminated do not occur within the same program. I am calling LaunchExecutableEx in one program, and that program then terminates and passes the handle obtained from LaunchExecutableEx to another program as a calling argument. That program in turn then calls ExecutableHasTerminated using the passed handle. Here I am getting the return code of -1 (invalid handle). I suspect that I cannot do what I intended. Do you have any suggestions to fix or circumvent this? Thanks again! - Harald
0 Kudos
Message 3 of 10
(5,816 Views)
Harald -

There are Win32 rules about passing handles between processes. 

Is the second process (that's using the ExecutableHasTerminated() function) a child process?

The Win32 CreateProcess call, used to create a "child" process, is parameterized as to how to treat handle inheritance.  You can establish that the child process simply inherits the parent process's handles.

I suspect LaunchExecutableEx is using the Win32 SDK CreateProcess function, but you can't tell how they deal with handle inheritance.

The issue is further complicated if the process receiving the handle is not a child process.  You may have to establish access rights to the handle you pass.

An alternative that might be a tad simpler would be to use a named mutex or other kernel object (e.g. semaphore) to signal process completion. 

If you're willing to learn the Win32 SDK Process functions and behavior, you'll be able to more clearly understand and better control what's happening.  NI's trying to help simplify the complexity with the LaunchExecutable functions, but like other CVI "front ends" to Win32 SDK calls, they don't always yield the degree of control that you need.

Hayes
0 Kudos
Message 4 of 10
(5,792 Views)
Thanks, Hayes, for your time in offering your explanation. I suspected that my problem was a handle inheritance issue. I have found a solution unrelated to using the process handles. Regards, Harald.
0 Kudos
Message 5 of 10
(5,783 Views)

Dear Wendy L.,

I tried your simple example and LaunchExecutableEx() does indeed wait for the execution of notepad to finish.  I then modified your program to run notepad, then wordpad, and then notepad again and each time the program spun waiting for the executable to finish before starting the next executable.  However, if you substitute the "setup.exe" program  from a CVI Distribution Kit it does not wait.  At least, our application's setup.exe does not wait.  It initially returns a "0" status but eventually returns a "1" indicating the setup.exe finished installing.  Meanwhile the Distribution Kit is still running.  We are trying to create a simple CVI application that installs three different CVI Distribution Kits sequentially.  Instead of running them squentially as we want, our application runs the first then the second and then the third -- all three of them run concurrently.

Is this because the setup.exe programs are 16-bit executables?  The CVI help page on LaunchExecutableEx() specifically states that LaunchExecutableEx() will fail to work if the launched application is a 16-bit application.  This would explain why the setup.exe programs do not work (do not run to completion before returning).

Do you know some way that I can get setup.exe to work properly with LaunchExecutableEx()?

-- Don

0 Kudos
Message 6 of 10
(5,675 Views)
Hello Don.

You can probably achieve what you want using one of the Advanced Distribution Kit Options. From the Create Distribution Kit help:

"The Advanced Distribution Kit Options dialog box has the following options:

Executable Filename — The name of an executable file to run after the user installation is complete. Use the Select button to select a file that you have already added to one of the file groups.

If you want to run an application that already exists on the target system, create and include, in one of your file groups, a .bat batch file that specifies the application you want to run. You can use the Windows START utility in the batch file before the application name if you do not want the installer to wait on the launched application to exit. Choose this batch file as the executable file to run after installation."

You could use a batch file included in each installer to chain to the subsequent installer. There are special environment variables available to assist you with this.

I believe that LaunchExecutableEx() is behaving properly; setup.exe completes before the installation is finished. Executing the .msi files instead of the setup.exe's may fix the problem (I have not tested this).

Good luck,
Colin.
0 Kudos
Message 7 of 10
(5,650 Views)

I think you're right about setup.exe finishing quickly.  I read that "setup.exe" only checks for the presence of the msi program in the operating system and installs it if it is missing (for Win95/98 and Win2K).  I think it then kicks off the install using msi and returns.  That would explain the behavior we've observed  Today I will try to check on starting the msi program instead of setup.exe to see if that solves our problem.  Thanks for the tip, Colin.  I'll post my results soon.

-- Don

0 Kudos
Message 8 of 10
(5,634 Views)
Colin,
     By using "msiexec /i msifilename.msi" instead of "setup.exe" we were able to create a CVI program that correctly installs multiple CVI applications sequentially.  The LaunchExecutableEx function  running the msiexec.exe for each install did not come back until the installation was completely done.  When LaunchExecutableEx was used to run the three separate "setup.exe" programs (one in each Distribution Kit) the calls came back immediately and would run the next LaunchExecutableEx and then the next so that all three installations were going at once.  Using msiexec.exe with LaunchExecutableEx instead of setup.exe fixed our problem.  Thanks for your help Colin.  I've been working on this problem off and on for a long time.  You nailed it.
-- Don
0 Kudos
Message 9 of 10
(5,622 Views)
You're welcome, Don.

You might need to consider the possibility that the target computer is not running the installer service. In this case, you would need to restart the service (if present) or manually execute setup.exe, which will install the service and your application.

Colin.
0 Kudos
Message 10 of 10
(5,600 Views)