Page 1 sur 2

Ouvrir un fichier avec une extension précise

Publié : lun. 21/nov./2005 19:11
par AWEAR
Voilà j'aimerais que mon programme devienne celui par défaut d'un certain type d'extension fichier, mais je ne sais pas comment faire.....
Quelqu'un pourrait-il m'aiguiller ?

Publié : lun. 21/nov./2005 19:51
par Chris
Trouvé dans CodeArchiv.

Code : Tout sélectionner

; English forum: http://purebasic.myforums.net/viewtopic.php?t=6763&highlight=
; Author: GPI (updated for PB3.92+ by Lars)
; Date: 29. June 2003
;
;- ATTENTION
; This code changes your standard program to open pb files and creates 
; an right-click.menu item for pb files. You have to undo these changes 
; yourself.

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 

;and now an addition context-item 
AssociateFileEx("txt","","C:\WINDOWS\notepad.exe","","Notepad.exe","Ouvrir avec Bloc-Notes de Windows","open_with_notepad") 

Publié : lun. 21/nov./2005 19:54
par AWEAR
8O Bon ben merci je vais regarder ça..

Publié : lun. 21/nov./2005 20:21
par AWEAR
A quoi correspond Adr de la fonction Setkey() ???

Publié : lun. 21/nov./2005 20:39
par Chris
Ca correspond à ce qui va s'afficher dans le menu pop-up quand tu vas cliquer sur ton fichier.

Dans l'exemple que je t'ai posté, si tu fais un clic droit sur un ficher *.txt, le menu pop-up affichera "Ouvrir avec Bloc-Notes de Windows"

Publié : lun. 21/nov./2005 20:48
par AWEAR
Vu que je ne possède toujours pas la version complète de PureBasic :(
j'ai essayé de remplacer ton code par un code qui fonctionnerait sous ma démo.
J'ai donc fait ceci :

Code : Tout sélectionner

Procedure setkey(dossier, key$, subkey$, type, Adr, len)
OpenLibrary(0, "advapi32.dll")
If CallFunction(0, "RegCreateKeyEx", dossier, key$, 0, 0, 0, 44, 0, @newkey, @keyinfo) = 0
CallFunction(0, "RegSetValueEx", newkey, subkey$, 1, Adr, len)
CallFunction(0, "RegCloseKey", newkey)
EndIf
CloseLibrary(0)
EndProcedure
Procedure associatefileex(extension$, ext_description$, programme$, icon$, prgkey$, cmd_description$, cmd_key$)
cmd$ = Chr(34) + programme$ + Chr(34) + " " + Chr(34) + "%1" + Chr(34) 
SetKey(-2147483648, "Applications\" + prgkey$ + "\shell\" + cmd_description$ + "\command", "", 1, @cmd$, Len(cmd$) + 1)
If ext_description$ 
      key$ = extension$ + "_auto_file" 
      SetKey(-2147483648, "." + extension$ , "", 1, @Key$, Len(Key$) + 1) 
      SetKey(-2147483648, Key$,"", 1, @ext_description$, Len(ext_description$) + 1)
      If Icon$ 
        SetKey(-2147483648, Key$ + "\DefaultIcon", "", 1, @Icon$, Len(Icon$) + 1) 
      EndIf 
    EndIf 
 SetKey(-2147483647, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext$, "Application", 1, @prgkey$, Len(prgkey$) + 1) 
 EndProcedure
 AssociateFileEx("txt","","C:\WINDOWS\notepad.exe","","Notepad.exe","Ouvrir avec Bloc-Notes de Windows","open_with_notepad") 
Mais lorsque je compile le programme j'ai un erreur invalid memory access..
Où ai-je fais une erreur ??

Publié : lun. 21/nov./2005 21:07
par Chris
Essaie ça.

Code : Tout sélectionner

Procedure SetKey(dossier, Key$, Subkey$, Type, Adr, len) 
  If OpenLibrary(0, "advapi32.dll") 
    If CallFunction(0, "RegCreateKeyExA", dossier, Key$, 0, 0, 0, 983103 , 0, @NewKey, @KeyInfo) <> 0 
      CallFunction(0, "RegSetValueExA", NewKey, Subkey$, 1, Adr, len) 
      CallFunction(0, "RegCloseKey", NewKey) 
    EndIf 
    CloseLibrary(0)
  EndIf
EndProcedure 

Procedure AssociateFileEx(extension$, ext_description$, programme$, Icon$, prgkey$, cmd_description$, cmd_key$) 
  cmd$ = Chr(34) + programme$ + Chr(34) + " " + Chr(34) + "%1" + Chr(34) 
  SetKey(-2147483648, "Applications\" + prgkey$ + "\shell\" + cmd_description$ + "\command", "", 1, @cmd$, Len(cmd$) + 1) 
  If ext_description$ 
    Key$ = extension$ + "_auto_file" 
    SetKey(-2147483648, "." + extension$ , "", 1, @Key$, Len(Key$) + 1) 
    SetKey(-2147483648, Key$,"", 1, @ext_description$, Len(ext_description$) + 1) 
    If Icon$ 
      SetKey(-2147483648, Key$ + "\DefaultIcon", "", 1, @Icon$, Len(Icon$) + 1) 
    EndIf 
  EndIf 
  SetKey(-2147483647, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext$, "Application", 1, @prgkey$, Len(prgkey$) + 1) 
EndProcedure 
AssociateFileEx("txt","","C:\WINDOWS\notepad.exe","","Notepad.exe","Ouvrir avec le bloc-notes de Windows","open_with_notepad") 

Publié : lun. 21/nov./2005 21:15
par AWEAR
Tu es sur Chris parce que si tu mets :

Code : Tout sélectionner

If CallFunction(0, "RegCreateKeyExA", dossier, Key$, 0, 0, 0, 983103 , 0, @NewKey, @KeyInfo) <> 0
la fonction ne fera rien de plus si RegCreateKeyA renvoi un succes.. :?

Publié : lun. 21/nov./2005 21:22
par Chris
J'avais modifié ça pour tester, j'ai oublié de le remettre. :oops:

Ce qui est bon, c'est la valeur 983103, et les deux commandes ou j'ai rajouté un A à la fin. Le reste, c'est la même code que ce que tu avais posté

Publié : lun. 21/nov./2005 21:30
par AWEAR
Tu es sur parce que j'ai toujours le même problème..

Publié : lun. 21/nov./2005 21:34
par Chris
Met un A à la fin de "RegCloseKey"

Publié : lun. 21/nov./2005 21:36
par AWEAR
Génial le code ne pose plus de problème !
Mais aucun résultat n'est visible....

Publié : lun. 21/nov./2005 21:39
par Chris
Quand tu cliques avec le bouton droit sur un fichier avec l'extension .txt, tu ne vois pas la phrase Ouvrir avec Bloc-Notes de Windows ?

Publié : lun. 21/nov./2005 21:40
par AWEAR
Ben.....non :(

Publié : lun. 21/nov./2005 21:42
par Chris
Tu as quoi, comme OS?
Parce que moi, avec XP, ça fonctionne.