Silverfrost Forums

Welcome to our forums

Starting a new process in a minimised window

30 Sep 2009 9:35 #5078

For some applications it is required to start an external executable from our Fortran application. In this particular case a new window is opened each time the program is called. This makes it impossible to continue working on the PC. We could not figure out a way to avoid this in FTN95.

I found a very useful C++ subroutine from a colleague which compile and execute wihtout any problems using the SCC compiler. However, when I try to use it (in a mix language project), the executable to be started does not open 😦 The Fortran code: program start_process implicit none integer :: start_process@,i

  C_EXTERNAL STARTAPPNOWINDOW 'StartAppNoWindow'(REF) : INTEGER

  i = StartAppNoWindow('notepad.exe')
  write(*,'(I2)') i 
  i = start_process@('notepad.exe','')
  write(*,'(I2)') i

end program start_process

and the C++ code: #include <windows.h>

extern 'C' int StartAppNoWindow(char* szPathApplication)
{
  /* Strukturen erstellen und initialisieren */
  PROCESS_INFORMATION pi = {0};
  
  
  STARTUPINFOA         si = {0};    //StartupInfo zum Parametrieren des Starts
  si.cb           = sizeof(STARTUPINFO);  //Strukturgroesse definieren
  si.dwFlags      = STARTF_USESHOWWINDOW; //Flag wShowWindow benutzen
  
  si.wShowWindow  = SW_MINIMIZE | SW_HIDE;//Fenster nicht anzeigen, minimiert
  

  /* Prozess starten */
  if (CreateProcessA(NULL,szPathApplication,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
  {
    CloseHandle(pi.hThread); //Thread-Handle wird nicht mehr benoetigt
    WaitForSingleObject(pi.hProcess,INFINITE); //Warten bis Prozess fertig ist!
    CloseHandle(pi.hProcess); //Prozess Handle wird nicht mehr benoetigt
  }
  else
    return -1;
  return 0;
};

Some advise wil be most welcome 😄 The C++ code can be tested by just adding a very simple main().

30 Sep 2009 10:47 #5080

You might find that the Microsoft WinExec solves the problem. It is available in windows.ins I think. It also uses SW_HIDE etc.

I don't know what you mean by 'does not open the Fortran' but your C function may not return until you close Notepad. If this is the problem then the C code needs to be modified to allow the function to return immediately.

30 Sep 2009 5:40 #5085

I think that I should reformulate the problem: The procedure calls the external program, wait for it and continue when done. However, the result of the C++ function is different when called from the Fortran project compared to when the C++ is used as a standalone.

The following C++ program illustrates what should happen (in this example the user must close the notepad application). In the actual application the external program does some calculations (which takes a few minutes). After the calculations are done the Fortran program reads the output file, collect the desired data and continues to call the external program with new parameters. int main() { int i,x; for ( x = 0; x < 10; x++ ){ printf( '%d\n', x ); i = StartAppNoWindow('notepad.exe'); /user needs to close potepad/ /'real' application close auto./ printf( '%d\n', i ); } }

1 Oct 2009 7:17 #5091

I cannot see any significant difference unless it is that you ought to pass the string as an INSTRING rather than a REF.

If furhter work is necessary then I would compile both the C and the Fortran with /DEBUG and see if stepping through the code produces any ideas.

6 Oct 2009 11:34 #5127

I tried several combinations of the code. On my machine the C++ project and the FTN95 project have different results. We could not resolve this issue.

At the moment we (anyway) work the external software supplier to change the code to include new options. However, there are still other applications where we would like this to work. Thus, we will investigate this issue at a later stage.

11 Nov 2009 6:33 #5360

Hi Paul,

as you know, one can use the compiler from the command line or with Plato (my favourite). Anyway, from the command line the messages are displayed in the DOS box. When one uses Plato the messegas are displayed in a small output window in the lower part of the Plato IDE. How is this done?

We have several applications where I would like to get this 'Plato' result. Even if the program is the same many users think that a DOS box is old and outdated stuff. One can say: keep the customer happy and present everthing in a window 😃

11 Nov 2009 8:24 #5362

It may be sufficient for you to put WINAPP at the beginning of your main program.

Plato causes the DOS output to be sent to a results file (which you can probably see in the relevant project folder) and then reads this data into the Output window.

11 Nov 2009 8:37 #5363

Thanks Paul. I always thought that the DOS box output was 'directly' redirectet to the Plato output window. My C++ colleague implement this with some redirection as he calls it, and this is what I tried as well. The file method as Plato does it, seems to be much easier or at least something that I can do as well 😄

Please login to reply.