File Associate

Share your advanced PureBasic knowledge/code with the community.
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

File Associate

Post by GPI »

Code: Select all

Procedure SetKey(fold,Key$,Subkey$,Type,Adr,len)
  If RegCreateKeyEx_(fold, Key$, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    RegSetValueEx_(NewKey, Subkey$, 0, Type,  Adr, len)
    RegCloseKey_(NewKey)
  EndIf
EndProcedure
Procedure AssociateFileEx(ext$,ext_description$,programm$,Icon$,prgkey$,cmd_description$,cmd_key$)
  cmd$=Chr$(34)+programm$+Chr$(34)+" "+Chr$(34)+"%1"+Chr$(34)
  If GetVersion_() & $ff0000  ; Windows NT/XP
    SetKey(#HKEY_CLASSES_ROOT, "Applications\"+prgkey$+"\shell\"+cmd_description$+"\command","",#reg_sz    ,@cmd$,Len(cmd$)+1)
    If ext_description$
      Key$=ext$+"_auto_file"
      SetKey(#HKEY_CLASSES_ROOT  ,"."+ext$           ,"",#reg_sz,@Key$,Len(Key$)+1)
      SetKey(#HKEY_CLASSES_ROOT  ,Key$               ,"",#reg_sz,@ext_description$,Len(ext_description$)+1)
      If Icon$
        SetKey(#HKEY_CLASSES_ROOT,Key$+"\DefaultIcon","",#reg_sz,@Icon$,Len(Icon$)+1)
      EndIf
    EndIf
    SetKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ext$,"Application",#reg_sz,@prgkey$         ,Len(prgkey$)+1)
  Else ;Windows 9x
    SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\."+ext$                        ,"",#reg_sz,@prgkey$         ,Len(prgkey$)+1)
    If ext_description$
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$                   ,"",#reg_sz,@ext_description$,Len(ext_description$)+1)
    EndIf
    If Icon$
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\DefaultIcon"    ,"",#reg_sz,@Icon$           ,Len(Icon$)+1)
    EndIf
    If cmd_description$<>cmd_key$
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$,"",#reg_sz,@cmd_description$,Len(cmd_description$)+1)
    EndIf
    SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$+"\command","",#reg_sz,@cmd$   ,Len(cmd$)+1)
  EndIf
EndProcedure
Procedure AssociateFile(ext$,ext_description$,programm$,Icon$)
  AssociateFileEx(ext$,ext_description$,programm$,Icon$,GetFilePart(programm$),"open","open")  
EndProcedure

;little Example
AssociateFile("pb","Pure Basic Source","C:\Programme\PureBasic\PureBasic.exe","F:\icon.bmp,0")
;and now an addition context-item
AssociateFileEx("pb","","F:\alternate rich\jaPBe.exe","","PureBasic.exe","Open with jaPBe","open_with_japbe")
EDIT Forget a "." in win9x part.

GPI
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

GPI, you're about the most handy guy ever. Million thanks for this!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

But how do you un-associate file and its extension ?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Progi1984 wrote:But how do you un-associate file and its extension ?
see in the includes of jaPBe: [Registry.pbi]

Code: Select all

Procedure Remove_AssociateFile(ext$,prgkey$) 
  If GetVersion_() & $ff0000 ; Windows NT/XP 
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"Applications"+prgkey$,"")
    Key$=ext$+"_auto_file" 
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"."+ext$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,Key$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ext$,"") 
  Else ;Windows 9x 
    Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE  ,"Software\Classes\."+ext$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE,"Software\Classes"+prgkey$,"")
  EndIf 
EndProcedure 
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

Thanks
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Post by Progi1984 »

Just after test, the extension is not more associated, but the icon is again on file. Why that ? how can i remove that ?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Use the MS PowerToys Tweak UI > Repair Rebuild Icon.
I think is a Cache issue
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: File Associate

Post by kenmo »

(Resurrecting a dinosaur of a thread.....)

Does anyone know if this registry code will work with Vista / Win7 ?
All I know is it works for me on XP Pro.

Also I cleaned up the formatting a bit and made it EnableExplicit friendly:

Code: Select all

Procedure SetKey(Fold.i, Key.s, Subkey.s, Type.i, Adr.i, len.i)
  Protected NewKey.i, KeyInfo.i
  
  If RegCreateKeyEx_(fold, Key, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    RegSetValueEx_(NewKey, Subkey, 0, Type,  Adr, len)
    RegCloseKey_(NewKey)
  EndIf
EndProcedure

Procedure AssociateFileEx(ext.s, ext_description.s, programm.s, Icon.s, prgkey.s, cmd_description.s, cmd_key.s)
  Protected cmd.s, Key.s
  
  cmd = Chr(34) + programm + Chr(34) + " " + Chr(34) + "%1" + Chr(34)
  If GetVersion_() & $FF0000 ; Windows NT/XP
    SetKey(#HKEY_CLASSES_ROOT, "Applications\" + prgkey + "\shell\" + cmd_description + "\command", "", #REG_SZ, @cmd, Len(cmd) + 1)
    
    If ext_description
      Key = ext + "_auto_file"
      SetKey(#HKEY_CLASSES_ROOT, "." + ext, "", #REG_SZ, @Key, Len(Key) + 1)
      SetKey(#HKEY_CLASSES_ROOT, Key, "", #REG_SZ, @ext_description, Len(ext_description) + 1)
      If Icon
        SetKey(#HKEY_CLASSES_ROOT, Key + "\DefaultIcon", "", #REG_SZ, @Icon, Len(Icon) + 1)
      EndIf
    EndIf
    
    SetKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext, "Application", #REG_SZ, @prgkey, Len(prgkey) + 1)
  Else ; Windows 9x
    SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\." + ext, "", #REG_SZ, @prgkey, Len(prgkey) + 1)
    
    If ext_description
      SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey, "", #REG_SZ, @ext_description, Len(ext_description) + 1)
    EndIf
    If Icon
      SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey + "\DefaultIcon", "", #REG_SZ, @Icon, Len(Icon) + 1)
    EndIf
    If cmd_description <> cmd_key
      SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey + "\shell\" + cmd_key, "", #REG_SZ, @cmd_description, Len(cmd_description) + 1)
    EndIf
    
    SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey + "\shell\" + cmd_key + "\command", "", #REG_SZ, @cmd, Len(cmd) + 1)
  EndIf
EndProcedure

Procedure AssociateFile(ext.s, ext_description.s, programm.s, Icon.s)
  AssociateFileEx(ext, ext_description, programm, Icon, GetFilePart(programm), "open", "open") 
EndProcedure
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: File Associate

Post by ts-soft »

kenmo wrote:(Resurrecting a dinosaur of a thread.....)

Does anyone know if this registry code will work with Vista / Win7 ?
All I know is it works for me on XP Pro.
should work on 7 and xp but only in admin-mode (see compileroptions)!
Post Reply