Archive: Cant get macro to run!


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?

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)


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 :(

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

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 :)

DetailPrint, as the name implies, writes to the installation details box...


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?

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