.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Tag Archives: Installer

How to indicate the remote host’s os


This post introduce how to find the remote machine(s) is Window or Linux in the system UI – Data Server – Equipment Service. In this case, if remote DS is Windows, you have to install Cygwin which simulate Linux environment on Windows, so you can execute the same command for both platform. After that, you will use plink application to get the output of command uname into a file, we check the file out and there is some key word for every operation system like: Linux (Linux) or Cygwin (Windows)
Read more of this post

Get size on disk of package in nsis


In case you want to deploy package to remote host. That means the installer can install on remote machine. You should pack the installation component into package may be tar.gz. So, in the component page the Space required control will show the wrong required space for hard disk. In this case, you should calculate the size of the package yourself and display in the control. Since, we get two things to do here: Get the actual size on disk of package, and display it on the Space required control which is instructed in the next post.

Get the space
One of the solution is to use another application named 7za.exe.
+ From nsis we will execute the application and write out the output contains the size on disk of the package.
+ Read the content of text file and get the size on disk you need.
Read more of this post

How to customize the default page in NSIS


As you know, NSIS supports user with default pages with fixed controls on its UI. So, the problem is how we can customize those page with our own control. This is impossible on NSIS2, upgrade to NSIS 3, we got the MUI2 instead of MUI. Hence, we can customize the default pages.

MUI2 supports the macros to customize pages as flowing point:

MUI_CUSTOMFUNCTION_GUIINIT function
MUI_CUSTOMFUNCTION_UNGUIINIT function
MUI_CUSTOMFUNCTION_ABORT function
MUI_CUSTOMFUNCTION_UNABORT function
MUI_CUSTOMFUNCTION_ONMOUSEOVERSECTION function
MUI_CUSTOMFUNCTION_UNONMOUSEOVERSECTION function

MUI_PAGE_CUSTOMFUNCTION_PRE function
MUI_PAGE_CUSTOMFUNCTION_SHOW function
MUI_PAGE_CUSTOMFUNCTION_LEAVE function

where can we put those macros?
The answer is before the default macros.
Example:

!insertmacro MUI_PAGE_CUSTOMFUNCTION_PRE func_ComponentPage_Pre
!insertmacro MUI_PAGE_COMPONENTS
Function func_ComponentPage_Pre
; do the stuff to pre-create the component page
FunctionEnd

NSIS script flow chart


After many hour research and test, i found that the running flow of a NSIS script like below;

Flow chart

Simply control selected components in NSIS component page


This is really a simple way to control the constraint between component in the NSIS component page

Function .onSelChange
	Pop $0
	${Switch} $0
		${Case} 0
			SectionSetFlags ${SEC_GUI} 0
			SectionSetFlags ${SEC_CONS} 1
			${Break}
		${Case} 1
			SectionSetFlags ${SEC_DS} 0
			SectionSetFlags ${SEC_SIM_MACH4} 0
			SectionSetFlags ${SEC_SIM_PM1} 0
			SectionSetFlags ${SEC_SIM_PM2} 0
			SectionSetFlags ${SEC_SIM_PM3} 0
          
			SectionSetFlags ${SEC_MACH4} 0
			SectionSetFlags ${SEC_PM1} 0
			SectionSetFlags ${SEC_PM2} 0
			SectionSetFlags ${SEC_PM3} 0
			SectionSetFlags ${SEC_CONS} 0
			${Break}
		${Case} 2
			SectionSetFlags ${SEC_DS} 1
			SectionSetFlags ${SEC_GUI} 0
			
			SectionSetFlags ${SEC_MACH4} 0
			SectionSetFlags ${SEC_PM1} 0
			SectionSetFlags ${SEC_PM2} 0
			SectionSetFlags ${SEC_PM3} 0
			${Break}
		${Case} 3
			SectionSetFlags ${SEC_DS} 1
			SectionSetFlags ${SEC_GUI} 0
			
			SectionSetFlags ${SEC_MACH4} 0
			SectionSetFlags ${SEC_PM1} 0
			SectionSetFlags ${SEC_PM2} 0
			SectionSetFlags ${SEC_PM3} 0
			${Break}
		${Case} 4
			SectionSetFlags ${SEC_DS} 1
			SectionSetFlags ${SEC_GUI} 0
			
			SectionSetFlags ${SEC_MACH4} 0
			SectionSetFlags ${SEC_PM1} 0
			SectionSetFlags ${SEC_PM2} 0
			SectionSetFlags ${SEC_PM3} 0
			${Break}
		${Case} 5
		${Case} 6
		${Case} 7
		${Case} 8
			SectionSetFlags ${SEC_DS} 1
			SectionSetFlags ${SEC_GUI} 0
			SectionSetFlags ${SEC_CONS} 0
			${Break}
		${Case} 9
		${Case} 10
		${Case} 11
		${Case} 12
		${Case} 13
		${Case} 14
			SectionSetFlags ${SEC_DS} 1
			SectionSetFlags ${SEC_GUI} 0
			${Break}
	${EndSwitch}
