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

WIN32/IVF Question

ahasan
Beginner
359 Views
When a file extension is associated with a WIN32 IVF application (via the registry), a user can double click on a fileand the application will open.
I would like the app then to read thedata in the file, but not sure how toretrieve the path to the file.
Ibelieve that windows sends a command-line to the app with a parameter that specifies a keyword 'Open'along with thepath to the file. What does this command line look like and how do I interpret it in WinMain()?
Any help would be appreciated.
0 Kudos
8 Replies
Jugoslav_Dujic
Valued Contributor II
359 Views
lpszCmdLine argument of WinMain will point to the string containing the file path. Here's a sample code which does that (and strips off quotes around file name if they exist; enclosing file association command in quotes ("%1") ensures correct resolution of paths containing spaces):
CHARACTER(MAX_PATH)::  szCmdFile; POINTER (pszCmdFile, szCmdFile)
CHARACTER(MAX_PATH):: szNtrFile
...
pszCmdFile=lpszCmdLine
iLen=INDEX(szCmdFile, CHAR(0))
IF (iLen.GT.1) THEN
szNtrFile=szCmdFile
!Strip quotes
DO i=1, iLen
IF (szNtrFile(i:i).EQ.'"') THEN
IF (i.EQ.1) THEN
szNtrFile(i:i)=' '
ELSE
szNtrFile(i:i)=CHAR(0)
END IF
END IF
END DO
szNtrFile=ADJUSTL(szNtrFile)
!Handler routine for closing previously open file, if any:
CALL OnFileClose()
!Routine for opening new file:
iRet=OpenNtrFile(szNtrFile(1:iLen),tNT,tSec,.TRUE.)
END IF

I think that GETARG/NARGS will also work, and they'll do some parsing for you; you should probably still do quote-stripping yourself. Didn't test that approach though.

Jugoslav
0 Kudos
ahasan
Beginner
359 Views
Very helpful, thank you! Did not consider closing previouly open file, so I was pleased to see that in your code.

Message Edited by halcyong@fcc.net on 12-01-2005 07:35 AM

0 Kudos
Jugoslav_Dujic
Valued Contributor II
359 Views
Actually, it is spurious in my code. By default, Windows will create a new instance of your .exe each time you double-click the associated file, thus it can never have a "previously open file" in WinMain. That can be workarounded explicitly (if you want just one instance), but even in that case you cannot pass it through WinMain.

Jugoslav
0 Kudos
Paul_Curtis
Valued Contributor I
359 Views
Here is an alternate approach to retreive command-line arguments, and also to determine the origination path ofan executing program.Code:
!   command-line arguments
nc = IARGC()
IF (nc > 0) THEN
  DO j = 1, nc
    CALL GetArg (j, readstring, status)
    IF (status <= 0) EXIT
    IF (INDEX(readstring, '-W') > 0) fullscreen = .FALSE.
    IF (INDEX(readstring, '-w') > 0) fullscreen = .FALSE.
    IF (INDEX(readstring, '-C') > 0) netclient  = .TRUE.
    IF (INDEX(readstring, '-c') > 0) netclient  = .TRUE.
    IF (INDEX(readstring, '-D') > 0) demo	     = .TRUE.
    IF (INDEX(readstring, '-d') > 0) demo	     = .TRUE.
  END DO
END IF

!   get local root path from opsys
localroot = ''
nc = GetModuleFileName(GetModuleHandle('MYPROG.EXE'C), fpname, LEN(fpname))
IF (nc > 0) THEN
  CALL updncase (fpname, 1)
  nc = INDEX(fpname, 'BINMYPROG.EXE')
  IF (nc > 0) localroot = fpname(1:nc - 1)
END IF


0 Kudos
ahasan
Beginner
359 Views
Ihave a few questions but will look over the code and comments provided before posting.
Thanks for all the info.
0 Kudos
ahasan
Beginner
359 Views

In Windows Installer one can specify -print "%1" for a command line parameter, where -print is a command switch.

Iassumethat-print "%1" is merged into one string,"-printC:mydir...myfile", whenthe command-line is sent to the app.

Is this correct?

Thanks.

0 Kudos
Jugoslav_Dujic
Valued Contributor II
359 Views
I'm not sure -- please try it yourself.

Jugoslav
0 Kudos
ahasan
Beginner
359 Views

Thanks for the reply. This is my first attempt at editing the registry with Windows Installer,and didn't want to try until I had as much info as possible. However, I think using Windows Installer ispretty safe, so I will give it a go.

After some reading in a C++ reference book, I do believe the command line sent to an app will have two parameter strings: "-print' and "%1". Therefore, the application code would have to determine if Iargc() = 1 or 2. If 2 then test for the "-print' string. If "-print" exists then the app would print the file in path "%1'.

Same for "-open". However, I believe, by convention the "-open" string isnot included in the command-linestrings when a file is double clicked, so the only command-line string [Iargc() = 1] an app would have to deal with is "%1".

Thanks again for all the info. Got me started in the right direction.

0 Kudos
Reply