From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to know exe is running and close the exe ?

Thanks Nick-

 

I just tried that and I received a link error that states 'Psapi.lib' is only available in the full development system.

 

Unfortunately,  I have the basic development system...

0 Kudos
Message 11 of 17
(1,698 Views)

I am looking for a way to not launch anoter instance of an application if it is already running. Is there a way to do this wih the basid development version of Labwindows CVI 2010?

 

0 Kudos
Message 12 of 17
(1,682 Views)

Hi,

 

If the other application is a CVI program, you can use CheckForDuplicateAppInstance

0 Kudos
Message 13 of 17
(1,680 Views)

Unfortunately, the other applications that I need to determine if they are running or not are not CVI applications. 

CheckForDuplicateAppInstance will only work if there is already "more than one" instance of a  non-CVI application ...

 

0 Kudos
Message 14 of 17
(1,676 Views)

Hi,

     i am not sure if this is possible in basic version, but you can try generate your own DLL import Lib:

    Open psapi.h in CVI and then select menu Options->Generate DLL import Library or Options->Generate DLL import Source

    (maybe will be  necessary to edit generated source file and clean up it).Windows library should be in

    C:\WINDOWS\system32 directory.

    Do not be afraid if some errors appear, just try generated library, or prepare simplified header file with only required functions(+typedefs +structs,...)

 

 I hope this help 🙂

 

 

 

0 Kudos
Message 15 of 17
(1,660 Views)

 

I tried the Options->Generate DLL import Source and I selected the psapi.dll from the windows32 folder
but it errored out with the following LabWindows CVI Message:
"Unable to generate DLL glue code.  Error in header file "C:...\Microsoft SDKs\Windows\V7.1\Include\"Psapi.h" at line 83, column 1: syntax error; found 'identifier' expecting ';'
So what I did instead was to remove the CVI version of the psapi.h & psapi.lib from my project and added toe ones from the Microsoft SDK to my project. I then rewrote your code a bit as I was only interested in searching for a certain file to see if it was already running or not and only to launch it if it was not already running.
The following code utilizing the Windows SDK is as follows: (Please excuse that i didn't remove the extra un-needed terminate code)
New Function definition:
static int CheckForAppInstance (char *appName);
Call from main():

filexist= CheckForAppInstance ("PeregrineGUI.exe");
if (filexist == FALSE)
  {
  LaunchExecutable ("C:\\PeregrineGUI.exe");
  for (delay =0; delay < 10; delay++);
  }
===========================================================
static int CheckForAppInstance (char *appName)
{
    DWORD numProcs, size, *pids = NULL;
    HANDLE process, processToKill;
    HMODULE module;
    int i, found = FALSE;
    char fPath[MAX_PATHNAME_LEN], *fName = NULL;
    
    // 256 is our initial guess at the number of processes
    size = 256 * sizeof (DWORD);
    do 
    {
        // if pids is not null, we did not have enough room for all the 
        // processes running on the system (initially, > 256 processes)
        // so we need to make more room
        if (pids != NULL) 
        {
            free (pids);
            size *= 2;
        }
        pids = malloc (size);
        EnumProcesses (pids, size, &numProcs);
    } while (numProcs == size); // if numProcs equals size, we did not have enough room for all the processes
    
    numProcs /= sizeof(DWORD);
    
    for (i = 0; i < numProcs; i++)
    {
        process = OpenProcess (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pids[i]);
        if (!process)
            continue;
        // the first module is always the executable file
        if (!EnumProcessModules (process, &module, sizeof(module), &size))
        {
            CloseHandle (process);
            continue;
        }
        GetModuleFileNameEx (process, module, fPath, sizeof(fPath));
        fName = _mbsrchr (fPath, '\\');
        if (!fName)
        {
            CloseHandle (process);
            continue;
        }
        if (!_mbsicmp (fName + 1, appName))
        {
            found = TRUE;
        }
        CloseHandle (process);
    }
    
return (found);

 

 

0 Kudos
Message 16 of 17
(1,649 Views)

Hi,

you can use CheckForDuplicateAppInstance only if is same program

0 Kudos
Message 17 of 17
(1,587 Views)