?
Solved

SYNCHRONIZE Access

Posted on 2001-07-09
4
Medium Priority
?
611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 800 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.
Suggested Courses

777 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