replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Execute a program in another directory
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Execute a program in another directory

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: N�rnberg, Germany

PostPosted: Tue Sep 22, 2009 9:53 am    Post subject: Execute a program in another directory Reply with quote

What needs to be done in oder to execute another program in a different directory (other from the directory from which it is called), i.e.
    1) the actual program is started in c:\prog\test.exe
    2) somewhere in test.exe another program c:\users\test2.exe is called
    3) test2.exe will look in c:\prog (current directory) for any files (by default) and not in c:\users

Question: How can I force my current directory (c:\prog) form the calling program to be c:\users for the time test2.exe is called?

When I debug the calling directory is ..\debug\win32 and the program that is being debugged is will look for files in ..\..\. I do hope that my question is clear.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Tue Sep 22, 2009 1:03 pm    Post subject: Reply with quote

From an FTN95 program you can call ATTACH@.
Back to top
View user's profile Send private message AIM Address
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: N�rnberg, Germany

PostPosted: Tue Sep 22, 2009 2:13 pm    Post subject: Reply with quote

This works fine for our application and is sort of a work-around for the following situation:
We automatise software of which we do not have the source code. Without the attach@ subroutine we have to copy our executable in the project directory. Obviuos this becomes undesired with an increase in the number of projects. The code is as follows:
Code:
      write(*,'(1X,"Change to ",A)') trim(PATH) 
      call attach@(PATH,error_code)              !Change to project directory
      call doserr@(error_code)
      if (ik == 1) then
        write(*,'(1X,"Clean directory")')   
        call cissue('delete.bat',i)
      endif
      write(*,'(1X,"Write A_cpff")')     
      call change_mlo_file(Ld_Lq_Up)             !Update the input file
      write(*,'(1X,"Start batch file")')
      call cissue('A_cpff.bat',i)                !Call program
      write(*,'(1X,A)') 'Rename BCH ...'       
      call cissue('ren_BCH.bat',i)
      write(*,'(1X,A)') 'Read BCH ...'
      call read_BCH_file(Ld_Lq_Up)     
      call cissue('delete.bat',i)                !Clean the directory

Each time the software starts the window is maximised and becomes the top window of the desktop. For the time we have no better solution. It would be nice to have a trick to avoid this, i.e. be able to contol the windows.
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Wed Sep 23, 2009 2:29 am    Post subject: Reply with quote

I think you have a number of alternative options available, including:
1) install all .exe and .bat files into a general directory and include this directory in the path.
2) as you are using .bat files, you could include the project directiory path in the .bat files.
3) you could use an environment variable (see dos SET command) to define the project area for each run and use this in the .bat file

One of the problems I have found with the attach@ routine is that on exit from the control program, the default directory reverts to the original directory. I always had problems with that and I'm not sure if the changed directory will be kept when using cissue, start_process@ or start_pprocess@.
Defining a project directory environment variable, which is available in the .bat files may be a useful solution. Again, if you define this in the program by using
cissue ('SET Project_Directory=c:\user\project_3'), (or whatever is the correct syntax !!),
the extent of the availability of this variable definition in subsequent CISSUE calls may still be a problem.
So (in XP) you may need to define the project variable outside your program using Control Panel > System > Advanced > Environment Variables.
I think you need Administrator status to do this, which I typically do not have. I find this a very frustrating problem!

I tried the following program, but it does not work, (or my method of testing does not work), as I think I have the SET syntax wrong.
Code:

! Program is
      integer*4 i
      call cissue('set project_directory=c:\group\project_02',i)
      write (*,*) 'i=',i
      call cissue('set',i)
      call cissue('ss.bat',i)
      call cissue('set',i)
      end

! ss.bat is
set project_directory=test_02
set



FTN95 also use a number of these variables.

Good luck

John
Back to top
View user's profile Send private message
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: N�rnberg, Germany

PostPosted: Wed Sep 23, 2009 11:03 am    Post subject: Reply with quote

We indeed use the environment variable in the batch file (the external program allows this). Copying all the executables in one directory is a possible solution. However if we change the code one needs to copy the this again. In oder to avoid this we have
    1. c:\control_program and
    2. c:\external_program
where we put our control program and external program respectively.

Since the have different versions of the external program it is better to set the environment variable in the batch file rather than in the source code. For our application the attach@ functions works good. The batcg file looks like:
Code:
rem
rem Runs wfemag
rem

set femdir=C:\external_program\2009_08

%femdir%\wfemag A_cpff.MLO
In the given example we use version 2009_08 of the external program. This allow a users to freely specify the version to be used.
I prefer the cissue command rather than start_process@. To be honest I do not understand start_process@ or start_pprocess@ - at least for what we are doing.

