LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to know exe is running and close the exe ?

Highlighted
I use launchExecutableEx() to run a exe,but before running ,I want to know if this exe is running,if running I will terminal it at first, which labwindows function can do it? I know TerminateExecutable() function,but it need a handle, i do not know the handle because the exe is running before run my program.
0 Kudos
Message 1 of 17
(5,339 Views)
Highlighted

Not sure if all this is can really be helpful (I didn't tested it), but usinf some SDK functions you could:

 

  1. List all running processes using EnumProcesses. Inside the SDK that comes with CVI you can search an example called "Enumerating all processes": this is the skeleton of this part of the job
  2. Scan the list to obtain the process ID of your target executable, if running. You will need to know the exact name how the process appears in the list of active processes in the system: this can be difficult if the application is run under svchost or any other host process
  3. Use OpenProcess and TerminateProcess to kill it: this is actually equivalent to terminating an application inside the task manager; it's a very rude method and the SDK lists all reasons why it should be used very rarely. In any case, if you feel this is safe for your environment it could be tempted
As I told you, this is only a framework for you to elaborate on: I never attempted all that. I just wanted to explain how I'd approach this job if I have to.


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 17
(5,313 Views)
Highlighted

There is the CheckForDuplicateAppInstance function but it is useful to you if you can edit the code of the executable you are calling.

Read the function help and see if it works for you.

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 17
(5,309 Views)
Highlighted

As far as I can understand CheckForDuplicateAppInstance cannot be of any help in this case. This function checks if there is another running instance of the program in execution, while tnggio wants to check another running app. Additionally, the function does not give any handle to the running application, so there is no possibility to terminate it in any way.

 

But let's see if tanggio comments this: it may be that the situation is not as I have figured it.

Message Edited by Roberto Bozzolo on 12-17-2009 09:21 AM


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 17
(5,287 Views)
Highlighted

Yeah, I'm aware that function maybe helpful only for specific conditions where the exe code (hopefully written in CVI) being run needs to be available.

 

But if this is the case that exe can call CheckForDuplicateAppInstance and close itself if already being run.

That was best shot since I could not help much with the SDK 😉 

S. Eren BALCI
IMESTEK
0 Kudos
Message 5 of 17
(5,282 Views)
Highlighted

I had to write something similar recently, so I thought I'd post a simplified version of my code (most error handling removed for clarity's sake).  It is almost exactly what Roberto described, and does not try to account for any special cases.  When running this code under CVI, you will have to add psapi.lib to the project.

 

Should hopefully help get you started.

 

NickB

National Instruments 

0 Kudos
Message 6 of 17
(5,267 Views)
Highlighted

So I used the psapi.lib and example code from the earlier post-- all works fine in CVI6.0.

 

Now, we moved code to a CVI9 installation and it no longer worked--instead getting:

 

Undefined symbol '_K32EnumProcesses@12' referenced in "find_process.c".
Undefined symbol '_K32EnumProcessModules@16' referenced in "find_process.c".
Undefined symbol '_K32GetModuleBaseNameA@16' referenced in "find_process.c".
Did psapi.lib change between v6.0 and v9.0?  I don't get the '_K32' prefix in the err messages. Any ideas?

0 Kudos
Message 7 of 17
(4,626 Views)
Highlighted

Hello -

 

My best guess is that you're using a version of the psapi.h header file that did not ship with LabWindows/CVI 9.0 (perhaps using it from a windows sdk or visual studio installation).  The reason for this guess is that in the most recent versions of the Win32 API (Windows 7, Server 2008 R2 and later), EnumProcesses, EnumProcessModules, etc, are defined as follows, and are exported from Kernel32.lib and Kernel32.dll:

 

#if (PSAPI_VERSION > 1)
#define EnumProcesses        K32EnumProcesses
#define EnumProcessModules   K32EnumProcessModules
#define EnumProcessModulesEx K32EnumProcessModulesEx
...

 

In general, you should use the Win32 API headers and libs that ship with LabWindows/CVI to avoid this kind of thing.

 

NickB

National Instruments

0 Kudos
Message 8 of 17
(4,587 Views)
Highlighted

Hi Nick-

 

I tried your sample program and received similar results. I added the psapi.h file from the CVI2010\sdk\include folder and at run time I get hte following errors:

 

 

Project link errors
 Undefined symbol '_EnumProcesses@12' referenced in "EnumProcesses.c".
 Undefined symbol '_EnumProcessModules@16' referenced in "HEnumProcesses.c".
 Undefined symbol '_GetModuleFileNameExA@16' referenced in "EnumProcesses.c"
I do have the Windows SDK on my test system but this psapi.h file resides in a different folder than what I added to the project.
Any ideas?

 

0 Kudos
Message 9 of 17
(4,272 Views)
Highlighted

Did you add psapi.lib from CVI2010\sdk\lib\msvc to your project?  If not, just browse to that directory, and drag psapi.lib into your project tree.

 

As a general side note, when you are using CVI, you should only use the SDK include files and libs that install as part of the CVI installation - not the SDK you can download online.  This is because CVI makes some changes to the header files to ensure compatibility with the CVI environment.

 

NickB

National Instruments

0 Kudos
Message 10 of 17
(4,270 Views)