Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, is it possible to run a native executable by the way of a Fortran program? Consider a Fortran program trying to open notepad.exe or something like that. Is it possible, if so, how?
Post Follow-up to this messageDrLecter wrote: > > Hi, > is it possible to run a native executable by the way of a Fortran > program? Consider a Fortran program trying to open notepad.exe or > something like that. Is it possible, if so, how? Check your compiler documentation...many (most, nearly, all?) current compilers will have an extension that allows it. FSYSTEM() or something similar, perhaps...
Post Follow-up to this message"DrLecter" <DrLecter.gulli@gmail.com> wrote in message news:b3a0ffa3.0504140525.1c06f1e1@posting.google.com... > Hi, > is it possible to run a native executable by the way of a Fortran > program? Consider a Fortran program trying to open notepad.exe or > something like that. Is it possible, if so, how? This is simple on Windows which I gather is your OS of choice. Here's how using C(I)VF: use kernel32 use dfwinty implicit none character, pointer :: AppName type(T_STARTUPINFO) StartInfo type(T_PROCESS_INFORMATION) ProcessInfo logical(4) ret StartInfo%wShowWindow = 1 !SW_NORMAL StartInfo%cb = 68 !bytes NULLIFY(AppName) ret = CreateProcess(AppName, 'Notepad.exe "Filename.txt" ', NULL_SECURITY_ATTRIBUTES, NULL_SECURITY_ATTRIBUTES, .FALSE., CREATE_SEPARATE_WOW_VDM, NULL, AppName, StartInfo, ProcessInfo) Note the following: 1. You don't have to specify an argument (Filename.txt) for Notepad.exe. 2. ShellExecute/Ex is always less powerful than CreateProcess. Also, ShellExecute/Ex used to be vulnerable to a 'buffer overrun' hack although you-know-who might have fixed it by now they having much experience in this department. 3. CreateProcess, in contrast to ShellExecute/Ex, ignores the dictates of user policy restrictions. If these prohibit an app from starting other apps (via ShellExecute/Ex) then the spawning app is SOL. -- HTH, Gerry T.
Post Follow-up to this messageUh this is hardcore. ;) Thanks, I will try it out. BTW Compiler is Intel 8.1
Post Follow-up to this messageDrLecter wrote: > ... > BTW Compiler is Intel 8.1 Since you are using the Intel compiler, you can use the POSIX.9 "PXF" calls - in particular, PXFEXECV: use ifposix : character(*), parameter :: notepad_path = 'notepad.exe' integer, parameter :: nargs = 2 character(20), parameter :: argv(nargs) integer :: argv_l(nargs) data argv/ 'notepad', 'myfile.txt'/ : argv_l = len_trim (argv) call PXFEXECV (notepad_path, len (notepad_path), argv, argv_l, nargs, pxferr ) Note that the above will overwrite your current process with the one you specify (notepad, in the above.) If you want the current process to keep running, you must 'fork' off a seperate process to do the 'execv' - using PXFFORK: call PXFFORK (ipid, pxferr) if (ipid == 0) then ! child - so do the execv here call PXFEXECV (....) else ! parent - can call PXFWAIT here, or do something else end if Walt -...- Walt Spector (w6ws at earthlink dot net)
Post Follow-up to this messageHere is a complete example of calling notepad using the PXF library
routines (which are supplied with the Intel compiler.)
Hope it helps.
Walt
-...-
Walt Spector
(w6ws at earthlink dot net)
----------------------------------------------------------------------------
----------
program call_notepad
! use ifposix ! Useful when using Intel compiler
! use pxf_definitions ! Useful with some other PXF implementations
implicit none
character(64) :: path
integer :: path_l
! data path /'/cygdrive/c/WINNT/system32/notepad.exe'/ ! cygwin alt
ernative
data path /'C:/WINNT/system32/notepad.exe'/
integer :: nargs
parameter (nargs=2)
character(32) :: argv(nargs)
data argv /'notepad', 'myfile.txt'/
integer :: argv_l(nargs)
integer :: i
integer :: ipid, iretpid, pxferr
integer :: istat
path_l = len_trim (path)
do, i=1, nargs
argv_l(i) = len_trim (argv(i))
end do
CALL PXFFORK (ipid, pxferr)
if (pxferr /= 0) call fatal ('fork', pxferr)
if (ipid == 0) then
print *,'CHILD: calling: ', path(:path_l)
CALL PXFEXECV (path, path_l, argv, argv_l, nargs, pxferr)
call fatal ('exec (child)', pxferr)
else
print *,'PARENT: waiting for child to complete'
CALL PXFWAIT (istat, iretpid, pxferr)
print *,'PARENT: child finished.'
endif
end program
Post Follow-up to this message
"Walter Spector" <w6ws_xthisoutx@earthlink.net> wrote in message
news:42627B77.7DC37FEA@earthlink.net...
> Here is a complete example of calling notepad using the PXF library
> routines (which are supplied with the Intel compiler.)
>
> Hope it helps.
>
> Walt
> -...-
> Walt Spector
> (w6ws at earthlink dot net)
>
> -------------------------------------------------------------------------
-------------
>
> program call_notepad
> ! use ifposix ! Useful when using Intel compiler
> ! use pxf_definitions ! Useful with some other PXF
implementations
> implicit none
>
> character(64) :: path
> integer :: path_l
> ! data path /'/cygdrive/c/WINNT/system32/notepad.exe'/ ! cygwin
alternative
> data path /'C:/WINNT/system32/notepad.exe'/
>
> integer :: nargs
> parameter (nargs=2)
> character(32) :: argv(nargs)
> data argv /'notepad', 'myfile.txt'/
> integer :: argv_l(nargs)
>
> integer :: i
> integer :: ipid, iretpid, pxferr
> integer :: istat
>
> path_l = len_trim (path)
> do, i=1, nargs
> argv_l(i) = len_trim (argv(i))
> end do
>
> CALL PXFFORK (ipid, pxferr)
> if (pxferr /= 0) call fatal ('fork', pxferr)
>
> if (ipid == 0) then
> print *,'CHILD: calling: ', path(:path_l)
> CALL PXFEXECV (path, path_l, argv, argv_l, nargs, pxferr)
> call fatal ('exec (child)', pxferr)
> else
> print *,'PARENT: waiting for child to complete'
> CALL PXFWAIT (istat, iretpid, pxferr)
>
> print *,'PARENT: child finished.'
> endif
>
> end program
Nice sample. Note, however, IVF doesn't support PXFFORK, fatal, and
PXFWAIT, but PXFEXECV will create the process.
--
Ciao,
Gerry T.
Post Follow-up to this message"Gerald F. Thomas" wrote: > ... > Nice sample. Note, however, IVF doesn't support PXFFORK, fatal, and > PXFWAIT, but PXFEXECV will create the process. Oops - you are correct. The example was tested using g77, cygwin, and my own version of PXF. My experience with Intels compilers are on Linux-based systems - which do support PXFFORK and PXFWAIT. I also should have included a version of 'fatal', as it is one of mine: subroutine fatal (c, e) implicit none character(*) :: c integer :: e print *, 'fatal: ', c, ', errno =', e stop end subroutine Walt -...- Walt Spector (w6ws at earthlink dot net)
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.