Archive: How to register extensions and map them to my program?


How to register extensions and map them to my program?
I am trying to register .mpo and .mmx with a program which should be safe to run on OSes as early as 95. I would like it very much if these extensions can register globally for any user.

I did some searching and have come up with the following resources.

Path Manipulation - NSIS Archive
http://nsis.sourceforge.net/archive/....php?pageid=91

Path Manipulation - Windows 2000 - thread
http://forums.winamp.com/showthread....hreadid=140083

Search path functions - thread
http://forums.winamp.com/showthread....ght=refreshenv

Am I hitting this search on the head? Am I way off? Can this be done and is it safe on all operating systems? No telling now unless I try...

Someone help guide me a little?

Thanks in advance :)


Isn't this what you're looking for?
http://nsis.sourceforge.net/archive/...instances=0,11

-Stu


Thank you Afrow UK,

This looks like its going to be easy (I hope). I am pasting the code back in here with my modifications, just guide and correct me where I go wrong if you could, thanks again!

  ; back up old value of .mmx
ReadRegStr $1 HKCR ".mmx" ""
StrCmp $1 "" NoBackup
StrCmp $1 "OptionsFile" NoBackup
WriteRegStr HKCR ".nsi" "backup_val" $1 Whats the .NSI doing here?
NoBackup:
WriteRegStr HKCR ".mmx" "" "OptionsFile"
ReadRegStr $0 HKCR "Options File" ""
StrCmp $0 "" 0 skipOPTAssoc
WriteRegStr HKCR "OptionsFile" "" "Options File"
WriteRegStr HKCR "OptionsFile\shell" "" "open"
WriteRegStr HKCR "OptionsFile\DefaultIcon" "" $INSTDIR\mypathto\myprogram.exe,1
skipOPTAssoc:
WriteRegStr HKCR "OptionsFile\shell\open\command" "" '$INSTDIR\mypathto\myprogram.exe
"%1"'
WriteRegStr HKCR "OptionsFile\shell\edit" "" "Edit Options File"
WriteRegStr HKCR "OptionsFile\shell\edit\command" "" '$INSTDIR\mypathto\myprogram.exe
"%1"'

I am really excited about this... Have I got it right so far?

I must place this at the end of the install section correct?

Thank you for pointing this out Afrow UK :)

This script was only posted about 5 days ago, so you were lucky to ask for it now and not 5 days ago :)
This script was used in the NSIS b4 installer exe to register the .nsi and .nsh files to have the special logo and to open with notepad.exe!

Therefore, just replace all the .nsi bits with you're own extension (I presume!)

I've never used this script, so hope it works!

-Stu


It works with one quirk I've not managed to get around. How do I map an icon to the new extension? It works but the files look like files on Windows without an associated program...

Also, how do I call two sections during uninstall? Its pretty easy to embed the uninstall script into the uninstall section but unfortunately I can only embed the script once. Adding it a second time causes a conflict.

Thank you Afrow UK for your point in the right direction!


If you're on Windows 9x the icon doesn't update immediately. See this page for a possible solution.

As for the uninstall sections, you can only have one. If you need to pass on data from the installer to the uninstaller to know what to uninstall do so in the registry or in an INI file.


I am on WinXP SP1 fully updated. This script works and will register the mmx and mpo extensions but it will not give the file its associated icon. This is the current script. The blue text is where along the script this section is currently located. The crimson text is the only changes I've made. I also numbered this to make help a little easier. Thanks :)


01. !insertmacro MUI_FUNCTIONS_DESCRIPTION_END
02. Section ;Register mmx Extension
03. ; back up old value of .mmx
04. ReadRegStr $1 HKCR ".mmx" ""
05. StrCmp $1 "" NoBackup
06. StrCmp $1 "OptionsFile" NoBackup
07. WriteRegStr HKCR ".nsi" "backup_val" $1
08. NoBackup:
09. WriteRegStr HKCR ".mmx" "" "OptionsFile"
10. ReadRegStr $0 HKCR "Mark Master File" ""
11. StrCmp $0 "" 0 skipOPTAssoc
12. WriteRegStr HKCR "OptionsFile" "" "Mark Master File"
13. WriteRegStr HKCR "OptionsFile\shell" "" "open"
14. WriteRegStr HKCR "OptionsFile\DefaultIcon" "" $INSTDIR\mmx.exe,1
15. skipOPTAssoc:
16. WriteRegStr HKCR "OptionsFile\shell\open\command" "" '$INSTDIR\mmx.exe "%1"'
17. WriteRegStr HKCR "OptionsFile\shell\edit" "" "Edit Mark Master File"
18. WriteRegStr HKCR "OptionsFile\shell\edit\command" "" '$INSTDIR\mmx.exe "%1"'
19. SectionEnd
20. Function un.onUninstSuccess
The above code will register the mmx extension but fails in giving the associated files the executable icon.

