- NSIS Discussion
 - Cant get macro to run!
 
Archive: Cant get macro to run!
starfighter5
8th December 2010 15:50 UTC
Cant get macro to run!
Hi all,
I have been battling for a while with this one and have decided to strip it back to basics....
I have been following the page at http://nsis.sourceforge.net/Macro_vs_Function
In my Function.onInit I have....
!macro Hello
  DetailPrint "Hello world"
!macroend
      
      Then I have.....
      
      
Function tester
!insertmacro Hello
FunctionEnd
      
      Lower down in my code, when I run it it doesn't display the Hellow World box, it's probably me being daft but can anyone point out my error?
    
 
    
    
      LoRd_MuldeR
      8th December 2010 15:53 UTC
      Why do you define in the macro in your .onInit function?
      
      Usually you would define the macro before all your functions and sections. And then insert the macro in some function or section.
      
      (Just a side note: Macros don't "run". They are inserted/expanded in-place, at the moment when your installer is built)
     
    
    
      starfighter5
      8th December 2010 16:02 UTC
      Hi,
      
      I put the macro there as I couldn't find anywhere to tell me where to put it! I have now put it outside of my oninit so it looks like this....
      
      
;Page Variables
Function .onInit
!insertmacro MUI_INSTALLOPTIONS_EXTRACT_AS "intro.ini" "intro"
!insertmacro MUI_INSTALLOPTIONS_EXTRACT_AS "softselect.ini" "softselect"
FunctionEnd
!macro Hello
  DetailPrint "Hello world"
!macroend
      
      It still is not displaying the message though :(
    
 
    
    
      LoRd_MuldeR
      8th December 2010 16:07 UTC
      Okay, let me make this clear again:
      
      1. Define your macro using "!macro ... !macroend" before anything else and outside any function/section.
      
      2. Then insert your macro, using "!insertmacro" at the desired place, which can be in a function or a section, for example.
      
      3. Inserting a macro basically means that the macro code will be copied to that place.
      
      4. If you inserted the macro into some function, that code won't execute until the enclosing function is called. If you inserted the macro into some section, that code won't be executed until the enclosing section is executed.
      
      
      Try like:
      
      
OutFile test.exe
Name "Test"
!macro Hello
  MessageBox MB_OK "Hello world"
!macroend
Function .onInit
  !insertmacro Hello
FunctionEnd
Section
  [Your installer section here]
SectionEnd
      
      
      Which is 100% equivalent to:
      
      
Function .onInit
  MessageBox MB_OK "Hello world"
FunctionEnd
Section
  [Your installer section here]
SectionEnd
     
    
    
      starfighter5
      8th December 2010 16:29 UTC
      Thanks for clarifying things for me, seems I am getting mixed up with commands
      
      
  MessageBox MB_OK "Hello world"
      
      VS
      
      
DetailPrint "Hello world"
      
      I was expecting Hello World to pop up in a box with DetailPrint, which it obviously won't do.
      
      Thanks for steering me in the right direction :)
    
 
    
    
      LoRd_MuldeR
      8th December 2010 16:39 UTC
      DetailPrint, as the name implies, writes to the installation details box...
     
    
    
      starfighter5
      8th December 2010 16:42 UTC
      My mistake.....
      
      On a secondary note, when using the code below
      
      
Function tester
!insertmacro test
FunctionEnd
      
      Gives me
      
      
install function "tester" not referenced - zeroing code out
      
      Do I need to reference the function elsewhere?
    
 
    
    
      LoRd_MuldeR
      8th December 2010 17:01 UTC
      Well, you insert (copy) your macro into a custom function named "tester" here.
      
      But apparently you never call that function!
      
      The special .onInit function is called automatically on startup, but "normal" functions are not.
      
      NSIS is smart and will "zero out" (ignore) functions that are never called ;)
      
      
      Try adding something like:
      
      
Section
  DetailPrint "Now calling the function..."
  Call tester
SectionEnd