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

Activating a program from an associated file

michael_green
Beginner
455 Views
Hi All,

I would like to be able to activate a Win32 program by double clicking on an associated file in Windows Explorer. I don't want to disturb the program's normal menu driven operation, but if a user decides to double click on one of its normal data files I would like the program to open in its graphical mode and deal with the file, etc, etc. I presume this would be achieved through some operation using the WinMain lpCmdLine argument but I am unable to make much progress with the online documentation. Can this be done?

Many thanks for any help or suggestions.

Mike
0 Kudos
4 Replies
DavidWhite
Valued Contributor II
455 Views
Quoting - michaelgreen
Hi All,

I would like to be able to activate a Win32 program by double clicking on an associated file in Windows Explorer. I don't want to disturb the program's normal menu driven operation, but if a user decides to double click on one of its normal data files I would like the program to open in its graphical mode and deal with the file, etc, etc. I presume this would be achieved through some operation using the WinMain lpCmdLine argument but I am unable to make much progress with the online documentation. Can this be done?

Many thanks for any help or suggestions.

Mike
Mike,

When you do this does the filename get appended to the program name as an argument? If so, you should be able to check the number of arguments and take action accordingly. I don't have IVF on this machine, so I can't look up the commandline functions.

David
0 Kudos
anthonyrichards
New Contributor III
455 Views
Yep, that's the way. The data file's name will appear on the command line after the name of the executable associated with the file extension, so you should then use the GETARG routine e.g.

USE IFLIB (or DFLIB if using CVF)
character(256) filenamebuffer
integer(2) istat
CALL GETARG(1,filenamebuffer,istat)

to retrieve the filename and put it into the character string buffer filenamebuffer. Check the status using istat.

Drag-and-drop will also work this way. If you drag a datafile from Explorer onto a desk top shortcut to the the application, or if you drag a data file onto the applications execuatble file in Explorer, then Windows will start the application as if started with a command line and it will supply the file name as the first argument on the commandline.
0 Kudos
Les_Neilson
Valued Contributor II
455 Views
Mike,

If you are using a version of the compiler with the Fortran 2003 command line options then, for portability and standards conformancee you should use them :
GET_COMMAND
GET_COMMAND_ARGUMENT
COMMAND_ARGUMENT_COUNT

here isthe example from the help :

[cpp]program echo_command_line
  integer i, cnt, len, status
  character c*30, b*100

  call get_command (b, len, status)
  if (status .ne. 0) then
     write (*,*) 'get_command failed with status = ', status
     stop
  end if
  write (*,*) 'command line = ', b (1:len)

  call get_command_argument (0, c, len, status)
  if (status .ne. 0) then
     write (*,*) 'Getting command name failed with status = ', status
     stop
  end if
  write (*,*) 'command name = ', c (1:len)

  cnt = command_argument_count ()
  write (*,*) 'number of command arguments = ', cnt

  do i = 1, cnt
     call get_command_argument (i, c, len, status)
     if (status .ne. 0) then
       write (*,*) 'get_command_argument failed: status = ', status, ' arg = ', i
       stop
     end if
     write (*,*) 'command arg ', i, ' = ', c (1:len)
  end do

  write (*,*) 'command line processed'
  end
[/cpp]


Les
0 Kudos
Jugoslav_Dujic
Valued Contributor II
455 Views
Quoting - michaelgreen
Hi All,

I would like to be able to activate a Win32 program by double clicking on an associated file in Windows Explorer. I don't want to disturb the program's normal menu driven operation, but if a user decides to double click on one of its normal data files I would like the program to open in its graphical mode and deal with the file, etc, etc. I presume this would be achieved through some operation using the WinMain lpCmdLine argument but I am unable to make much progress with the online documentation. Can this be done?


Few additional data:

  1. Data that you get through WinMain's lpszCmdLine is the same as through GETARG, which is in turn the same as standard F2003 GET_COMMAND_ARGUMENT (supported since Ifort 9.1 or so). The latter two already do the command line parsing (argument splitting) so they're easier to use.
  2. Pay attention to file paths containing blanks. Without quotes, such paths will be interpreted as multiple command-line arguments by GETARG/GET_COMMAND_ARGUMENTs. So, ensure that your file association reads like myprogram.exe "%1". In that case (if I recall correctly), you will get the filename quoted (e.g. ' "C:usersMikeMy documentsfoo.dat" ') within the string, so you must strip the quotes using e.g. INDEX intrinsic before you OPEN the file.
  3. Also consider use case where you have file A already open in the application, but user double-clicks on file B. Do you want to start a new instance (default behavior), or open B in existing instance? In the latter case, what happens if there are unsaved data in A? Etc.
0 Kudos
Reply