Archive: File association/defaults question


File association/defaults question
Hello again, I'm trying to associate my application with .txt files and make it the default program for opening .txt files. My app only runs on Vista/Windows 7 so I figured IApplicationAssociationRegistration would do the trick.

I found this plugin on the nsis website:
http://nsis.sourceforge.net/Applicat...ration_plug-in

I have a custom InstallOptions page with a checkbox "Make Notpad the default text editor". First it associates .txt files then reads the ini to get the checkbox state. If checked, it sets the app as default:

Function CustomPageEnd
WriteRegStr HKLM "Software\Classes\Notpad.TextFile" "" "Text Document"
WriteRegStr HKLM "Software\Classes\Notpad.TextFile\DefaultIcon" "" "$INSTDIR\Notpad.exe, 1"
WriteRegStr HKLM "Software\Classes\Notpad.TextFile\shell\open\command" "" "$INSTDIR\Notpad.exe %1"

SetRegView 64
WriteRegStr HKLM "Software\Notpad\Capabilities" "ApplicationDescription" "Notpad is not Notepad"
WriteRegStr HKLM "Software\Notpad\Capabilities\FileAssociations" ".txt" "Notpad.TextFile"

WriteRegStr HKLM "Software\RegisteredApplications" "Notpad" "Software\Notpad\Capabilities"
SetRegView 32

!insertmacro MUI_INSTALLOPTIONS_READ $R1 "notpad.ini" "Field 3" "State"
${If} $R1 == 1
AppAssocReg::SetAppAsDefault "Notpad" ".txt" "file"
Pop $0
MessageBox MB_ICONINFORMATION|MB_OK "SetAppAsDefault Result = $0"
${EndIf}
FunctionEnd

It seems to associate the type correctly, I can right-click .txt files and Notpad appears in the "Open with" submenu. The problem is the SetAppAsDefault function always returns "method failed". I don't know if I'm using it properly the documentation on the plugin page is a little confusing.

I looked in the registry and everything looks good, except it creates 2 copies of the RegisteredApplications\Notpad key. One in HKLM\Software and one in HKLM\Software\Wow6432Node, even when I use SetRegView 64 in the script. Maybe this is whats causing SetAppAsDefault to fail. Am I doing something wrong?


Try to contact the plugin's author ?

Anybody can list his plugin on http://nsis.sourceforge.net/
I'm not sure the author reads this forum regularly.


Well I've done a few tests and noticed something strange...
First I created a managed wrapper around the IApplicationAssociationRegistration COM interface, and tried using it in my application.

http://pastebin.com/L5xfjdij

private void SetDefault()
{
var aar = new ApplicationAssociationRegistration();
var iaar = (IApplicationAssociationRegistration)aar;
string currentDefault = iaar.QueryCurrentDefault(".txt", Global.AssociationType.AT_FILEEXTENSION, Global.AssociationLevel.AL_EFFECTIVE);
if (currentDefault != "Notpad.TextFile") iaar.SetAppAsDefault("Notpad", ".txt", AssociationType.AT_FILEEXTENSION);
}

From c# everything works fine, QueryCurrentDefault returns "txtfile", then it sets the default to my app. But when I try the same code in my script using the plugin:

AppAssocReg::QueryCurrentDefault ".txt" "file" "effective"
Pop $0
${If} $0 != "Notpad.TextFile"
AppAssocReg::SetAppAsDefault "Notpad" ".txt" "file"
Pop $1
MessageBox MB_ICONINFORMATION|MB_OK "SetAppAsDefault Result: $1"
${EndIf}

QueryCurrentDefault works, but SetAppAsDefault still returns "method failed". The strange thing is when I try other applications and association types like "Internet Explorer" "http" "protocol", the function succeeds and sets the default.

Has anyone else used this plugin before or the IApplicationAssociationRegistration api in their script? I don't understand why it works for other apps but not mine :(


Would it make a difference if Notepad were spelled correctly?


Notpad is the name of my application. It's a replacement for notepad with more features like tabs, desktop composition and windows 7 jumplists. To replace notepad as the default text editor I need to use IApplicationAssociationRegistation api in my nsis script.

The creator of the plugin says it works 100% on vista/7. The api works fine when I run it from c#, it only fails when I run it from my script using the plugin.


After trying some more tests the error the function is really returning is HRESULT 0x80070002 (file not found). I get the same error from c# so the plugin seems to be working fine. I still can't figure out whats causing it to fail, I'll keep trying I guess :(

If anyone knows what is causing this error please let me know!


Sorry for double posting, but I noticed something else very strange...

SetAppAsDefault fails when called from the installer
SetAppAsDefault fails when called from Notpad.vshost.exe (Debug mode)
SetAppAsDefault succeeds when called from Notpad.exe (Release mode)

It appears the function only succeeds if the process is open, but that still doesn't explain why it works for other programs like ie.

I guess my only option is to launch Notpad.exe silently from the installer and set the default that way. I still need a way to reset the default back to notepad when Notpad is uninstalled.