The below code is what I have for uninstall. To be honest I do not think it is uninstalling any registry entries. I'll explain later.

01. Section Uninstall
02. ;start of restore script
03. ReadRegStr $1 HKCR ".mmx" ""
04. StrCmp $1 "OptionsFile" 0 NoOwn ; only do this if we own it
05. ReadRegStr $1 HKCR ".mmx" "backup_val"
06. StrCmp $1 "" 0 RestoreBackup ; if backup == "" then delete the whole key
07. DeleteRegKey HKCR ".mmx"
08. Goto NoOwn
09. RestoreBackup:
10. WriteRegStr HKCR ".mmx" "" $1
11. DeleteRegValue HKCR ".mmx" "backup_val"
12. NoOwn:
13. ;rest of script
14. ...

I checked my registry with regedit before a test install and I noticed I had no registry entries to my program. After install I checked again but didn't see any. Once I launched an associated file the registry entries kicked in. When I decided to uninstall the application and check the registry again, it seemed some entries were left behind.

These are just a few entries left behind which were never in the system to begin with.
[HKEY_CLASSES_ROOT\Applications\mmx.exe]

[HKEY_CLASSES_ROOT\Applications\mmx.exe\shell]
@="open"

[HKEY_CLASSES_ROOT\Applications\mmx.exe\shell\edit]
@="Edit Mark Master File"

[HKEY_CLASSES_ROOT\Applications\mmx.exe\shell\edit\command]
@="C:\\mmx\\mmx.exe \"%1\""

[HKEY_CLASSES_ROOT\Applications\mmx.exe\shell\open]

[HKEY_CLASSES_ROOT\Applications\mmx.exe\shell\open\command]
@="C:\\mmx\\mmx.exe \"%1\""


I am really trying but build after build I do not know what left to do in order to get the icon to map and the uninstall to fully uninstall the registry entries. I would appreciate backing up an original and replacing it but I am doing this without original registry entries.

How do I map the icons and fully uninstall the reg entries?

Thanks in advance :)

I tested your script on a WinXP machine and default icon seems to work only after restart of the machine. As for the registry entries the current uninstall anly deleted the association entries and leaves the main entries intact. I'll try to fix this and post back when done.

Vytautas


Still working on the uninstall function but I also found an error in my original archive page. Line 10 of your script should be

10.   ReadRegStr $0 HKCR "OptionsFile" ""
Also note that you should use a more descriptive and unique replacement for "OptionsFile", e.g. it should reflect the file description in some way preferably with the name of you program to avoid other programs overwriting your association accidentally.

Vytautas

I am not too worried about the icon file because this script is fabulous at what it does now. I modified the script so to register both the mmx and mpo extensions... I also added the new lines in your latest update. Also the new orange color shows how I incremented the code to work for two extensions. Take note of the green snippets the second time around and how they're suffixed with a 2. I am simply trying to accomplish registering two extension in one section. This works but is this right?

00. !insertmacro MUI_FUNCTIONS_DESCRIPTION_END
01. Section
02. ;Register mmx Extension
03. ; back up old value of .mmx
04. ReadRegStr $1 HKCR ".mmx" ""
05. StrCmp $1 "" NoBackup
06. StrCmp $1 "MarkMasterFile" NoBackup
07. WriteRegStr HKCR ".nsi" "backup_val" $1
08. NoBackup:
09. WriteRegStr HKCR ".mmx" "" "MarkMasterFile"
10. ReadRegStr $0 HKCR "MarkMasterFile" ""
11. StrCmp $0 "" 0 skipOPTAssoc
12. WriteRegStr HKCR "MarkMasterFile" "" "Program Options File"
13. WriteRegStr HKCR "MarkMasterFile\shell" "" "open"
14. WriteRegStr HKCR "MarkMasterFile\DefaultIcon" "" $INSTDIR\mmx.exe,1
15. skipOPTAssoc:
16. WriteRegStr HKCR "MarkMasterFile\shell\open\command" "" '$INSTDIR\mmx.exe "%1"'
17. WriteRegStr HKCR "MarkMasterFile\shell\edit" "" "Edit Mark Master File"
18. WriteRegStr HKCR "MarkMasterFile\shell\edit\command" "" '$INSTDIR\mmx.exe "%1"'
19.
20. ;Register mpo Extension
21. ; back up old value of .mpo
22. ReadRegStr $1 HKCR ".mpo" ""
23. StrCmp $1 "" NoBackup2
24. StrCmp $1 "MarkMasterFile2" NoBackup2
25. WriteRegStr HKCR ".nsi" "backup_val" $1
26. NoBackup2:
27. WriteRegStr HKCR ".mpo" "" "MarkMasterFile2"
28. ReadRegStr $0 HKCR "MarkMasterFile2" ""
29. StrCmp $0 "" 0 skipOPTAssoc2
30. WriteRegStr HKCR "MarkMasterFile2" "" "Program Options File"
31. WriteRegStr HKCR "MarkMasterFile2\shell" "" "open"
32. WriteRegStr HKCR "MarkMasterFile2\DefaultIcon" "" $INSTDIR\mmx.exe,1
33. skipOPTAssoc2:
34. WriteRegStr HKCR "MarkMasterFile2\shell\open\command" "" '$INSTDIR\mmx.exe "%1"'
35. WriteRegStr HKCR "MarkMasterFile2\shell\edit" "" "Edit Mark Master File"
36. WriteRegStr HKCR "MarkMasterFile2\shell\edit\command" "" '$INSTDIR\mmx.exe "%1"'
37. SectionEnd
38. Function un.onUninstSuccess


