14 Replies - 910 Views - Last Post: 21 April 2019 - 01:12 PM Rate Topic: -----

#1 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

how can i test if the file is open from another process?

Posted 20 April 2019 - 03:12 AM

i know the folder and file name, and i can test if the file exists.... but how can i test if the file is open from another process?
FILE *text;
        text = fopen ("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.exe", "w+");
        while(text==NULL)
        {
            DoEvents();
        }
        fclose(text);

the OS give me an error because the file is used on another process

This post has been edited by Cambalinho: 20 April 2019 - 03:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how can i test if the file is open from another process?

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,051
  • Joined: 12-December 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 03:49 AM

I am not a c/c++ person but I would have thought you could trap the error.
Was This Post Helpful? 1
  • +
  • -

#3 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 04:04 AM

i know the error, but i don't know avoid it....
what i mean is the Windows give me a message, on MessageBox, that the file is opened...
i just need test if the file is open... when the file is closed, i can execute it.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,051
  • Joined: 12-December 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 05:11 AM

I am not saying to avoid it, but to let it happen and trap it. Investigate error handling.
Was This Post Helpful? 0
  • +
  • -

#5 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 05:21 AM

i'm sorry but see these function:
void run(string FileName, string Parameters="", string ActualFolder="")
{
    SHELLEXECUTEINFO ShExecInfo={0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = "open";
    ShExecInfo.lpFile = FileName.c_str();
    if(Parameters!="")
        ShExecInfo.lpParameters = Parameters.c_str();
    if(ActualFolder!="")
        ShExecInfo.lpDirectory = ActualFolder.c_str();
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;


    ShellExecuteEx(&ShExecInfo);
    if(ShExecInfo.hProcess)
        CloseHandle(ShExecInfo.hProcess);
}

now i call it for compile:
cout << "\ncreating an exe....\n";
        string compilerPath = "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin\\";
        string compiler_exe = "x86_64-w64-mingw32-g++.exe";

        string FileName1 = compilerPath + compiler_exe;
        string parameters = "-Wall -g -std=c++14  -c \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.cpp\" -o \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o\"";
        string actualFolder = compilerPath;
        while( IfFileExist("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.cpp")==false)
        {
            DoEvents();
        }

        run(FileName1, parameters);
        while( IfFileExist("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o")==false)
        {
            DoEvents();
        }
        parameters="-o C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.exe C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o -static-libstdc++ -static-libgcc -fpermissive \"C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\x86_64-w64-mingw32\\lib\\libgdi32.a\"";
        run(FileName1,parameters);

these run() calls the C\C++\GNU compiler for create the exe.
but when i run() the exe:
run("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.exe", "" ,"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug");

the file can be not completed....
so, using the SHELLEXECUTEINFO struture, can i avoid these problem?
i mean execute the program and go to next line, only, when the process is closed
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2339
  • View blog
  • Posts: 4,449
  • Joined: 30-May 10

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 09:06 AM

Cross-posted here -> http://www.cplusplus...general/252528/
Was This Post Helpful? 1
  • +
  • -

#7 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 09:40 AM

Salem_c let me ask friendly: is that a problem? is bad doing that? or it's rude doing these? please tell me
Was This Post Helpful? 1
  • +
  • -

#8 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2339
  • View blog
  • Posts: 4,449
  • Joined: 30-May 10

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 09:53 AM

View PostCambalinho, on 20 April 2019 - 04:40 PM, said:

Salem_c let me ask friendly: is that a problem? is bad doing that? or it's rude doing these? please tell me

http://www.catb.org/...ions.html#forum
and indirectly, inferring http://www.catb.org/...ons.html#urgent

If two people on two forums, unknown to each other, give you the same answer, then one of them just wasted their time.

Indirectly, this means that
- someone else got an answer later than they would otherwise have.
- someone else didn't get an answer at all.

People who help have a finite amount of time that is given freely.
You should be wise to not waste that time.

Sure, if one forum doesn't give you an answer in a couple of days, then move on to pastures new.
But as a courtesy to all, you should include your own cross-references to other places where you have posted.
Was This Post Helpful? 2
  • +
  • -

#9 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 10:15 AM

i see what you mean... thank you so much for answer me... thank you

lets go back to my question... now that i have the solution, i can share.
1 - i avoid the OS message box using the SEE_MASK_FLAG_NO_UI flag on SHELLEXECUTEINFO structure;
2 - now the function returns a number if was success or the error code.

DWORD run(string FileName, string Parameters="", string ActualFolder="")
{
    SHELLEXECUTEINFO ShExecInfo={0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = "open";
    ShExecInfo.lpFile = FileName.c_str();
    if(Parameters!="")
        ShExecInfo.lpParameters = Parameters.c_str();
    if(ActualFolder!="")
        ShExecInfo.lpDirectory = ActualFolder.c_str();
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;

    if (!ShellExecuteEx(&ShExecInfo))
        return GetLastError();

    if(ShExecInfo.hProcess)
        CloseHandle(ShExecInfo.hProcess);

    return ERROR_SUCCESS;
}

now heres how i use it on a while loop:
while(run("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.exe", "" ,"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug")!=ERROR_SUCCESS)
        {
            DoEvents();
        }

the ShellExecuteEx() only executes if the file exists or if isn't used.
heres the DoEvents():
void DoEvents()
{
    MSG msg;
    BOOL result;

    while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
    {
        result = ::GetMessage(&msg, NULL, 0, 0);
        if (result == 0) // WM_QUIT
        {
            PostQuitMessage(msg.wParam);
            break;
        }
        else if (result == -1)
        {
             // Handle errors/exit application, etc.
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
}


thank you so much for all
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6881
  • View blog
  • Posts: 23,344
  • Joined: 05-May 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 05:36 PM

View PostCambalinho, on 20 April 2019 - 07:04 AM, said:

i know the error, but i don't know avoid it....
what i mean is the Windows give me a message, on MessageBox, that the file is opened...
i just need test if the file is open... when the file is closed, i can execute it.

The OS will never pop open a message box when you call fopen() and the file is currently open. I think that you are mistaken about who/what is showing the message box.
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6881
  • View blog
  • Posts: 23,344
  • Joined: 05-May 12

Re: how can i test if the file is open from another process?

Posted 20 April 2019 - 05:48 PM

The main problem is that you are trying to run your executable before the compiler is done compiling. The whole point of the SEE_MASK_NOCLOSEPROCESS is that you so that you can wait for the process to end. From the documentation (again):

Quote

SEE_MASK_NOCLOSEPROCESS (0x00000040)
Use to indicate that the hProcess member receives the process handle. This handle is typically used to allow an application to find out when a process created with ShellExecuteEx terminates. ...


All you seem to do is just to see if the process successfully started. Why not actually wait for the compiler to finish running before trying to run your executable?

As a quick aside, you are running a console program (based on your use of cout). What the hell are you doing pumping messages with DoEvents()?
Was This Post Helpful? 1
  • +
  • -

#12 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

Re: how can i test if the file is open from another process?

Posted 21 April 2019 - 03:26 AM

while( IfFileExist("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o")==false)
        {
            DoEvents();
        }

if i don't use the DoEvents(), the program can freezes. is for the Window continue working normaly.
you mean that i'm mistake and i must use:
"SEE_MASK_NOASYNC (0x00000100)
Wait for the execute operation to complete before returning. This flag should be used by callers that are using ShellExecute forms that might result in an async activation, for example DDE, and create a...."
thank you so much for all.... thank you
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6881
  • View blog
  • Posts: 23,344
  • Joined: 05-May 12

Re: how can i test if the file is open from another process?

Posted 21 April 2019 - 10:00 AM

If you are compiling your application as a console application (e.g. /SUBSYSTEM:CONSOLE), you shouldn't get any complaints from Windows that your program "is not responding". Console applications on Windows have their own message pump provided by the OS. On the other hand, if you are compiling your console application as if it were a Windows application (e.g. /SUBSYSTEM:WINDOWS), then yes, Windows would expect your program to be pumping messages. This is because Windows applications are supposed to be event driven, unlike console applications which are procedural.
Was This Post Helpful? 1
  • +
  • -

#14 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

Re: how can i test if the file is open from another process?

Posted 21 April 2019 - 12:46 PM

thank you so much for correct me.
i thot the message was from OS, but it's from ShellExecuteEx() function, that's why i used the SEE_MASK_FLAG_NO_UI flag.
speaking on flags... i read them all and i thot the SEE_MASK_NOASYNC flag was for wait until process ends for go to next line, but isn't.. i had tested.. what you can tell me more about it?
Was This Post Helpful? 0
  • +
  • -

#15 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 2
  • View blog
  • Posts: 783
  • Joined: 04-March 12

Re: how can i test if the file is open from another process?

Posted 21 April 2019 - 01:12 PM

fixed using the WaitForSingleObject() with process handle:
DWORD run(string FileName, string Parameters="", string ActualFolder="", bool WaitUntilFinish=true)
{
    //Always clean the structure instead random values:
    SHELLEXECUTEINFO ShExecInfo={0};
    
    //The structure size:
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    
    //These flags are for avoid the error messages box and for get the process handle:
    ShExecInfo.fMask = SEE_MASK_FLAG_NO_UI|SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    
    //The program that it's for execute:
    ShExecInfo.lpVerb = "open";
    ShExecInfo.lpFile = FileName.c_str();
    ShExecInfo.nShow = SW_SHOWNORMAL;
    ShExecInfo.hInstApp = NULL;
    
    //Adding parameters:
    if(Parameters!="")
        ShExecInfo.lpParameters = Parameters.c_str();
    
    //Adding the Working directory(best is using the program folder):
    if(ActualFolder!="")
        ShExecInfo.lpDirectory = ActualFolder.c_str();
    
    //Execute the program and if there any error, it will return it:
    if (!ShellExecuteEx(&ShExecInfo))
        return GetLastError();
    
    //Wait Until process ends:
    if(ShExecInfo.hProcess && WaitUntilFinish==true)
        WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
    
    //The Process Handle must be destroyed:
    if(ShExecInfo.hProcess)
        CloseHandle(ShExecInfo.hProcess);
    
    //Executed success(zero):
    return ERROR_SUCCESS;
}


heres how it's used:
//compile the *.cpp file to object file:
        string compilerPath = "C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin\\";
        string compiler_exe = "x86_64-w64-mingw32-g++.exe";

        string FileName1 = compilerPath + compiler_exe;
        string parameters = "-Wall -g -std=c++14  -c \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.cpp\" -o \"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o\"";
        string actualFolder = compilerPath;
        run(FileName1, parameters);

        //creating the exe file using the object file and libraries and dependencies:
        parameters="-o C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.exe C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.o -static-libstdc++ -static-libgcc -fpermissive \"C:\\Program Files\\mingw-w64\\x86_64-7.2.0-win32-seh-rt_v5-rev1\\mingw64\\x86_64-w64-mingw32\\lib\\libgdi32.a\"";
        run(FileName1,parameters);

        //Execute exe file:
        run("C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug\\test.exe", "" ,"C:\\Users\\Cambalinho\\Documents\\CB\\testfolder\\bin\\Debug");


thank you so much for all
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1