Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Starting external program?
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?

Report this thread to moderator Post Follow-up to this message
Old Post
DrLecter
04-14-05 08:58 PM


Re: Starting external program?
DrLecter 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...

Report this thread to moderator Post Follow-up to this message
Old Post
Duane Bozarth
04-14-05 08:58 PM


Re: Starting external program?
"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.





Report this thread to moderator Post Follow-up to this message
Old Post
Gerald F. Thomas
04-14-05 08:58 PM


Re: Starting external program?
Uh this is hardcore. ;)
Thanks, I will try it out.
BTW Compiler is Intel 8.1

Report this thread to moderator Post Follow-up to this message
Old Post
DrLecter
04-15-05 01:57 PM


Re: Starting external program?
DrLecter 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)

Report this thread to moderator Post Follow-up to this message
Old Post
Walter Spector
04-17-05 01:57 AM


Re: Starting external program?
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 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

Report this thread to moderator Post Follow-up to this message
Old Post
Walter Spector
04-17-05 08:58 PM


Re: Starting external program?
"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.



Report this thread to moderator Post Follow-up to this message
Old Post
Gerald F. Thomas
04-17-05 08:58 PM


Re: Starting external program?
"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)

Report this thread to moderator Post Follow-up to this message
Old Post
Walter Spector
04-17-05 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:58 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.