Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Trying to start a process with CreateProcess()

Geoff_Hall
Beginner
1,934 Views
Hi,

I can see on the wires that lots of people have problems with this but I've run out of ideas for what to try next.

I have isolated the task into a little standalone program:
------------------------------------
program Main
!
! TestProc - Test calling CreateProcess() in a standalone environment.
!

use ifwin

implicit none

character(100) :: CmdLine ! Work area for the command line
character(1), target :: NC ! Yr actual null character

integer :: iCRC, iRet

type (T_StartupInfo) :: StartInfo ! CreatProcess parms
type (T_Process_Information) :: ProcInfo ! CreatProcess parms (created process info)

type (T_Security_Attributes), target :: SecurAttrib !!Yr actual security attributes

! Initialise

NC = char(0)

StartInfo%cb = 68 ! # bytes in type struct: 16xint(4) + 2xint(2)
StartInfo%lpReserved = 0
StartInfo%lpDesktop = NULL
StartInfo%lpTitle = NULL
StartInfo%dwX = 0
StartInfo%dwY = 0
StartInfo%dwXSize = 0
StartInfo%dwYSize = 0
StartInfo%dwXCountChars = 0
StartInfo%dwYCountChars = 0
StartInfo%dwFillAttribute = 0
StartInfo%dwFlags = StartF_UseShowWindow
StartInfo%wShowWindow = SW_HIDE ! int(2)
StartInfo%cbReserved2 = 0 ! int(2)
StartInfo%lpReserved2 = NULL

SecurAttrib%nLength = 0
SecurAttrib%lpSecurityDescriptor = 0
SecurAttrib%bInheritHandle = 0

Null_Character => NC
Null_Security_Attributes => SecurAttrib

! Prepare the command line string and arguments

cmdLine = 'C:\\Devel\\AMS\\test_MainlyBat\\hello.exe' // char(0)

! Initiate process

iCRC = CreateProcess( &
Null_Character, & ! pointer to name of executable module
cmdLine, & ! pointer to command line string
Null_Security_Attributes, & ! process security attributes
Null_Security_Attributes, & ! thread security attributes
.false., & ! handle inheritance flag
Null, & ! creation flags
Null, & ! pointer to new environment block
Null_Character, & ! pointer to current directory name
StartInfo, & ! pointer to STARTUPINFO
ProcInfo) ! pointer to PROCESS_INFORMATION

! Check return code from CreateProcess

if (iCRC .eq. 0) then !Nonzero=.true. => success (i.e. the process id)
iRet = GetLastError ()
print *, 'Command [',trim(cmdLine),']'
print *, 'has CreateProcess error ', iRet
else
print *, 'Command [',trim(cmdLine),'] worked !!'
end if

end
------------------------------------
in the hope that someone who has got this running successfully can point me in the right direction.

The reason I want to do this, is eventually I want to trap the output of the child process, but right now, I don't get off the ground. The error returned is 123 which means "The filename, directory name, or volume label syntax is incorrect."

TIA

Cheers, Geoff
0 Kudos
2 Replies
Paul_Curtis
Valued Contributor I
1,934 Views
[bash]SUBROUTINE Launch (progname, classname) IMPLICIT NONE CHARACTER(LEN=*), INTENT(IN) :: progname, classname INTEGER :: rval TYPE(T_STARTUPINFO) :: si TYPE(T_PROCESS_INFORMATION) :: pi ! TYPE T_PROCESS_INFORMATION ! SEQUENCE ! integer(HANDLE) hProcess ! knowns HANDLE ! integer(HANDLE) hThread ! knowns HANDLE ! integer(DWORD) dwProcessId ! knowns DWORD ! integer(DWORD) dwThreadId ! knowns DWORD ! END TYPE CALL ZeroMemory (LOC(pi), SIZEOF(pi)) CALL ZeroMemory (LOC(si), SIZEOF(si)) si%cb = SIZEOF(si) si%dwFlags = STARTF_USESHOWWINDOW si%wShowWindow = SW_SHOWNORMAL IF (CreateProcess (NULL, & ! process name progname, & ! command line NULL_SECURITY_ATTRIBUTES, & ! security attributes NULL_SECURITY_ATTRIBUTES, & ! thread attributes FALSE, & ! handle inheritance 0, & ! creation flags NULL, & ! environment block NULL, & ! initial working path si, & ! startup info pi) ) THEN ! process info ! let the process load itself rval = WaitForInputIdle (pi%hProcess, 2000) rval = CloseHandle (pi%hProcess) rval = CloseHandle (pi%hThread)
END IF

END SUBROUTINE Launch [/bash]
0 Kudos
Geoff_Hall
Beginner
1,934 Views

Had to add:

USE ifwin

Then:

TYPE(T_SECURITY_ATTRIBUTES) :: sa

...

sa%NLENGTH = 0

sa%LPSECURITYDESCRIPTOR = 0 !Possibly unnecessary

sa%BINHERITHANDLE = 0 !Possibly unnecessary

but then it worked. Thanks very much. Very appreciated.

Ive included my changes here in case someone else finds them useful :)

Cheers, Geoff

0 Kudos
Reply