FunctionEnd

How to execute batch file and read the out stream in NSIS


Plugin ExecDos will help us to do the title’s content easier.
First, we normally create an nsis wizard page like this

Plugin ExecDos will help us to do the title's content easier.
First, we normally create an nsis wizard page like this
Var hCtl_test
Var hCtl_test_ProgressBar1
Var hCtl_test_TextBox1
Var hCtl_test_Button1

Function fnc_test_Create
  
	; === test (type: Dialog) ===
	nsDialogs::Create 1018
	Pop $hCtl_test
	${If} $hCtl_test == error
		Abort
	${EndIf}
	!insertmacro MUI_HEADER_TEXT "Dialog title..." "Dialog subtitle..."
  
	; === ProgressBar1 (type: ProgressBar) ===
	${NSD_CreateProgressBar} 8u 7u 280u 13u ""
	Pop $hCtl_test_ProgressBar1
	SetCtlColors $hCtl_test_ProgressBar1 0x3399FF 0xF0F0F0
  
	; === TextBox1 (type: TextMultiline) ===
	nsDialogs::CreateControl EDIT ${DEFAULT_STYLES}|${ES_AUTOHSCROLL}|${ES_AUTOVSCROLL}|${ES_MULTILINE}|${ES_WANTRETURN}|${WS_HSCROLL}|${WS_VSCROLL} ${WS_EX_WINDOWEDGE}|${WS_EX_CLIENTEDGE} 8u 23u 280u 84u ""
	Pop $hCtl_test_TextBox1
	SendMessage $hCtl_test_TextBox1 ${ES_MULTILINE} 0 0
  
	; === Button1 (type: Button) ===
	${NSD_CreateButton} 238u 111u 49u 13u "Button1"
	Pop $hCtl_test_Button1
	${NSD_OnClick} $hCtl_test_Button1 onButtonClick
  
FunctionEnd

; dialog show function
Function fnc_test_Show
	Call fnc_test_Create
	nsDialogs::Show $hCtl_test
FunctionEnd
Function appendText
	Pop $0
	MessageBox MB_OK $0
FunctionEnd

Function onButtonClick
	GetFunctionAddress $R1 appendText
	ExecDos::exec /ASYNC /TOFUNC "batfile.bat" $R1
FunctionEnd

Section flag in NSIS script


1. Flag
The flag is just a 32 bit integer. The below table assembles every bit in it

The first bit (lowest) represents whether the section is currently selected
The second bit represents whether the section is a section group (don’t modify this unless you really know what you are doing)
The third bit represents whether the section is a section group end (again, don’t modify)
The fourth bit represents whether the section is shown in bold or not
The fifth bit represents whether the section is read-only
The sixth bit represents whether the section group is to be automatically expanded
The seventh bit is set for section groups which are partially selected
The eighth bit is internally used for partially selected section group toggling and the ninth bit is used for reflecting section name changes
The error flag will be set if an out of range section is specified.
Each flag has a name, prefixed with `SF_`:

!define SF_SELECTED 1
!define SF_SECGRP 2
!define SF_SECGRPEND 4
!define SF_BOLD 8
!define SF_RO 16
!define SF_EXPAND 32
!define SF_PSELECTED 64
2. The Section Flag
The section flag is a combination of the flags in a section. The section flag is still a 32 bit integer. To create the section flag, we use the OR (|) operation on all the flags in binary format.
0000…00001 | 0000…00010…
For example: we have a section with 2 flags like this:

Section test test_section_id
     SectionIn RO
SectionEnd

Here we have the flag SF_SECLECTED (which value is 1) and the flag SF_RO (which value is 16). So, we have the section flag will be:
0000…0001 | 0000…1 0000 = 0000…1 0001 (that is 17 in decimal format.
Since, when we get the section flag via the SectionGetFlags instruction we will have the value 17 (actually, it’s in binary format)

SectionGetFlags test_section_id

3. Check for a flag in section flag
Now, simple instance below will show how to test a flag in section flag. We have a simple section

Section testSection testSectionId
    SectionIn RO
SectionEnd

The problem is how to indicate that the above section is selected or not. We have 3 steps have to be done:
– Get the section flag an store in a variable
– Process that flag with the operation binary AND (&) and store in any variable (clear any other flag out of the section flag)
– Compare the variable in second step with the flag you want to know (the SF_SELECTED in this case)

SectionGetFlags testSectionId $0
IntOp $0 $0 & SF_SELECTED
IntCmp $0 SF_SELECTED IS_SELECTED NOT_SELECTED
IS_SELECTED;
; do some stuff when selected
NOT_SELECTED:
; do some otherwise stuff

Why don’t we use just a comparation?
Because in the section flag there are still other flags, so if we compare it when not clear the other ones, the comparation operation will be able to be wrong since we need the accurate binary number.

%d bloggers like this: