by Richard Russell, May 2015

The code below associates an application with a particular file type, as indicated by its extension. The extension is specified as Extension$ (don't forget the dot) and the command used to open files with that extension is specified as Command$. The code assumes that the name of the file is passed to the application as a simple command-line parameter.

The variable ProgID$ must be unique to that particular application, and this can be most easily achieved by incorporating the extension as part of the name, as shown in the example code (i.e. the extension .xyz and the ProgID xyzfile).

The code writes to the HKEY_CLASSES_ROOT registry hive and will only run successfully if it has administrative privileges. On Windows Vista or later, with UAC (User Account Control) enabled, this can be most easily achieved by using Run As Administrator (either for the LBB IDE or the compiled executable, depending on how it is run).

Use this code with care, especially as it can change the association of an existing file type - without prompting for confirmation or otherwise alerting the user.

    Extension$ = ".xyz"
    ProgID$ = "xyzfile"
    Command$ = CHR$(34) + "C:\Temp\MyProg.exe" + CHR$(34) + " %1"
 
    ProgIDLen = LEN(ProgID$) + 1
    CommandLen = LEN(Command$) + 1
 
    STRUCT key, handle AS ULONG
 
    CALLDLL #advapi32, "RegCreateKeyExA", _HKEY_CLASSES_ROOT AS ULONG, _
      Extension$ AS PTR, 0 AS LONG, "" AS PTR, 0 AS LONG, _
      _KEY_WRITE AS LONG, 0 AS LONG, key AS STRUCT, _
      0 AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegCreateKeyEx failed (ProgID)" : END
 
    CALLDLL #advapi32, "RegSetValueExA", key.handle.struct AS ULONG, _
      "" AS PTR, 0 AS LONG, _REG_SZ AS LONG, _
      ProgID$ AS PTR, ProgIDLen AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegSetValueEx failed (ProgID)" : END
 
    CALLDLL #advapi32, "RegCloseKey", key.handle.struct AS ULONG, _
      ret AS LONG
 
    CALLDLL #advapi32, "RegCreateKeyExA", _HKEY_CLASSES_ROOT AS ULONG, _
      ProgID$ + "\shell\Open\command" AS PTR, 0 AS LONG, "" AS PTR, _
      0 AS LONG, _KEY_WRITE AS LONG, 0 AS LONG, key AS STRUCT, _
      0 AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegCreateKeyEx failed (Command)" : END
 
    CALLDLL #advapi32, "RegSetValueExA", key.handle.struct AS ULONG, _
      "" AS PTR, 0 AS LONG, _REG_SZ    AS LONG, _
      Command$ AS PTR, CommandLen AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegSetValueEx failed (Command)" : END
 
    CALLDLL #advapi32, "RegCloseKey", key.handle.struct AS ULONG, _
      ret AS LONG