Page 1 of 1

Posted: Tue Dec 23, 2003 2:45 pm
by freak
Writing a shell extension handler is not the easiest task, even if you do it
in a language that makes it easier to create COM objects than PB.

As you can read in the documentation, you'll have to export several
Interfaces, which in PB is quite a lot of work. (just look at my COM-downlaod
tutorial in the tips&tricks section, and you'll see.)

A context menu handler is only needed, if you need to dynamically add
menu items, depending on each individual file. If you only want an item
for all files of a type, it should be possible by just adding
some registry entrys.

See here for more details:
http://msdn.microsoft.com/library/defau ... ontext.asp

The PureBasic editor for example does that to add the standart "Open" item:

Code: Select all

; Now, update the registry to associate correctly the '.pb' with the editor.
;

If GetVersion_() & $ff0000  ; Windows NT/XP
  
  If RegCreateKeyEx_(#HKEY_CLASSES_ROOT, "Applications\PureBasic.exe\shell\open\command", 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    StringBuffer$ = Chr(34)+FullPath+"PureBasic.exe"+Chr(34)+" "+Chr(34)+"%1"+Chr(34)
    RegSetValueEx_(NewKey, "", 0, #REG_SZ,  StringBuffer$, Len(StringBuffer$)+1)
    RegCloseKey_(NewKey)
  EndIf
  
  If RegCreateKeyEx_(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.pb", 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    RegSetValueEx_(NewKey, "Application", 0, #REG_SZ,  "PureBasic.exe", Len("PureBasic.exe")+1)
    RegCloseKey_(NewKey)
  EndIf
  
Else  ; The same for Win9x
  
  If RegCreateKeyEx_(#HKEY_LOCAL_MACHINE, "Software\Classes\PureBasic.exe\shell\open\command", 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    StringBuffer$ = Chr(34)+FullPath+"PureBasic.exe"+Chr(34)+" "+Chr(34)+"%1"+Chr(34)
    RegSetValueEx_(NewKey, "", 0, #REG_SZ,  StringBuffer$, Len(StringBuffer$)+1)
    RegCloseKey_(NewKey)
  EndIf
  
  If RegCreateKeyEx_(#HKEY_LOCAL_MACHINE, "Software\CLASSES\.pb", 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    RegSetValueEx_(NewKey, "", 0, #REG_SZ,  "PureBasic.exe", Len("PureBasic.exe")+1)
    RegCloseKey_(NewKey)
  EndIf
  
EndIf
As you see, it adds the subkey "PureBasic.exe\shell\open\command", and
the default value is the action to take when the 'Open' menuitem is clicked.

If you add something like "PureBasic.exe\shell\execute\command", and
set the default value to "C:\PureBasic\Compilers\pbcompiler.exe %1", and
set the default value of the "PureBasic.exe\shell\execute" key to "Execute",
you get another popupmenu item called "Execute", which will runn the
source file directly.


Timo

Posted: Sun Jun 15, 2008 7:36 am
by Joakim Christiansen
Hmm, I've been trying freak's code on Vista but I don't get it to work :S
What I want to do is to add an item in the right click menu for .jpg images.

Edit; this seems to work on Vista:

Code: Select all

[HKEY_CLASSES_ROOT\jpegfile\shell\Upload with ImageUploader]
@="Upload with ImageUploader"

[HKEY_CLASSES_ROOT\jpegfile\shell\Upload with ImageUploader\command]
@="\"C:\\Users\\Joakim\\Desktop\\PureBasic\\ImageUploader.exe" %1"

Posted: Wed Aug 13, 2008 1:03 am
by Karbon
Is there anything that works for both XP and Vista?

Posted: Wed Aug 13, 2008 1:36 am
by freak
The Win9x part of my code above will work on XP and Vista if you change #HKEY_LOCAL_MACHINE to #HKEY_CURRENT_USER.

Posted: Wed May 13, 2009 4:30 pm
by srod
freak wrote:The Win9x part of my code above will work on XP and Vista if you change #HKEY_LOCAL_MACHINE to #HKEY_CURRENT_USER.
@Timo : I have just added the win 9x variant of your code to a program of mine and it works fine on XP and Vista. Strangely though, on both of these systems, running the code results not only in being able to execute my 'default program' against the registered file extension through the shell/open menu, but also by double clicking any file with the appropriate extension!

This is strange because the registry entries allowing for execution by double clicking reside within the HKEY_CLASSES_ROOT tree! I have checked and the relevant entries have been added automatically by the shell!

I have confirmed by manually removing all of these registry entries and running your Win 9x code again!

This is an unexpected side-effect. Not a bad one because I was just about to add the code to do this anyhow! :) I am just a little puzzled why the system should add these entries automatically?

Any idea?

Thanks.