Solved

SYNCHRONIZE Access

Posted on 2001-07-09
4
605 Views
Last Modified: 2013-12-03
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
Comment
Question by:jdwarren
  • 2
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 200 total points
ID: 6265647
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
 

Author Comment

by:jdwarren
ID: 6266255
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
 
LVL 32

Expert Comment

by:jhance
ID: 6266556
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
 

Author Comment

by:jdwarren
ID: 6269320
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now