I tested your code and can report the same experience than you: It does not work. Unfortunately I cannot explain why it does not work Sad
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Thu Sep 24, 2009 1:46 am    Post subject: Reply with quote

My take on why the attach@ works in your control program, but my use of SET to change environment variables does not transfer between CISSUE is that:-
Attach@ is an initial "environment" setting which is applied to all run environments it initiates via CISSUE, while;
Each CISSUE starts with the environment of the control program, but any changes made in this process are lost when it ends.

This is demonstrated in my program, where within the 3rd CISSUE, SET shows the variable has been changed, while the 4th CISSUE use of SET shows the changes are lost.

I previously tried to write a program GO.exe, where you could specify a directory, and the program would search the disk for a tree name that contained the directory, go there and then exit. The idea was to save on typing the full tree name. You can do it in a batch file, but not using a .exe with an attach@, as the effective CD is lost when the program ends.

I may have this all wrong, so would welcome any corrections to my misconceptions !!

John
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Thu Sep 24, 2009 6:44 am    Post subject: Reply with quote

The underlying issue concerns the way in way the Windows API CreateProcess is called. If you look up the details on MSDN then you will see that it is possible for the new process to inherit the existing environment. Chances are that CISSUE etc do not do this.
Back to top
View user's profile Send private message AIM Address
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Fri Sep 25, 2009 3:23 am    Post subject: Reply with quote

Paul,

I have attempted to change directory and exit the program to the new directory using 2 possible ways and both appear to fail after exiting the program.
Can either of these be modified to achieve my aim ?
I can later use goo.bat as a command, but that's another command.

I use recursive calls to files@ to search the disk for possible values of target_path, which works very well, after the first time it is used.

Code:
!
      write (*,*) 'Attaching to ', trim (target_path)
      call attach@ (target_path, error_code)
      call doserr@ (error_code)
!      call exit (0)                               ! did not work
!
!--- Create a goo.bat file if all else fails
      open  (unit=11, file='c:\tmp\goo.bat')
      write (11,*) 'cd ', trim (target_path)
      write (11,*) 'cd'
      close (unit=11)
!
      call cissue ('c:\tmp\goo.bat',i)    ! also did not work

      end
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Fri Sep 25, 2009 11:14 am    Post subject: Reply with quote

Both CISSUE and START_PROCESS@ end up calling CreateProcess. The only difference is that CISSUE prepends "cmd /c" to the given command. The result is that CISSUE launches a new command shell (presumably a shell within a shell).

Anyway, the problem is that CreateProcess does not pass the so-called "environment block" to the spawned process. Also the current working directory is not and cannot be inherited by the call to CreateProcess.

It looks like we need to be using CreateProcessAsUser or CreateProcessWithLogonW in order to get the spawned process to inherit the current working directory.

Given time, I could probably provide a new Fortran library routine (a variant of START_PROCESS@) that does the trick but not in time for the inpending new release.

In the mean time users could try writing their own function in C. Calling CreateProcessAsUser directly from Fortran is probably not an option.
Back to top
View user's profile Send private message AIM Address
JohnCampbell



Joined: 16 Feb 2006
Posts: 2615
Location: Sydney

PostPosted: Mon Sep 28, 2009 1:26 am    Post subject: Reply with quote

I put my .exe program in a batch file "go.bat" which consists of:
Code:
go_find.exe %1
\tmp\goo

This solves the problem, as batch files can modify the current directory
Back to top
View user's profile Send private message
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: N�rnberg, Germany

PostPosted: Mon Sep 28, 2009 7:11 am    Post subject: Reply with quote

Hi John,

we do it in a similar way. The batch file that we call from our executable has the following format:
Code:
set femdir=C:\Programme\femag\2009_08

%femdir%\wfemag %1
If we know the log file which the (external) program should run on start the %1 is replaced with the name of the logfile, i.e. %femdir%\wfemag logfile.MLO (for example).
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Mon Sep 28, 2009 1:33 pm    Post subject: Reply with quote

I am not clear as to whether this issue has now been resolved but I need to correct an error in my previous post.

CreateProcess has an argument for the current working directory. If this argument is NULL (as it is within CISSUE and START_PROCESS@) then the new process inherits the cwd of the calling process.
Back to top
View user's profile Send private message AIM Address
jjgermis



Joined: 21 Jun 2006
Posts: 404
Location: N�rnberg, Germany

PostPosted: Tue Sep 29, 2009 8:57 am    Post subject: Reply with quote

Paul thanks for your last comment. For us this issue is resolved and we can continue our project as planned (the FTN95 attach@ function works fine for us). From the last post from John it seems like he has a solution as well.

The START_PROCESS@ and CISSUE functions is clear as well.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group