NSIS plugin example

Share your advanced PureBasic knowledge/code with the community.
PeterH
User
User
Posts: 28
Joined: Sun Apr 11, 2010 11:01 am

NSIS plugin example

Post by PeterH »

Noticed that there's a "half-done" example to be found in the forum (thanks for the jumpstart, sepp!) so I tinkered a little and managed to accomplish this to fully support the return values as well in NSIS:

Code: Select all

; Minimal PureBasic NSIS plugin for stack handling test
; In this example compiled with /UNICODE and as x86.
; Filename: TestPlugin.dll

Structure stack_t
  *Next.stack_t
  text.s{1024}
EndStructure

Procedure.s PopString(*stacktop_pointer)
  Protected result.s = ""
  Protected *stacktop.stack_t = PeekI(*stacktop_pointer)
  If *stacktop
    result = PeekS(@*stacktop\text, 1024)
    PokeI(*stacktop_pointer, *stacktop\Next)
    GlobalFree_(*stacktop)
  EndIf
  ProcedureReturn result
EndProcedure

Procedure PushString(str.s, *stacktop_pointer)
  Protected *new.stack_t = GlobalAlloc_(#GMEM_FIXED, SizeOf(stack_t))
  If *new
    PokeS(@*new\text, str, 1024)
    *new\Next = PeekI(*stacktop_pointer)
    PokeI(*stacktop_pointer, *new)
  EndIf
EndProcedure

ProcedureCDLL TestPlugin(hwndParent, string_size, *variables, *stacktop, *extra)
  Protected *stacktop_pointer = *stacktop
  Protected param.s = PopString(*stacktop_pointer)
  MessageRequester("TestPlugin", param)
  PushString("42", *stacktop_pointer)
EndProcedure 

Code: Select all

; Minimal NSIS script to test the PureBasic TestPlugin DLL
; Assuming the TestPlugin.dll has been placed in the NSIS\Plugins\x86-unicode folder

Unicode True

Name "TestPlugin Test"
OutFile "TestPluginTest.exe"

Section "Test TestPlugin"
  DetailPrint "Calling TestPlugin with parameter: HelloFromNSIS"
  TestPlugin::TestPlugin "HelloFromNSIS"
  Pop $0
  DetailPrint "TestPlugin result: $0"
  ; Will output "42", as a string since all return values in NSIS are strings
SectionEnd 
I hope it might help someone at some point. I use it because the multisz support for the registry it horrible to work with in NSIS, even if using REG_MULTI_SZ_Reader and REG_MULTI_SZ_Editor
Quin
Addict
Addict
Posts: 1128
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: NSIS plugin example

Post by Quin »

Whoa, now that is cool. Thanks for sharing!
PeterH
User
User
Posts: 28
Joined: Sun Apr 11, 2010 11:01 am

Re: NSIS plugin example

Post by PeterH »

Quin wrote: Tue May 20, 2025 2:00 pm Whoa, now that is cool. Thanks for sharing!
My pleasure. Happy to contribute. In fact so happy that it took me less than 10 minutes from testing it to posting it. ;)

If you want to compete with the sizes of other NSIS-plugins, try a version <4 or PureBasic and you should get a really small DLL. For me the 100-150kb addition to an installer that takes up 6-10MB is not an issue, but for smaller apps it might be of interest. Beware of the risk that some antivirus goes bonkers though.
Post Reply