Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Calling Wordpad to open a file with a space in the name

Neels
New Contributor II
500 Views

I have been using the following to open Wordpad:

            iRet = WinExec('C:\Program Files\Windows NT\Accessories\wordpad.exe '//Trim(File)//'.txt'//CHAR(0),SW_ShowNormal)

However, if the file name has a space in it it does not work, stopping the file name at the space. Is there a workaround?

0 Kudos
11 Replies
jimdempseyatthecove
Honored Contributor III
500 Views

Enclose file name with other quote marks. In the above case, use the double-didt quote (") inside the single-didt quote(')

iRet = WinExec('"C:\Program Files\Windows NT\Accessories\wordpad.exe" "'//Trim(File)//'.txt"'//CHAR(0),SW_ShowNormal)

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
500 Views

I would think that ShellExecute is a better tool for this, especially if you're hardcoding the path to Wordpad.

WinExec? You could at least use the Fortran standard EXECUTE_COMMAND_LINE.

0 Kudos
Neels
New Contributor II
500 Views

Thanks Jim, that works fine.

Steve, where do I find the documentation for ShellExecute, I downloaded the pdf from FAQ as you posted but search does not find it or for that matter WinExec?

0 Kudos
JVanB
Valued Contributor II
500 Views

The documentation is found on MSDN, just look it up with google (actually bing works pretty well for MSDN stuff). For ShellExecute, the MSDN docs says it's in Shell32.dll, so to see what interfaces ifort has made up for you, look in shell32.f90 in the directory that has all the Win32 API interfaces. WinExec is in Kernel32.dll, so the Fortran interface is to be found in kernel32.f90.

 

0 Kudos
Neels
New Contributor II
500 Views

Thanks Steve and RO,

I now have the following working:

            
            hwnd = GETHWNDQQ(QWIN$FRAMEWINDOW)

            iRet = ShellExecute (&
                   hwnd = hwnd,                            &  !!2
                   lpOperation = "open"C,                  &  !!3
                   lpFile = Trim(FileName)//CHAR(0),       &  !!4
                   lpParameters = NULL_CHARACTER,          &  !!5
                   lpDirectory = NULL_CHARACTER,           &  !!6
                   nShowCmd = SW_SHOWNORMAL)                  !!7

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
500 Views

Neels, et al

An additional option that can be used is to launch the program using the command line program START (start, Start, ...).

Issue: help start
from a command line

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
500 Views

The nice thing about ShellExecute is that it will open whatever application is associated with the file type. I wrote more about it here.

0 Kudos
Neels
New Contributor II
500 Views

Thanks Steve, I noticed that. You will see the code I used comes directly from the page you linked.

I first tried it in Release mode (my bad) and everything worked, however in Debug mode it complained about the "NULL_CHARACTER" so I changed it to the following:

            hwnd = GETHWNDQQ(QWIN$FRAMEWINDOW)

            iRet = ShellExecute (&
                   hwnd = hwnd,                            &  !!2
                   lpOperation = "open"C,                  &  !!3
                   lpFile = Trim(FileName)//CHAR(0),       &  !!4
                   lpParameters = "",                      &  !!5
                   lpDirectory = "",                       &  !!6
                   nShowCmd = SW_SHOWNORMAL)                  !!7
            

 

0 Kudos
JVanB
Valued Contributor II
500 Views

Looking in ifwinty.f90, NULL_CHARACTER is defined by:

character, pointer :: NULL_CHARACTER

Is ifwinty.f90 compiled with some switch that results in NULL_CHARACTER's pointer association status being defined at this point? In ifort's implementation

call C_F_POINTER(C_NULL_PTR,NULL_CHARACTER)

is the same as

NULL_CHARACTER => NULL()

so why isn't NULL_CHARACTER and its brethren initialized in ifwinty.f90?

Looking at the MSDN documentation for ShellExecute, it seems that you should be passing NULL for both lpParameters and lpDirectory (provided you aren't running in 32-bit mode with the -i8 switch active). In the code above, the strings passed aren't NUL-terminated, so anything could happen. Also you are copying an old example; normally

"open" // ACHAR(0)

would be preferable to

"open"c

0 Kudos
Neels
New Contributor II
500 Views

RO, I have not recompiled ifwinty or any of its brethren.

I implemented your comment about ACHAR(0)

Maybe Steve will update his example of ShellExecute?

0 Kudos
Steven_L_Intel1
Employee
500 Views

Gah! Those NULL_ constants had originally been initialized to NULL(). Not sure what happened here. I think the proper action here is to redeclare these as simply being NULL (the constant zero)( - it should work in most if not all cases. I'm in the middle of updating the API declarations so I will include this. And yes, I'll update the example.

0 Kudos
Reply