• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

SYNCHRONIZE Access

I have an executable created in Visual Studio as a FORTRAN console application. I am starting this application from within another application with the CreateProcess API. I would like to then use the WaitForSingleObject API to detect when it ends. To do this it appears that Windows NT requires at least 'SYNCHRONIZE' access (see SDK notes on the WaitForSingleObject API). Any thoughts on what can be done in Fortran source code at compile time to insure that I can have 'SYNCHRONIZE' access?
I think the solution to this is may be setting the access privileges (by using other API calls) within the FORTRAN source.
An API source example in C++ or any other language would be OK.
0
jdwarren
Asked:
jdwarren
  • 2
  • 2
1 Solution
 
jhanceCommented:
There is nothing that needs to be changed in your FORTRAN app.  Since you are starting from within your app with CreateProcess, you already have all the rights you need.  All you need to use WaitForSingleObject is the handle to the new process.  I'd suggest a function like this one:

BOOL DoProcess(const TCHAR *lpszCommand, const TCHAR *lpszDirectory, BOOL bWait)
{
     STARTUPINFO si;
     memset(&si, 0, sizeof(STARTUPINFO));

     si.cb = sizeof(STARTUPINFO);
     si.lpReserved = NULL;
     si.lpDesktop = _T("WinSta0\\Default");
     si.lpTitle = NULL;

     PROCESS_INFORMATION pi;

     if(CreateProcess(
          NULL,
          (TCHAR *)lpszCommand,
          NULL,
          NULL,
          FALSE,
          0,
          NULL,
          (_tcslen(lpszDirectory) == 0 ? NULL : lpszDirectory),
          &si,
          &pi
          )){

          // Close the hThread handle, not needed here
          CloseHandle(pi.hThread);

          if(bWait){
               WaitForSingleObject(pi.hProcess, INFINITE);
          }

          // The process is either done or we don't care about it finishing
          CloseHandle(pi.hProcess);

          return TRUE;
     }
     
     return FALSE;
}
0
 
jdwarrenAuthor Commented:
jhance,
I had actually used the same arguments to CreateProcess that you had suggested and it looked like a good value was returned to pi.hProcess.
Your comment that nothing needs to be done in FORTRAN seems valid so the points go to you as I can reproduce good results when using another type compiler.
It will remain a mystery to me as to why the WaitForSingleObject fails when Visual Basic is the compiler. Other VB attempts using Shell and then OpenProcess also fail at OpenProcess with a GetLastError of ERROR_ACCESS_DENIED. This is what lead me to believe that I did not have 'SYNCHRONIZE' access.
0
 
jhanceCommented:
Three questions:

1) Where did Visual Basic come from in this situation?  You didn't even mention it in passing before.

2) Why did you accept this if it didn't solve your problem?

3) Where did we start talking about OpenProcess()??  You asked about CreateProcess and WaitForSingleObject.  Neither of which have anything to do with OpenProcess().  You are correct that OpenProcess needs privilege to succeed but that has nothing to do with the process you are opening and everything to do with the privilege of the process calling OpenProcess.


I guess it's no wonder that my answer didn't match up with your "real" question since you seem to have asked about one thing when there was really a different question involved...
0
 
jdwarrenAuthor Commented:
jhance,
Your informing me that nothing needed to be done in the Fortran Application was a reasonable answer.

I neglected to give details what compilers and other failed attempts occurred because I erroneously drew a conclusion that I needed to set something in the Fortran source code to make it work. Once you indicated that it was not necessary, I then proved it to myself by using a different type compiler.

My follow-up comment only was put there to indicate that I had actually tried more than one approach in VB to get it to work but everything had failed.

For the purpose of addressing this now as a new VB question, I would be willing to add another question to experts-exchange if you think you have a VB solution to the problem. Right now I have something working but it would be nice to know how to do it in VB since I spent so much time there in the first place.

Would you like to answer another (more specific) Widows API question relating to VB?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now