I hope the above code is correct. My greatest worry is leaving behind registry entries after an uninstall. In theory, should the uninstall script restore the association if any and if their wasn't any then completely remove all newly created associations?

Thank you so much for this powerful script Vytautas. I am not too worried about the icons if they pop up at some point but would definitely stress removing them during uninstall... Personally I do not like the idea of a program leaving anything behind.

It is so far awesome and I have just two questions. Is the above code correct for registering two extensions (mmx, mpo)? It works for me but I would really just like to make sure and not sit on a time bomb.

How is the uninstall coming along?

Thank you again Vytautas :)

I know of a way of making the uninstall work properly, however I am having problems with that approach in Win2K/WinXP and possibly NT, waiting for reply on another thread before I can finish this.

00. !insertmacro MUI_FUNCTIONS_DESCRIPTION_END
01. Section
02. ;Register mmx Extension
03. ; back up old value of .mmx
04. ReadRegStr $1 HKCR ".mmx" ""
05. StrCmp $1 "" NoBackup
06. StrCmp $1 "MarkMasterFile" NoBackup
07. WriteRegStr HKCR ".mmx" "backup_val" $1
08. NoBackup:
09. WriteRegStr HKCR ".mmx" "" "MarkMasterFile"
10. ; back up old value of .mpo
11. ReadRegStr $1 HKCR ".mpo" ""
12. StrCmp $1 "" NoBackup2
13. StrCmp $1 "MarkMasterFile" NoBackup2
14. WriteRegStr HKCR ".mpo" "backup_val" $1
15. NoBackup2:
16. WriteRegStr HKCR ".mpo" "" "MarkMasterFile"
17. ;Check Associations
18. ReadRegStr $0 HKCR "MarkMasterFile" ""
19. StrCmp $0 "" 0 skipAssoc
20. WriteRegStr HKCR "MarkMasterFile" "" "Program Options File"
21. WriteRegStr HKCR "MarkMasterFile\shell" "" "open"
22. WriteRegStr HKCR "MarkMasterFile\DefaultIcon" "" $INSTDIR\mmx.exe,1
23. skipAssoc:
24. WriteRegStr HKCR "MarkMasterFile\shell\open\command" "" '$INSTDIR\mmx.exe "%1"'
25. WriteRegStr HKCR "MarkMasterFile\shell\edit" "" "Edit Mark Master File"
26. WriteRegStr HKCR "MarkMasterFile\shell\edit\command" "" '$INSTDIR\mmx.exe "%1"'
27. SectionEnd
28. Function un.onUninstSuccess
This code should be simpler and work as long as the commands for both file types are identical.

Vytautas

Excellent, so the code I had earlier would be better if the program was different? Very cool, thank you for your knowledge and expertise on this issue Vytautas.

I certainly do look forward to an enhanced uninstall indeed. Thank you again for your time :)


Sorry vbgunz, as can be seen from the discusion in this thread the script for complete removal and restoration of previous registry keys will become very complicated and does not have a lot of benifits. Most if not all other programs leave similar registry entries after their removal. I will not completely give up on this topic and if I ever figgure out a simpler way of achieving this goal I will post it here.

Vytautas


Hello Vytautas,

At the moment I am forced to leave behind the registry entries in which my program created during install? I would really like to then completely remove those entries without restoration to a previous association.

Is this a real possibility? I believe it is simply sloppy of a program to leave anything behind. I hope you understand. Is it possible to completely remove the keys without worrying about restoring any previous ones?

Thank you Vytautas :)


Yes you can just delete the other key using this command.

DeleteRegKey HKEY_CLASSES_ROOT "MarkMasterFile"
Just be carefull to try and use a key which is unlikely to be registered to other programs, e.g. do not use a key such as 'txtfile'.

Vytautas

Thank you Vytautas, this does the trick :)