Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to register a file type and icon with Windows?

API Functions

How to register a file type and icon with Windows?

by  EricDenDoop  Posted    (Edited  )
Q: I have written a small word processing program in VFP. I have given the extension ".RST". When I view the .rst files in Windows Explorer, the default Windows icon is displayed. I want my own icon to appear.

A: This requires some registry modifications:
Code:
* load api functions
DECLARE INTEGER RegOpenKeyEx IN Win32API ;
    INTEGER nKey, STRING @cSubKey, INTEGER nReserved, ;
    INTEGER nAccessMask, INTEGER @nResult

DECLARE INTEGER RegQueryValue IN WIN32API INTEGER nKey, ;
    STRING @cSubKey, STRING @cBuffer, INTEGER nBufferSize

DECLARE INTEGER RegQueryValueEx IN Win32API INTEGER nKey, ;
    STRING cValueName, INTEGER nReserved, ;
    INTEGER @nType, STRING @cBuffer, INTEGER @nBufferSize

DECLARE INTEGER RegCloseKey IN Win32API INTEGER nKey

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

DECLARE INTEGER RegOpenKey IN ADVAPI32.DLL INTEGER hKey, ;
   STRING lpSubKey, INTEGER @phkResult

DECLARE INTEGER RegCreateKey IN ADVAPI32.DLL INTEGER nHKey, ;
    STRING @cSubKey, INTEGER @nResult

* some constants
#DEFINE EXEFILE "c:\program files\myfolder\myapp.exe" && your program file name and path here
#DEFINE EXEDESC "My Application" && your app description here
#DEFINE FILE_EXT ".RST" && your file type here
#DEFINE SECURITY_ACCESS_MASK 983103 
#DEFINE SHCNE_ASSOCCHANGED 0x08000000
#DEFINE SHCNF_IDLIST 0x0000
#DEFINE HKEY_CLASSES_ROOT -2147483648 
#DEFINE HKEY_CURRENT_USER -2147483647 
#DEFINE HKEY_LOCAL_MACHINE -2147483646 
#DEFINE HKEY_USERS -2147483645 
#DEFINE KEY_ALL_ACCESS 0x3F

* create a registry key for your application
lcExe = FORCEEXT(JUSTFNAME(EXEFILE), "")
lnResult=0
lnDisplay=0
lcKeyName = lcExe
lcKeyValue = EXEDESC
lnKeyLen = LEN(lcKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT, lcKeyName, 0, "REG_SZ", 0, SECURITY_ACCESS_MASK, 0, @lnResult, @lnDisplay)
RegSetValueEx(lnResult, "", 0, 1, lcKeyValue, lnKeyLen)
RegCloseKey(@lnResult)

* register your file type
lnResult= 0
lnDisplay = 0
lcKeyName = ".RST"
lcKeyValue = lcExe
lnKeyLen = LEN(lcKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT, lcKeyName, 0, "REG_SZ", 0,SECURITY_ACCESS_MASK, 0, @lnResult, @lnDisplay)
RegSetValueEx(lnResult, "", 0, 1, lcKeyValue, lnKeyLen)
RegCloseKey(@lnResult)

* register the command that has to be executed when the file type is double clicked in Windows Explorer
lnResult= 0
lnDisplay = 0
lcKeyName = lcExe + "\shell\open\command"
* if you want to pass the file name that was doubleclicked in Explorer as a parameter, then
* replace the following line with: 
* lcKeyValue = EXEFILE + " %1"
lcKeyValue = EXEFILE
lnKeyLen = LEN(lcKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT, lcKeyName, 0, "REG_SZ", 0, SECURITY_ACCESS_MASK, 0, @lnResult, @lnDisplay)
RegSetValueEx(lnResult, "", 0, 1, lcKeyValue, lnKeyLen)
RegCloseKey(@lnResult)

* add a reference top the icon.
* note: Windows 'extracts' the icon that was attached to the exe/project at design time
lnResult= 0
lnDisplay = 0
lcKeyName = lcExe + "\DefaultIcon"
lcKeyValue = EXEFILE + ",0" && ",0" is a pointer to the icon inside the .EXE
lnKeyLen = LEN(lcKeyValue)
RegCreateKeyEx(HKEY_CLASSES_ROOT, lcKeyName, 0, "REG_SZ", 0, SECURITY_ACCESS_MASK, 0, @lnResult, @lnDisplay)
RegSetValueEx(lnResult, "", 0, 1, lcKeyValue, lnKeyLen)
RegCloseKey(@lnResult)

* Instruct Windows to refresh the icon & file type information
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top