View previous topic :: View next topic |
Author |
Message |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Sep 30, 2009 10:35 am Post subject: Starting a new process in a minimised window |
|
|
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: 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: 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(). |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8211 Location: Salford, UK
|
Posted: Wed Sep 30, 2009 11:47 am Post subject: |
|
|
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. |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Sep 30, 2009 6:40 pm Post subject: |
|
|
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. Code: | 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 );
}
} |
|
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8211 Location: Salford, UK
|
Posted: Thu Oct 01, 2009 8:17 am Post subject: |
|
|
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. |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Tue Oct 06, 2009 12:34 pm Post subject: |
|
|
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. |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Nov 11, 2009 7:33 am Post subject: |
|
|
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  |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8211 Location: Salford, UK
|
Posted: Wed Nov 11, 2009 9:24 am Post subject: |
|
|
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. |
|
Back to top |
|
 |
jjgermis
Joined: 21 Jun 2006 Posts: 404 Location: N�rnberg, Germany
|
Posted: Wed Nov 11, 2009 9:37 am Post subject: |
|
|
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  |
|
Back to top |
|
 |
|