Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I

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

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
Highlighted

Enclose file name with other

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
Highlighted

I would think that

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.

Retired 12/31/2016
0 Kudos
Highlighted
New Contributor I

Thanks Jim, that works fine.

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
Highlighted
Valued Contributor II

The documentation is found on

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
Highlighted
New Contributor I

Thanks Steve and RO,

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
Highlighted

Neels, et al

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
Highlighted

The nice thing about

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

Retired 12/31/2016
0 Kudos
Highlighted
New Contributor I

Thanks Steve, I noticed that.

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
Highlighted
Valued Contributor II

Looking in ifwinty.f90, NULL

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
Highlighted
New Contributor I

RO, I have not recompiled

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
Highlighted

Gah! Those NULL_ constants

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.

Retired 12/31/2016
0 Kudos