home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Programming / nsis-2.46-setup.exe / Examples / System / SysFunc.nsh < prev    next >
Encoding:
Text File  |  2007-12-01  |  10.3 KB  |  398 lines

  1. ; Some useful functions based on System plugin
  2. ; (c) brainsucker, 2002
  3. ; (r) BSForce
  4.  
  5. ; Check for double includes
  6. !ifndef SysFunc.NSH.Included
  7. !define SysFunc.NSH.Included
  8.  
  9. !include "${NSISDIR}\Examples\System\System.nsh"
  10.  
  11. !verbose 3      ; For WinMessages especially
  12.   !include "WinMessages.nsh"
  13. !verbose 4
  14.  
  15. ; ================= GetInstallerExeName implementation =================
  16.  
  17. ; Adopted Get Parameter function -> now it gets full installer.exe path
  18. ; input - nothing, output -> full path at the top of the stack
  19. Function GetInstallerExeName
  20.    Push $R0
  21.    Push $R1
  22.    Push $R2
  23.    StrCpy $R0 $CMDLINE 1
  24.    StrCpy $R1 '"'
  25.    StrCpy $R2 1
  26.    StrCmp $R0 '"' loop
  27.      StrCpy $R1 ' ' ; we're scanning for a space instead of a quote
  28.    loop:
  29.      StrCpy $R0 $CMDLINE 1 $R2
  30.      StrCmp $R0 $R1 loop2
  31.      StrCmp $R0 "" loop2
  32.      IntOp $R2 $R2 + 1
  33.      Goto loop
  34.    loop2:
  35.  
  36.    ; Ok, have we found last exename character or string end?
  37.    StrCmp $R0 "" "" +2
  38.         IntOp $R2 $R2 - 1       ; last exename char
  39.    StrCmp $R1 ' ' +3    ; was first character the '"', or something other?
  40.         StrCpy $R1 1    ; it was quote
  41.         Goto +2
  42.         StrCpy $R1 0    
  43.    IntOp $R2 $R2 - $R1
  44.    StrCpy $R0 $CMDLINE $R2 $R1  
  45.  
  46.    SearchPath $R0 $R0      ; expand file name to full path
  47.  
  48.    Pop $R2
  49.    Pop $R1
  50.    Exch $R0
  51. FunctionEnd
  52.  
  53. ; ================= systemGetFileSysTime implementation =================
  54.  
  55. !macro smGetFileSysTime FILENAME
  56.         Push ${FILENAME}
  57.         Call systemGetFileSysTime
  58.         Pop  $R0
  59. !macroend
  60.  
  61. ; -----------------------------------------------------------------
  62. ; systemGetFileSysTime (params on stack):
  63. ;       FILENAME        -       name of file to get file time
  64. ; returns to stack (SYSTEMTIME struct addr)
  65. ; -----------------------------------------------------------------
  66.  
  67. ; uses original method from NSIS
  68. Function systemGetFileSysTime
  69.     System::Store "s r1"
  70.  
  71.     StrCpy $R0 0     
  72.  
  73.     ; create WIN32_FIND_DATA struct
  74.     System::Call '*${stWIN32_FIND_DATA} .r2'
  75.  
  76.     ; Find file info
  77.     System::Call '${sysFindFirstFile}(r1, r2) .r3'
  78.  
  79.     ; ok?
  80.     IntCmp $3 ${INVALID_HANDLE_VALUE} sgfst_exit
  81.  
  82.     ; close file search
  83.     System::Call '${sysFindClose}(r3)'
  84.  
  85.     ; Create systemtime struct for local time
  86.     System::Call '*${stSYSTEMTIME} .R0'
  87.  
  88.     ; Get File time
  89.     System::Call '*$2${stWIN32_FIND_DATA} (,,, .r3)'
  90.  
  91.     ; Convert file time (UTC) to local file time
  92.     System::Call '${sysFileTimeToLocalFileTime}(r3, .r1)'
  93.  
  94.     ; Convert file time to system time
  95.     System::Call '${sysFileTimeToSystemTime}(r1, R0)'
  96.  
  97. sgfst_exit:
  98.     ; free used memory for WIN32_FIND_DATA struct
  99.     System::Free $2    
  100.  
  101.     System::Store "P0 l"
  102. FunctionEnd
  103.  
  104. ; ================= systemMessageBox implementation =================
  105.  
  106. ; return to $R0
  107. !macro smMessageBox MODULE MSG CAPTION STYLE ICON
  108.      Push "${ICON}"
  109.      Push "${STYLE}"
  110.      Push "${CAPTION}"
  111.      Push "${MSG}"
  112.      Push "${MODULE}"
  113.      Call systemMessageBox
  114.      Pop $R0
  115. !macroend
  116.  
  117. ; -----------------------------------------------------------------
  118. ; systemMessageBox (params on stack):
  119. ;       Module: either handle ("i HANDLE", HANDLE could be 0) or "modulename" 
  120. ;       Msg: text of message
  121. ;       Caption: caption of message box window
  122. ;       Style: style, buttons etc
  123. ;       Icon: either icon handle ("i HANDLE") or resource name 
  124. ; returns to stack
  125. ; -----------------------------------------------------------------
  126. Function systemMessageBox
  127.      System::Store "s r2r3r4r5r6"
  128.  
  129.      ; may be Module is module handle?
  130.      StrCpy $1 $2
  131.      IntCmp $1 0 0 smbnext smbnext
  132.  
  133.      ; Get module handle
  134.      System::Call '${sysGetModuleHandle}($2) .r1'
  135.      IntCmp $1 0 loadlib libnotloaded libnotloaded
  136.  
  137. loadlib:
  138.      ; Load module and get handle
  139.      System::Call '${sysLoadLibrary}($2) .r1'
  140.      IntCmp $1 0 0 smbnext smbnext
  141.  
  142. libnotloaded:
  143.      ; Indicate that LoadLibrary wasn't used
  144.      StrCpy $2 1
  145.  
  146. smbnext:
  147.      ; Create MSGBOXPARAMS structure
  148.      System::Call '*${stMSGBOXPARAMS}(, $HWNDPARENT, r1, r3, r4, "$5|${MB_USERICON}", $6, _) .r0'
  149.      ; call MessageBoxIndirect
  150.      System::Call '${sysMessageBoxIndirect}(r0) .R0'
  151.      ; free MSGBOXPARAMS structure
  152.  
  153.      System::Free $0
  154.  
  155.      ; have we used load library at start?
  156.      IntCmp $2 0 0 smbskipfree smbskipfree
  157.      ; No, then free the module
  158.      System::Call '${sysFreeLibrary}(r1)'
  159.  
  160. smbskipfree:
  161.      System::Store "P0 l"   
  162. FunctionEnd
  163.  
  164. ; ================= systemSplash implementation =================
  165.  
  166. ; returns to $R0
  167. !macro smSystemSplash DELAY FILE
  168.     Push ${FILE}
  169.     Push ${DELAY}
  170.     call systemSplash
  171.     Pop $R0    
  172. !macroend
  173.  
  174. ; -----------------------------------------------------------------
  175. ; systemSplash (params on stack):
  176. ;       Delay - time in ms to show the splash
  177. ;       File - bitmap (& audio) file name (without extension)
  178. ; returns to stack
  179. ; -----------------------------------------------------------------
  180.  
  181. Function _systemSplashWndCB
  182.    ; Callback receives 4 values
  183.    System::Store "s r2r5r7r9"
  184.  
  185.    ; Message branching
  186.    IntCmp $5 ${WM_CLOSE} m_Close
  187.    IntCmp $5 ${WM_TIMER} m_Timer
  188.    IntCmp $5 ${WM_LBUTTONDOWN} m_Lbtn
  189.    IntCmp $5 ${WM_CREATE} m_Create
  190.    IntCmp $5 ${WM_PAINT} m_Paint
  191.    goto default
  192.  
  193. m_Create:
  194.    ; Create structures
  195.    System::Call "*${stRECT} (_) .R8"
  196.    System::Call "*${stBITMAP} (_, &l0 .R7) .R9"
  197.  
  198.    ; Get bitmap info
  199.    System::Call "${sysGetObject} (r6, R7, R9)" 
  200.    
  201.    ; Get desktop info
  202.    System::Call "${sysSystemParametersInfo} (${SPI_GETWORKAREA}, 0, R8, 0)" 
  203.  
  204.    ; Style (callbacked)
  205.    System::Call "${sysSetWindowLong} (r2, ${GWL_STYLE}, 0) .s" 
  206.    !insertmacro SINGLE_CALLBACK 5 $R7 1 _systemSplashWndCB
  207.  
  208.    ; Calculate and set window pos
  209.  
  210.    ; Get bmWidth(R2) and bmHeight(R3)
  211.    System::Call "*$R9${stBITMAP} (,.R2,.R3)"
  212.    ; Get left(R4), top(R5), right(R6), bottom(R7)
  213.    System::Call "*$R8${stRECT} (.R4,.R5,.R6,.R7)"
  214.  
  215.    ; Left pos
  216.    IntOp $R0 $R6 - $R4
  217.    IntOp $R0 $R0 - $R2
  218.    IntOp $R0 $R0 / 2
  219.    IntOp $R0 $R0 + $R4
  220.  
  221.    ; Top pos
  222.    IntOp $R1 $R7 - $R5
  223.    IntOp $R1 $R1 - $R3
  224.    IntOp $R1 $R1 / 2
  225.    IntOp $R1 $R1 + $R5
  226.  
  227.    System::Call "${sysSetWindowPos} (r2, 0, R0, R1, R2, R3, ${SWP_NOZORDER}) .s" 
  228.    !insertmacro SINGLE_CALLBACK 6 $R7 1 _systemSplashWndCB
  229.  
  230.    ; Show window
  231.    System::Call "${sysShowWindow} (r2, ${SW_SHOW}) .s" 
  232.    !insertmacro SINGLE_CALLBACK 7 $R7 1 _systemSplashWndCB
  233.  
  234.    ; Set Timer
  235.    System::Call "${sysSetTimer} (r2, 1, r8,)"
  236.  
  237.    ; Free used memory
  238.    System::Free $R8
  239.    System::Free $R9
  240.  
  241.    StrCpy $R0 0
  242.    goto exit
  243.  
  244. m_Paint:
  245.    ; Create structures
  246.    System::Call "*${stRECT} (_) .R8"
  247.    System::Call "*${stPAINTSTRUCT} (_) .R9"
  248.  
  249.    ; Begin Paint
  250.    System::Call "${sysBeginPaint} (r2, R9) .R7"
  251.  
  252.    ; CreateCompatibleDC
  253.    System::Call "${sysCreateCompatibleDC} (R7) .R6"
  254.  
  255.    ; GetClientRect
  256.    System::Call "${sysGetClientRect} (r2, R8)"
  257.   
  258.    ; Select new bitmap
  259.    System::Call "${sysSelectObject} (R6, r6) .R5"
  260.  
  261.    ; Get left(R0), top(R1), right(R2), bottom(R3)
  262.    System::Call "*$R8${stRECT} (.R0,.R1,.R2,.R3)"
  263.      
  264.    ; width=right-left  
  265.    IntOp $R2 $R2 - $R0
  266.    ; height=bottom-top
  267.    IntOp $R3 $R3 - $R1
  268.  
  269.    System::Call "${sysBitBlt} (R7, R0, R1, R2, R3, R6, 0, 0, ${SRCCOPY})" 
  270.  
  271.    ; Select old bitmap
  272.    System::Call "${sysSelectObject} (R6, R5)"
  273.    
  274.    ; Delete compatible DC
  275.    System::Call "${sysDeleteDC} (R6)"
  276.  
  277.    ; End Paint
  278.    System::Call "${sysEndPaint} (r2, R9)"
  279.  
  280.    ; Free used memory
  281.    System::Free $R8
  282.    System::Free $R9
  283.  
  284.    StrCpy $R0 0
  285.    goto exit
  286.  
  287. m_Timer:
  288. m_Lbtn:
  289.    StrCpy $4 0
  290.    IntCmp $5 ${WM_TIMER} destroy
  291.         StrCpy $4 1
  292.  
  293. destroy:
  294.    System::Call "${sysDestroyWindow} (r2) .s"
  295.    !insertmacro SINGLE_CALLBACK 12 $R4 1 _systemSplashWndCB
  296.  
  297. default:
  298.    ; Default
  299.    System::Call "${sysDefWindowProc} (r2, r5, r7, r9) .s"
  300.    !insertmacro SINGLE_CALLBACK 14 $R0 1 _systemSplashWndCB
  301.    goto exit
  302.  
  303. m_Close:
  304.    StrCpy $R0 0
  305.    goto exit
  306.  
  307. exit:
  308.    ; Restore
  309.    System::Store "p4P0 l R0r4"
  310.  
  311.    ; Return from callback
  312.    System::Call "$3" $R0
  313. FunctionEnd
  314.  
  315. Function systemSplash
  316.  
  317.    ; Save registers and get input 
  318.    System::Store "s r8r9"
  319.  
  320.    ; Get module instance
  321.    System::Call "${sysGetModuleHandle} (i) .r7"
  322.  
  323.    ; Get arrow cursor
  324.    System::Call "${sysLoadCursor} (0, i ${IDC_ARROW}) .R9" 
  325.  
  326.    ; Get callback
  327.    System::Get "${sysWNDPROC}"
  328.    Pop $3
  329.  
  330.    ; Create window class
  331.    System::Call "*${stWNDCLASS} (0,r3,0,0,r7,0,R9,0,i 0,'_sp') .R9"
  332.  
  333.    ; Register window class
  334.    System::Call "${sysRegisterClass} (R9) .R9"
  335.    IntCmp $R9 0 errorexit ; Class registered ok?
  336.  
  337.    ; Load Image (LR_CREATEDIBSECTION|LR_LOADFROMFILE = 0x2010)
  338.    System::Call '${sysLoadImage} (, s, ${IMAGE_BITMAP}, 0, 0, ${LR_CREATEDIBSECTION}|${LR_LOADFROMFILE}) .r6' "$9.bmp"
  339.    IntCmp $6 0 errorexit        ; Image loaded ok?
  340.  
  341.    ; Start the sound (SND_ASYNC|SND_FILENAME|SND_NODEFAULT = 0x20003)
  342.    System::Call "${sysPlaySound} (s,,${SND_ASYNC}|${SND_FILENAME}|${SND_NODEFAULT})" "$9.wav" 
  343.  
  344.    ; Create window
  345.    System::Call "${sysCreateWindowEx} (${WS_EX_TOOLWINDOW}, s, s,,,,,, $HWNDPARENT,,r7,) .s" "_sp" "_sp" 
  346.    !insertmacro SINGLE_CALLBACK 1 $5 1 _systemSplashWndCB
  347.  
  348.    ; Create MSG struct
  349.    System::Call "*${stMSG} (_) i.R9"
  350.  
  351.    ; -------------------------
  352. repeat:
  353.         ; Check for window
  354.         System::Call "${sysIsWindow} (r5) .s"
  355.         !insertmacro SINGLE_CALLBACK 2 $R8 1 _systemSplashWndCB
  356.         IntCmp $R8 0 finish
  357.  
  358.         ; Get message
  359.         System::Call "${sysGetMessage} (R9, r5,_) .s"
  360.         !insertmacro SINGLE_CALLBACK 3 $R8 1 _systemSplashWndCB
  361.         IntCmp $R8 0 finish
  362.  
  363.         ; Dispatch message
  364.         System::Call "${sysDispatchMessage} (R9) .s"
  365.         !insertmacro SINGLE_CALLBACK 4 $R8 1 _systemSplashWndCB
  366.  
  367.         ; Repeat dispatch cycle
  368.         goto repeat
  369.    ; -------------------------
  370.  
  371. finish:
  372.    ; Stop the sound
  373.    System::Call "${sysPlaySound} (i 0, i 0, i 0)"
  374.  
  375.    ; Delete bitmap object
  376.    System::Call "${sysDeleteObject} (r6)"
  377.  
  378.    ; Delete the callback queue
  379.    System::Free $3
  380.  
  381.    ; Dialog return
  382.    StrCpy $R0 $4
  383.    goto exit
  384.  
  385. ; Exit in case of error
  386. errorexit:
  387.    StrCpy $R0 -1
  388.    goto exit
  389.  
  390. exit:
  391.    ; Restore register and put output
  392.    System::Store "P0 l"
  393. FunctionEnd
  394.  
  395. !verbose 4
  396.  
  397. !endif