HOWTO: Associate File Extensions with a Custom Visual FoxPro ApplicationID: Q237454
|
Sometimes, developers may want to associate certain file extensions with a custom Visual FoxPro application. It may be desirable to perform certain actions on certain file types. For instance, the developer may want to run .ZZ0 files and edit .ZZ1 files. The following example demonstrates how to do this from Visual FoxPro using API calls.
NOTE: Developers will need Visual FoxPro 6.0 with Visual Studio 6.0 Service Pack 3 (VFP6_SP3) in order to RUN the custom files using the method outlined here. This is because the ability to compile programs through custom Visual FoxPro applications was introduced in VFP6_SP3.
MESSAGEBOX("HELLO!",48,"Associated Via API")
LOCAL nResult,nDisplay, cKeyName, cKeyValue, nKeyLen
#DEFINE SECURITY_ACCESS_MASK 983103 && SAM value KEY_ALL_ACCESS
#DEFINE SHCNE_ASSOCCHANGED 0x08000000
#DEFINE HKEY_CLASSES_ROOT -2147483648
#DEFINE SHCNF_IDLIST 0x0000
DECLARE RegCreateKeyEx IN ADVAPI32.DLL;
INTEGER,STRING,INTEGER,STRING,INTEGER,INTEGER,INTEGER,INTEGER @, INTEGER @
DECLARE RegSetValueEx IN ADVAPI32.DLL;
INTEGER,STRING,INTEGER,INTEGER,STRING,INTEGER
DECLARE RegCloseKey IN ADVAPI32.DLL INTEGER nHKey
DECLARE SHChangeNotify IN Shell32.DLL INTEGER, INTEGER, STRING, STRING
*~ This creates a Root entry called "aaApp."
nResult=0
nDisplay=0
cKeyName = "aaAPP"
cKeyValue = "My Application"
nKeyLen = LEN(cKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT,cKeyName ,0,"REG_SZ", ;
0,SECURITY_ACCESS_MASK,0,@nResult,@nDisplay)
RegSetValueEx(nResult,"",0,1,cKeyValue ,nKeyLen)
RegCloseKey(@nResult)
*~ This creates a Root entry called .ZZ0 associated with "aaApp."
nResult= 0
nDisplay = 0
cKeyName = ".ZZ0"
cKeyValue = "aaApp"
nKeyLen = LEN(cKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT,cKeyName,0,"REG_SZ",;
0,SECURITY_ACCESS_MASK,0,@nResult,@nDisplay)
RegSetValueEx(nResult,"",0,1,cKeyValue,nKeyLen)
RegCloseKey(@nResult)
*~ This creates a Root entry called .ZZ1 associated with "aaApp."
nResult= 0
nDisplay = 0
cKeyName = ".ZZ1"
cKeyValue = "aaApp"
nKeyLen = LEN(cKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT,cKeyName,0,"REG_SZ",;
0,SECURITY_ACCESS_MASK,0,@nResult,@nDisplay)
RegSetValueEx(nResult,"",0,1,cKeyValue,nKeyLen)
RegCloseKey(@nResult)
*~ This sets the command line for files associated with "aaApp."
*~ The path will need to be adjusted.
nResult= 0
nDisplay = 0
cKeyName = "aaApp\shell\open\command"
*~ ADJUST THIS PATH TO REFLECT YOUR DIRECTORY STRUCTURE.
*~
cKeyValue = "C:\WINDOWS\Desktop\testexe.exe %1"
*~
*~
nKeyLen = LEN(cKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT,cKeyName,0,"REG_SZ",;
0,SECURITY_ACCESS_MASK,0,@nResult,@nDisplay)
RegSetValueEx(nResult,"",0,1,cKeyValue,nKeyLen)
RegCloseKey(@nResult)
*~ Code to refresh icons after association.
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL)<BR/>
CLEAR DLLS
cKeyValue = "C:\WINDOWS\Desktop\testexe.exe %1"
*~ Double-clicking the file passes its full path to the
*~ associated program (this .EXE).
LPARAMETER cFileName
#DEFINE MB_OK 0 && OK button only
#DEFINE MB_ICONINFORMATION 64 && Information icon
SET SYSMENU OFF
_SCREEN.CAPTION = "Test Executable"
*~ Check extension of passed file name.
DO CASE
CASE UPPER(RIGHT(cFileName,3)) = "ZZ0"
*~ This code checks for VFP 6 SP3.
*~ Exits if anything but.
IF !("06.00.8492.00" $ ALLT(VERS()))
MESSAGEBOX("Not using VFP6 SP3. Needed to 'COMPILE' in an .EXE.", ;
MB_ICONINFORMATION + MB_OK,"Exiting")
RETURN
ENDIF
* File extension is .ZZO and you are in VFP6 SP3. Compile and run.
COMPILE (cFileName)
DO (cFileName)
CASE UPPER(RIGHT(cFileName,3)) = "ZZ1"
*~ File is .ZZ1, so modify it. Not VFP6SP3 dependant.
MODI COMM (cFileName)
ENDCASE
*!* After the demo has run, use this program to remove the registry keys it created.
#DEFINE SHCNE_ASSOCCHANGED 0x08000000
#DEFINE HKEY_CLASSES_ROOT -2147483648
#DEFINE SHCNF_IDLIST 0x0000
FILE_KEY1 = ".ZZ0"
FILE_KEY2 = ".ZZ1"
EXE_KEY0 = "aaAPP\shell\open\command"
EXE_KEY1 = "aaAPP\shell\open"
EXE_KEY2 = "aaAPP\shell"
EXE_KEY3 = "aaAPP"
DECLARE RegDeleteKey IN ADVAPI32.DLL INTEGER, STRING
DECLARE SHChangeNotify IN Shell32.DLL INTEGER, INTEGER, STRING, STRING
RegDeleteKey(HKEY_CLASSES_ROOT,FILE_KEY1)
RegDeleteKey(HKEY_CLASSES_ROOT,FILE_KEY2)
RegDeleteKey(HKEY_CLASSES_ROOT,EXE_KEY0)
RegDeleteKey(HKEY_CLASSES_ROOT,EXE_KEY1)
RegDeleteKey(HKEY_CLASSES_ROOT,EXE_KEY2)
RegDeleteKey(HKEY_CLASSES_ROOT,EXE_KEY3)
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL)
CLEAR DLLS
Additional query words:
Keywords : kbAPI kbNTOS kbVFp300 kbVFp300b kbVFp500 kbVFp500a kbVFp600 kbWinOS95 kbWinOS98 kbGrpFox kbDSupport
Version : WINDOWS:5.0,5.0a,6.0,95,98,98 Second Edition; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbhowto
Last Reviewed: August 12, 1999