Seite 1 von 1

Dateiendung in Registry zuweisen

Verfasst: 04.09.2010 15:57
von Batze
Hallo Leute,

ich habe eine Dateiendung die ich standardmäßig mit meinem Programm öffnen lassen möchte.
Es werden auch die Registryeinträge die dort stehen geschrieben und das richtige Icon wird angezeigt. Aber beim Doppelklick auf die Datei fragt Windows mit welchem Programm er sie öffnen soll, statt mein Programm zu verwenden.
Ich vermute ja, ich habe irgendeinen wichtigen Schlüssel vergessen, finde aber nicht raus welchen.

Code: Alles auswählen

Procedure Reg_CreateKeyValue(topKey.i, KeyName$, ValueName$, Value$, Type.i, ComputerName$)
  Protected SecurityAttributes.SECURITY_ATTRIBUTES
  Protected NewKey.i, GetHandle.i, RemoteRegistry.i
  Protected Datas$, Value.i
  Protected cbData.i
  
  ; Key erstellen
  If Left(KeyName$, 1) = "\"
    KeyName$ = Right(KeyName$, Len(KeyName$) - 1)
  EndIf
 
  If ComputerName$ = ""
    GetHandle = RegCreateKeyEx_(topKey, KeyName$, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @SecurityAttributes, @NewKey, @GetHandle)
  Else
    RegConnectRegistry_(ComputerName$, topKey, @RemoteRegistry)
    GetHandle = RegCreateKeyEx_(RemoteRegistry, KeyName$, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @SecurityAttributes, @NewKey, @GetHandle)
  EndIf
  
  ; Key setzen
  If GetHandle = #ERROR_SUCCESS
    cbData = 255
    Datas$ = Space(255)
       
    Select Type
       Case #REG_SZ
          GetHandle = RegSetValueEx_(NewKey, ValueName$, 0, #REG_SZ, @Value$, Len(Value$) + 1)
       Case #REG_DWORD
          Value = Val(Value$)
          GetHandle = RegSetValueEx_(NewKey, ValueName$, 0, #REG_DWORD, @Value$, 4)
    EndSelect
    RegCloseKey_(NewKey)
  EndIf
EndProcedure 

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

AssociateFileEx("bit4", "BIT4 - Einstellungen", ProgramFilename(), ProgramFilename()+",1", "BIT4", "open", "")

Re: Dateiendung in Registry zuweisen

Verfasst: 07.09.2010 12:49
von Nino
Hi,

soweit ich weiß, sollten dazu v.a. zwei Schlüssel in HKEY_CLASSES_ROOT vorhanden sein.
Ich habe diese beiden mal (für meine PB-Installation) aus meiner Registry exportiert (Windows XP).
Der erste Schlüssel ist die betr. Dateierweiterung, sein Wert ist der Name des zweiten Schlüssels:

Code: Alles auswählen

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.pb]
@="PureBasic.exe"

Code: Alles auswählen

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\PureBasic.exe]

[HKEY_CLASSES_ROOT\PureBasic.exe\DefaultIcon]
@="C:\\Programme\\PureBasic\\PureBasic.exe,1"

[HKEY_CLASSES_ROOT\PureBasic.exe\shell]

[HKEY_CLASSES_ROOT\PureBasic.exe\shell\open]

[HKEY_CLASSES_ROOT\PureBasic.exe\shell\open\command]
@="\"C:\\Programme\\PureBasic\\PureBasic.exe\" \"%1\"
Grüße, Nino

Re: Dateiendung in Registry zuweisen

Verfasst: 09.10.2010 12:36
von Batze
Zwar etwas späte antwort, aber Danke so funktioniert es.

Code: Alles auswählen

Procedure CreateRegistryKeyValue(topKey.i, KeyName$, ValueName$, Value$, Type.i=#REG_SZ, ComputerName$="")
  Protected SecurityAttributes.SECURITY_ATTRIBUTES
  Protected NewKey.i, GetHandle.i, RemoteRegistry.i
  Protected Datas$, Value.i
  Protected cbData.i
 
  ; Key erstellen
  If Left(KeyName$, 1) = "\"
    KeyName$ = Right(KeyName$, Len(KeyName$) - 1)
  EndIf
  Debug p

  If ComputerName$ = ""
    GetHandle = RegCreateKeyEx_(topKey, KeyName$, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @SecurityAttributes, @NewKey, @GetHandle)
  Else
    RegConnectRegistry_(ComputerName$, topKey, @RemoteRegistry)
    GetHandle = RegCreateKeyEx_(RemoteRegistry, KeyName$, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, @SecurityAttributes, @NewKey, @GetHandle)
  EndIf
 
  ; Key setzen
  If GetHandle = #ERROR_SUCCESS
    cbData = 255
    Datas$ = Space(255)
       
    Select Type
       Case #REG_SZ
          GetHandle = RegSetValueEx_(NewKey, ValueName$, 0, #REG_SZ,    @Value$, StringByteLength(Value$) + 1)
       Case #REG_DWORD
          Value = Val(Value$)
          GetHandle = RegSetValueEx_(NewKey, ValueName$, 0, #REG_DWORD, @Value$, 4)
    EndSelect
    RegCloseKey_(NewKey)
  EndIf
EndProcedure

Procedure AssociateFileExtension(Key$, Ext$, ExtDescription$, Programm$, Icon$, PrgKey$, CmdDescription$, CmdKey$)   ; Dateierweiterung in Registry eintragen
  Protected Cmd$                                                                                                    ; Command
  Protected CmdPath$                                                                                                ; Pfad zu dem Eintrag
  
   CmdPath$ = Key$+"\shell\"+CmdDescription$+"\command"                           ; Pfad erstellen
   Cmd$     = Chr('"')+Programm$+Chr('"') + " " + Chr('"')+"%1"+Chr('"')          ; Command erstellen
   CreateRegistryKeyValue(#HKEY_CLASSES_ROOT,   "."+Ext$,   "", Key$)             ; Schlüssel für die Erweiterung
   CreateRegistryKeyValue(#HKEY_CLASSES_ROOT,   Key$,       "", ExtDescription$)  ; Beschreibung
   CreateRegistryKeyValue(#HKEY_CLASSES_ROOT,   CmdPath$,   "", Cmd$)             ; Programmaufruf
   
   If Icon$                                                                       ; Wenn ein Icon angegeben ist ...
     CreateRegistryKeyValue(#HKEY_CLASSES_ROOT, Key$+"\DefaultIcon",  "", Icon$)  ; Icon-Datei eintragen
   EndIf
EndProcedure

Text_Project.s = "Projekt"
AssociateFileExtension("Bafran.bfn", "tfn", "Bafran-"+Text_Project, ProgramFilename(), ProgramFilename()+",0", "Bafran", "open", "")

MessageRequester("Registriert", "Erfolgreich registriert")