<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Approximatrix Forums — Executing an external application from SF application]]></title>
	<link rel="self" href="http://forums.approximatrix.com/extern.php?action=feed&amp;tid=597&amp;type=atom" />
	<updated>2017-01-13T17:55:19Z</updated>
	<generator>PunBB</generator>
	<id>http://forums.approximatrix.com/viewtopic.php?id=597</id>
		<entry>
			<title type="html"><![CDATA[Re: Executing an external application from SF application]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=2721#p2721" />
			<content type="html"><![CDATA[<p>Frank,</p><p>Glad to hear it&#039;s working!&nbsp; Let me know if there are any improvements you might need.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2017-01-13T17:55:19Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=2721#p2721</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Executing an external application from SF application]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=2720#p2720" />
			<content type="html"><![CDATA[<p>Jeff,</p><p>I should know by now that you are always correct.&nbsp; I did, in fact, place the C file in with the the fortran module when I tried to compile it.&nbsp; I&#039;ve since taken your advice and placed the C and F codes in separate files and it compiled just fine - no errors.</p><p>Fantastic little routine for seamlessly executing *exe files like notepad, etc.</p><p>Thank you again for sharing this in the SF forum.</p><p>Regards,</p><p>Frank</p>]]></content>
			<author>
				<name><![CDATA[drfrank]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=223</uri>
			</author>
			<updated>2017-01-10T21:21:14Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=2720#p2720</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Executing an external application from SF application]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=2719#p2719" />
			<content type="html"><![CDATA[<p>Frank,</p><p>Are you putting the C code in a Fortran file?&nbsp; I had meant for the C code to be in a separate file, <strong>launch.c</strong>.&nbsp; You don&#039;t need to enable the C preprocessor either; that option only pertains to C preprocessor directives (<em>#include</em>, <em>#ifdef</em>, etc.) being utilized in Fortran files.&nbsp; The C code must be in a C file alone in order for Simply Fortran to launch the C compiler.</p><p>Sorry for the confusion!</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2017-01-10T20:52:51Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=2719#p2719</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Executing an external application from SF application]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=2718#p2718" />
			<content type="html"><![CDATA[<p>Jeff,</p><p>Very interesting approach, but I&#039;m having some difficulty in compiling your example, Helloworld.</p><p>I&#039;ve placed your entire example code, C and Fortran, into SF, enabled the C Preprocessor in the Project Options menu, and then tried to compile it all together, but received pages of error code.&nbsp; I&#039;m using Windows 7, 64-bit.</p><p>Example code:<br />!-------------------------------------------------------------------------------<br />#include &lt;windows.h&gt;<br />int LaunchExecutable(char *exe, int wait)<br />{<br />.<br />.<br />}<br />!-------------------------------------------------------------------------------<br />module launcher<br />implicit none<br />private<br />public::LaunchWindow<br />contains<br />&nbsp; &nbsp; subroutine LaunchWindow(exe, wait, status)<br />&nbsp; &nbsp; use iso_c_binding<br />.<br />.<br />&nbsp; &nbsp; end subroutine LaunchWindow&nbsp; &nbsp; <br />end module launcher<br />!-------------------------------------------------------------------------------<br />program helloworld<br />use launcher<br />implicit none<br />&nbsp; &nbsp; integer::i<br />&nbsp; &nbsp; CALL LaunchWindow(&quot;C:\Windows\System32\notepad.exe&quot;, status=i, wait=.FALSE.)<br />end program helloworld<br />!-------------------------------------------------------------------------------</p><p>Here are some of the error messages received when I tried to compile it.&nbsp; I couldn&#039;t copy all of the messages as they were pages long.&nbsp; I also tried compiling with the SF options of 32-bit and 64-bit, with and without enabling the C Preprocessor, but to no avail.</p><p>==============================================================================<br />Generating Makefile... Okay<br />==============================================================================<br />Compiling .\Helloworld.f90<br />- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br />c:\program files (x86)\simply fortran 2\mingw-w64\x86_64-w64-mingw32\include\_mingw.h:268:2:</p><p> #error Only Win32 target is supported!<br />&nbsp; 1~~~~<br />Error: #error Only Win32 target is supported!<br />- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br />c:\program files (x86)\simply fortran 2\mingw-w64\x86_64-w64-mingw32\include\sdkddkver.h:182:0:</p><p> #if ((OSVER(NTDDI_VERSION) == NTDDI_WIN2K) &amp;&amp; (_WIN32_WINNT != _WIN32_WINNT_WIN2K)) || \</p><p>Error: token &quot;##&quot; is not valid in preprocessor expressions<br />- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br />c:\program files (x86)\simply fortran 2\mingw-w64\x86_64-w64-mingw32\include\_mingw_stdarg.h:11:2:</p><p> #error Only Win32 target is supported!<br />&nbsp; 1~~~~<br />Error: #error Only Win32 target is supported!<br />- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br />c:\program files (x86)\simply fortran 2\mingw-w64\x86_64-w64-mingw32\include\winnt.h:376:2:</p><p> #error Must define a target architecture.<br />&nbsp; 1~~~~<br />Error: #error Must define a target architecture.<br />.<br />.<br />.<br />.</p><p>Do you know what I may be doing incorrectly?</p><p>Frank</p>]]></content>
			<author>
				<name><![CDATA[drfrank]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=223</uri>
			</author>
			<updated>2017-01-10T01:27:08Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=2718#p2718</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Executing an external application from SF application]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=2717#p2717" />
			<content type="html"><![CDATA[<p>Frank,</p><p>You are doing what you should do by using <em>execute_command_line</em>, but it will launch a console every time it is called if your program doesn&#039;t already have a console window.&nbsp; There&#039;s no pure-Fortran solution for this issue on Windows, but it&#039;s relatively simple to get around using a simple C file added to your project.</p><p>I&#039;ve written a short C routine, <em>LaunchExecutable</em>, that uses the Windows API to launch an arbitrary program.&nbsp; The C routine is in this file:</p><p>launch.c:<br /></p><div class="codebox"><pre><code>#include &lt;windows.h&gt;

int LaunchExecutable(char *exe, int wait)
{
SECURITY_ATTRIBUTES secattr;
PROCESS_INFORMATION pi;
STARTUPINFO si;
BOOL bSuccess;

    /* Open up some pipes */
    ZeroMemory(&amp;secattr, sizeof(SECURITY_ATTRIBUTES));
    secattr.nLength = sizeof(SECURITY_ATTRIBUTES);
    secattr.lpSecurityDescriptor = NULL;
    secattr.bInheritHandle = TRUE;

    ZeroMemory(&amp;si, sizeof(STARTUPINFO));
    
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOWDEFAULT;
    
    bSuccess = CreateProcess(NULL,       /* lpApplicationName */
                             exe,        /* lpCommandLine */
                             NULL,       /* lpProcessAttributes */
                             NULL,       /* lpThreadAttributes */
                             TRUE,       /* bInheritHandles */
                             0,          /* dwCreationFlags */
                             NULL,       /* lpEnvironment */
                             NULL,       /* lpCurrentDirectory */
                             &amp;si,        /* lpStartupInfo */
                             &amp;pi);       /* lpProcessInformation */

    if(!bSuccess)
        return GetLastError();
        
    if(wait)
        WaitForSingleObject(pi.hProcess, INFINITE);
    
    return ERROR_SUCCESS;
}</code></pre></div><p>The above routine, though, needs a Fortran wrapper to work properly.&nbsp; My Fortran module, <em>launcher</em>, contains one routine, <em>LaunchWindow</em>, that implements the necessary Fortran wrapper:</p><p>launchf.f90:<br /></p><div class="codebox"><pre><code>module launcher
implicit none

private

public::LaunchWindow

contains

    subroutine LaunchWindow(exe, wait, status)
    use iso_c_binding
    implicit none
    
    character(*)::exe
    logical, optional, intent(in)::wait
    integer, optional, intent(out)::status
    
    integer::internal_wait
    integer::internal_ret
    
    ! For conversion to a C character array
    integer::i
    character(kind=c_char), dimension(:), allocatable::internal_exe
    
    interface
        function LaunchExecutable(exe, wait) bind(c, name=&quot;LaunchExecutable&quot;)
        use iso_c_binding
        integer(kind=c_int)::LaunchExecutable
        character(kind=c_char)::exe
        integer(kind=c_int), value::wait
        end function LaunchExecutable
    end interface
    
    internal_wait = 0
    if(present(wait)) then
        if(wait) then
            internal_wait = 1
        else
            internal_wait = 0
        end if
    end if
    
    allocate(internal_exe(len_trim(exe)+1))
    internal_exe = C_NULL_CHAR
    do i=1,len_trim(exe)
        internal_exe(i) = exe(i:i)
    end do
    
    internal_ret = LaunchExecutable(internal_exe(1), internal_wait)
    
    if(present(status)) then
        status = internal_ret
    end if
    
    deallocate(internal_exe)
    
    end subroutine LaunchWindow
    
end module launcher</code></pre></div><p>This routine takes a string with the <strong>full path</strong> to the executable.&nbsp; If the executable is located within the current working directory, you probably wouldn&#039;t need the full path.&nbsp; It has two optional arguments, <em>wait</em> which is a logical that reflects whether the routine should wait until the called process completes before returning, and <em>status</em> which returns any <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx">error codes</a> that may have been returned by the call (0 means success).</p><p>As a simple example, here&#039;s a Fortran program that actually calls the routine:</p><p>hello.f90:<br /></p><div class="codebox"><pre><code>program helloworld
use launcher
implicit none

    integer::i
    
    CALL LaunchWindow(&quot;C:\Windows\System32\notepad.exe&quot;, status=i, wait=.FALSE.)
    
end program helloworld</code></pre></div><p>The above launches Notepad and exits immediately without ever opening a console window.</p><p>Let me know if the above works for you.&nbsp; I&#039;ll be happy to update any code based on any requests.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2017-01-09T13:23:59Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=2717#p2717</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Executing an external application from SF application]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=2716#p2716" />
			<content type="html"><![CDATA[<p>Hi Jeff,</p><p>I would like to run an external application (e.g., notepad.exe) from a compiled SF application, but without the continued display of the cmd.exe console after the external application is loaded.&nbsp; I&#039;ve tried the following to run an external application from my SF program successfully.</p><p>call execute_command_line( &quot;Dataview.exe&quot;, wait=.false. )</p><p>Is there a way to close the cmd.exe console which is displayed concurrently with my application, but without closing my application or the externally run one?&nbsp; I can close the command console manually just fine, but I&#039;d like to automatically close it from my program.</p><p>Basically, I would like to load a text editor from my SF program without the persistent and concurrent appearance of the command console.</p><p>Any suggestions for how to close the command console once an external program is loaded?</p><p>Frank</p>]]></content>
			<author>
				<name><![CDATA[drfrank]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=223</uri>
			</author>
			<updated>2017-01-06T21:14:13Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=2716#p2716</id>
		</entry>
</feed>
