Advertisement
Anders

COM Test / System+ShellCOM

Mar 13th, 2011
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Name "COM Test"
  2. OutFile "COM Test.exe"
  3. Caption "NSIS $(^Name)"
  4. RequestExecutionLevel user
  5. XPStyle On
  6.  
  7. /* Use specific AMUI or let windows auto detect?
  8. */!define MyAppUserModelId "Nullsoft.NSIS.Example.COM-Test" ;*/
  9. ;!define ForceAddRecentAUMI
  10. !define ShortcutName "NSIS COM Test.lnk"
  11. !define MyExt ".nsi"
  12. !define MyProgId "NSIS.COM-Test${MyExt}"
  13.  
  14. !include LogicLib.nsh
  15. !include nsDialogs.nsh
  16. !include WinCore.nsh
  17. !include Win\COM.nsh
  18. !include Win\Propkey.nsh
  19. !include WinVer.nsh
  20.  
  21. !define /ifndef SHCNE_ASSOCCHANGED 0x08000000
  22. !define /ifndef SHARD_PATHW 0x00000003
  23. !define /ifndef SHARD_APPIDINFOIDLIST 0x00000005
  24.  
  25. Section
  26. SectionEnd
  27.  
  28. Page custom PageCreate
  29.  
  30. Function .onInit
  31. !ifdef MyAppUserModelId
  32. System::Call 'SHELL32::SetCurrentProcessExplicitAppUserModelID(w "${MyAppUserModelId}")i'
  33. !endif
  34.  
  35. ${IfNot} ${AtLeastWinVista}
  36.     ;IStartMenuPinnedList is documented as Vista+
  37.     MessageBox mb_iconexclamation "This example is only useful on Vista+"
  38. ${EndIf}
  39. FunctionEnd
  40.  
  41. Function PageCreate
  42. GetDlgItem $0 $hwndparent 2
  43. ShowWindow $0 0 ;Hide cancel button
  44. nsDialogs::Create 1018
  45. Pop $0
  46.  
  47. ${NSD_CreateButton} 0 0 49% 13u "Create Startmenu shortcut"
  48. Pop $0
  49. ${NSD_OnClick} $0 CreateLnk
  50.  
  51. ${NSD_CreateButton} 51% 0 49% 13u "Delete Startmenu shortcut"
  52. Pop $0
  53. ${NSD_OnClick} $0 DeleteLnk
  54.  
  55. ${NSD_CreateButton} 0 15u 100% 13u "Register file type (${MyExt})"
  56. Pop $0
  57. ${NSD_OnClick} $0 FTReg
  58.  
  59. ${NSD_CreateButton} 0 30u 100% 13u "Create dummy recent item"
  60. Pop $0
  61. ${NSD_OnClick} $0 CreateDummyRecent
  62.  
  63. ${NSD_CreateButton} 0 45u 100% 13u "Unregister file type (${MyExt})"
  64. Pop $0
  65. ${NSD_OnClick} $0 FTUnReg
  66.  
  67. nsDialogs::Show
  68. FunctionEnd
  69.  
  70.  
  71. !macro RegSafeDeleteEmptyKey HKEY Path
  72. Push $1
  73. Push $0
  74. StrCpy $0 "${Path}"
  75. ReadRegStr $1 ${HKEY} $0 ""
  76. ${IfThen} $1 == "" ${|} EnumRegValue $1 ${HKEY} $0 0 ${|}
  77. ${IfThen} $1 == "" ${|} DeleteRegKey /IfEmpty ${HKEY} $0 ${|}
  78. Pop $0
  79. Pop $1
  80. !macroend
  81.  
  82.  
  83. Function FTUnReg
  84. Pop $0
  85. DeleteRegValue HKCU "Software\Classes\${MyExt}\OpenWithProgIds" "${MyProgId}"
  86. DeleteRegKey HKCU "Software\Classes\${MyProgId}"
  87. !insertmacro RegSafeDeleteEmptyKey HKCU "Software\Classes\${MyExt}\OpenWithProgIds"
  88. !insertmacro RegSafeDeleteEmptyKey HKCU "Software\Classes\${MyExt}"
  89. System::Call 'SHELL32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED},i,p,p)'
  90. FunctionEnd
  91.  
  92.  
  93.  
  94. Function FTReg
  95. Pop $0
  96. WriteRegBin HKCU "Software\Classes\${MyExt}\OpenWithProgIds" "${MyProgId}" ""
  97. !ifdef MyAppUserModelId
  98. WriteRegStr HKCU "Software\Classes\${MyProgId}" "AppUserModelId" "${MyAppUserModelId}"
  99. !endif
  100. ;Override icon? WriteRegStr HKCU "Software\Classes\${MyProgId}\DefaultIcon" "" '"$exepath"'
  101. WriteRegStr HKCU "Software\Classes\${MyProgId}\shell\open\command" "" '"$exepath" "%1"'
  102. System::Call 'SHELL32::SHChangeNotify(i ${SHCNE_ASSOCCHANGED},i,p,p)'
  103. FunctionEnd
  104.  
  105.  
  106.  
  107. Function CreateDummyRecent
  108. Pop $9
  109. EnableWindow $9 0
  110.  
  111. ReadRegDWORD $2 HKCU "Software\Classes\${MyProgId}" "NSIS-DummyCount"
  112. IntOp $2 $2 + 1
  113. WriteRegDWORD HKCU "Software\Classes\${MyProgId}" "NSIS-DummyCount" $2
  114.  
  115. StrCpy $1 "$desktop\NSIS COM Test $2${MyExt}"
  116.  
  117. FileOpen $0 "$1" a
  118. FileClose $0
  119. !ifdef MyAppUserModelId & ForceAddRecentAUMI
  120. System::Call 'SHELL32::ILCreateFromPath(w "$1")p.r2'
  121. ${If} $2 <> 0
  122.     System::Call '*(p $2,p,&w129 "${MyAppUserModelId}")p.r3' ;SHARDAPPIDINFOIDLIST+(WCHAR*)AppUserModelId
  123.     ${If} $3 <> 0
  124.         IntOp $4 ${__WIN_PTRSIZE} * 2
  125.         IntOp $4 $3 + $4
  126.         System::Call '*$3(p,p $4)'
  127.         System::Call 'SHELL32::SHAddToRecentDocs(i ${SHARD_APPIDINFOIDLIST},p $3)'
  128.         System::Free $3
  129.     ${EndIf}
  130.     System::Call 'SHELL32::ILFree(pr2)'
  131. ${EndIf}
  132. !else
  133. System::Call 'SHELL32::SHAddToRecentDocs(i ${SHARD_PATHW},w "$1")'
  134. !endif
  135.  
  136. Sleep 1500
  137. Delete $1
  138. EnableWindow $9 1
  139. FunctionEnd
  140.  
  141.  
  142.  
  143. !macro CreatePropVariantStrW str sysvaroutProp
  144. System::Call 'SHLWAPI::SHStrDup(t "${str}",*p.s)' ; TODO: Should really be error checking here!
  145. System::Call *${SYSSTRUCT_PROPVARIANT}(${VT_LPWSTR},,s)p.${sysvaroutProp}
  146. !macroend
  147.  
  148.  
  149. Function CreateLnk
  150. Pop $0
  151. CreateShortcut "$SMPrograms\${ShortcutName}" '"$EXEPATH"'
  152.  
  153. !ifdef MyAppUserModelId
  154. ;Set the AppUserModelId on our .lnk (We have no tasks to show in the jump list, but this is just a sample)
  155. !insertmacro ComHlpr_CreateInProcInstance ${CLSID_ShellLink} ${IID_IShellLink} r1 .r0
  156. ${If} $1 <> 0
  157.     ${IUnknown::QueryInterface} $1 '("${IID_IPersistFile}",.r2)'
  158.     ${If} $2 <> 0
  159.         ${IPersistFile::Load} $2 '("$SMPrograms\${ShortcutName}",${STGM_READWRITE}).r0'
  160.         ${If} $0 = 0
  161.             ${IUnknown::QueryInterface} $1 '("${IID_IPropertyStore}",.r3).r0'
  162.             ${If} $3 <> 0
  163.                 System::Call '*${SYSSTRUCT_PROPERTYKEY}(${PKEY_AppUserModel_ID})p.r4'
  164.                 ${If} $4 <> 0
  165.                     !insertmacro CreatePropVariantStrW "${MyAppUserModelId}" r5
  166.                     ${If} $5 <> 0
  167.                         ${IPropertyStore::SetValue} $3 (r4,r5)i.r0
  168.                         System::Call 'OLE32::PropVariantClear(pr5)'
  169.                         System::Free $5
  170.                         ${If} $0 = 0
  171.                             ${IPropertyStore::Commit} $3 ()i.r0
  172.                             ${If} $0 = 0
  173.                                 ${IPersistFile::Save} $2 '(p 0,1)i.r0' ;First parameter is w by default, we need to pass NULL!
  174.                             ${EndIf}
  175.                         ${EndIf}
  176.                     ${EndIf}
  177.                     System::Free $4
  178.                 ${EndIf}
  179.                 ${IUnknown::Release} $3 ""
  180.             ${EndIf}
  181.         ${EndIf}
  182.         ${IUnknown::Release} $2 ""
  183.     ${EndIf}
  184.     ${IUnknown::Release} $1 ""
  185. ${EndIf}
  186. !else
  187. StrCpy $0 0
  188. !endif
  189.  
  190. MessageBox mb_ok 'You can now pin the shortcut to the startmenu and taskbar...$\nHRESULT=$0'
  191. FunctionEnd
  192.  
  193.  
  194.  
  195. Function DeleteLnk
  196. Pop $0
  197. !insertmacro ComHlpr_CreateInProcInstance ${CLSID_StartMenuPin} ${IID_IStartMenuPinnedList} r2 .r0
  198. ${If} $2 <> 0
  199.     System::Call 'SHELL32::SHCreateItemFromParsingName(w "$SMPrograms\${ShortcutName}",p0,g "${IID_IShellItem}",*p.r1)i.r0'
  200.     ${If} $1 <> 0
  201.         ${IStartMenuPinnedList::RemoveFromList} $2 ($1).r0
  202.         ${IUnknown::Release} $1 ""
  203.     ${EndIf}
  204.     ${IUnknown::Release} $2 ""
  205. ${EndIf}
  206.  
  207. Delete "$SMPrograms\${ShortcutName}"
  208.  
  209. Messagebox mb_ok 'IStartMenuPinnedList::RemoveFromList: HRESULT=$0'
  210. FunctionEnd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement