Solved

SYNCHRONIZE Access

Posted on 2001-07-09
4
604 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

758 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

19 Experts available now in Live!

Get 1:1 Help Now