LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to find how many instances of a file are opened?

Hello all,

My application requires a txt file to be generated and saved, followed by a subsequent calling of an external text editor and then after a user has modified the file and saved it, I need to take control of the file again. One option I'd consider is to find out how many instances of the file are opened, which would let me keep track of external applications editing the file, and after no instances being opened then I can take control of the file again.
Well, that's the idea, but nonetheless I have not found any way of doing this or even any other possible way.

Thanks,

Julio Jansson.
0 Kudos
Message 1 of 4
(3,179 Views)
While what you suggest is probably possible, it would be much simpler to just wait until the text editor closes (if this is acceptable). You can use Windows SDK functions to accomplish this. Here is the general layout for a function to do this:


#include >windows.h<

int RunCommandAndWaitForExit(char *command, char *workingDirectory, int timeoutInSeconds)
{
int result = 0;
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(π, sizeof(pi));

if (CreateProcess(0, command, 0, 0, 0, CREATE_NO_WINDOW, 0, workingDirectory, &si, π))
{
result = (WAIT_OBJECT_0 == WaitForSingleObject(pi.hProcess, timeoutInSeconds));
if (result)
{
DWORD code;
if (GetExitCodeProcess(pi.hProcess, &code))
result = (code == 0);
else
result = 0;
}
if (pi.hProcess)
CloseHandle(pi.hProcess);
if (pi.hThread)
CloseHandle(pi.hThread);
}

return result;
}


If this is not workable for you, let me know and we can work on a different solution.

Regards,

Alex
0 Kudos
Message 2 of 4
(3,154 Views)
Hello Julio,

You could also use the system() function, which will launch an executable and wait until this executable is closed before returning to CVI.

For instance you could use the system() function to launch your text editor. It will wait until the instance of the text editor to be closed before continuing execution in CVI. Then you could take control of the application again by calling system(), or a similar function.

Hope that helps.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 3 of 4
(3,143 Views)
Thanks Guys,

Both solutions work and satisfy the requirements of my application. Thanks for your prompt help.

Cheers,

Julio Jansson.
0 Kudos
Message 4 of 4
(3,133 Views)