
#DefineSubRoutine InitDialogConstants()
   ;DialogprocOptions Constants
   MSG_INIT=0                ; The one-time initialization
   MSG_TIMER=1               ; Timer event
   MSG_BUTTONPUSHED=2        ; Pushbutton or Picturebutton
   MSG_RADIOPUSHED=3         ; Radiobutton clicked
   MSG_CHECKBOX=4            ; Checkbox clicked
   MSG_EDITBOX=5             ; Editbox or Multilinebox
   MSG_FILESELECT=6          ; Filelistbox
   MSG_ITEMSELECT=7          ; Itembox
   MSG_COMBOCHANGE=8         ; Combobox/Droplistbox
   MSG_CALENDAR=9            ; Calendar date change
   MSG_SPINNER=10            ; Spinner number change
   MSG_CLOSEVIA49=11         ; Close clicked (Enabled via DialogProcOptions 1002
   MSG_FILEBOXDOUBLECLICK=12 ; Get double-click message on a FileListBox
   MSG_ITEMBOXDOUBLECLICK=13 ; Get double-click message on an ItemBox
   MSG_COMEVENT=14           ; COMCONTROL Event notification from DialogObject (NOT DialogProcOptions)

   DPO_DISABLESTATE=1000     ; codes -1=GetSetting 0=EnableDialog 1=DisableDialog
   DPO_CHANGEBACKGROUND=1001 ; -1=Get Current otherise bitmap or color string
   DPO_CHANGESYSMENU=1002    ; -1=Get Current 0=none 1=close 2=close/min 3=close/max 4=close/min/max 
   DPO_CHANGETITLE=1003      ; Set/Get Dialog Title - (-1 to get) 

   ;DialogControlState Constants
   DCSTATE_SETFOCUS=1        ; Give Control Focus
   DCSTATE_QUERYSTYLE=2      ; Query control's style
   DCSTATE_ADDSTYLE=3        ; Add control style
   DCSTATE_REMOVESTYLE=4     ; Remove control style
   DCSTATE_GETFOCUS=5        ; Get control that has focus
   DCSTATE_MOVEMOUSEOVER=6   ; Move the mouse over the control

   DCSTYLE_INVISIBLE=1       ; Set Control Invisible
   DCSTYLE_DISABLED=2        ; Set Control Disabled
   DCSTYLE_NOUSERDATA=4      ; Note: Setable via DialogControlState function ONLY SPINNER control only
   DCSTYLE_READONLY=8        ; Sets control to read-only (user cannot type in data) EDITBOX MULTILINEBOX SPINNER
   DCSTYLE_PASSWORD=16       ; Sets 'password mode' where only *'s are displayed EDITBOX
   DCSTYLE_DEFAULTBUTTON=32  ; Sets a button as the default button PUSHBUTTON PICTUREBUTTON
   DCSTYLE_DIGITSONLY=64     ; Set edit box to accept digits only EDITMOX MULTILINEBOX
   DCSTYLE_FLAT=128          ; Makes a 'flat' hyperlink-looking button PUSHBUTTON PICTUREBUTTON
   DCSTYLE_NOADJUST=256      ; Turns off auto-height adjustment  ITEMBOX FILELISTBOX
   DCSTYLE_TEXTCENTER=512    ; Center text in control VARYTEXT STATICTEXT
   DCSTYLE_TEXTRIGHT=1024    ; Flush-Right text in control VARYTEXT STATICTEXT
   DCSTYLE_NOSELCURLEFT=2048 ; No selection, cursor left EDITBOX MULTILINEBOX
   DCSTYLE_NOSELCURRIGHT=4096; No selection, cursor right EDITBOX MULTILINEBOX

   ;DialogControlSet / DialogControlGet Constants
   DC_CHECKBOX=1             ; CHECKBOX
   DC_RADIOBUTTON=2          ; RADIOBUTTON
   DC_EDITBOX=3              ; EDITBOX MULTILINEBOX
   DC_TITLE=4                ; PICTURE RADIOBUTTON CHECKBOX PICTUREBUTTON VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON
   DC_ITEMBOXCONTENTS=5      ; ITEMBOX FILELISTBOX DROPLISTBOX
   DC_ITEMBOXSELECT=6        ; ITEMBOX FILELISTBOX DROPLISTBOX
   DC_CALENDAR=7             ; CALENDAR
   DC_SPINNER=8              ; SPINNER
   DC_MULTITABSTOPS=9        ; MULTILINEBOX
   DC_ITEMSCROLLPOS=10       ; ITEMBOX FILELISTBOX
   DC_BACKGROUNDCOLOR=11     ; RADIOBUTTON CHECKBOX VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON ITEMBOX FILELISTBOX DROPLISTBOX SPINNER EDITBOX MULTILINEBOX
   DC_PICTUREBITMAP=12       ; PICTURE PICTUREBUTTON
   DC_TEXTCOLOR=13           ; RADIOBUTTON CHECKBOX VARYTEXT STATICTEXT GROUPBOX PUSHBUTTON ITEMBOX FIELLISTBOX DROPLISTBOX SPINNER EDITBOX MULTILINEBOX
   DC_ITEMBOXADD=14          ; ITEMBOX FILELISTBOX DROPLISTBOX
   DC_ITEMBOXREMOVE=15       ; ITEMBOX FILELISTBOX DROPLISTBOX
   DC_RADIOVALUE=16          ; RADIOBUTTON


   ;DialogObject constants
   DLGOBJECT_ADDEVENT=1      ; Call dialog callback when the specified event occurs
   DLGOBJECT_STOPEVENT=2     ; Stop calling dialog callback when an event previously requested with
   DLGOBJECT_GETOBJECT=3     ; Return an object references to the specified control
   DLGOBJECT_GETPICTURE=4    ; Create and return an object reference to a picture object

   ;Return code constants
   RET_DO_CANCEL=0           ; Cancels dialog
   RET_DO_DEFAULT= -1        ; Continue with default processing for control
   RET_DO_NOT_EXIT= -2       ; Do not exit the dialog
   return
#EndSubroutine



#DefineFunction SignCallbackProc(Sign_Handle,Sign_Message,Sign_ID,Sign_EventInfo,rsvd)
   InitDialogConstants()                                    ; Initialize Dialog Constants
   switch Sign_Message                                      ; Switch based on Dialog Message type
      case MSG_INIT                                         ; Standard Initialization message
         DialogProcOptions(Sign_Handle,MSG_BUTTONPUSHED,@TRUE)

			;Define ini file 
			ptr_ini = PtrGlobal( ini )

			;Read Ini File Settings
			filetosign = IniReadPvt( 'Settings', 'filetosign', '', *ptr_ini )
			friendlyname = IniReadPvt( 'Settings', 'friendlyname', '', *ptr_ini )
			desc = IniReadPvt( 'Settings', 'desc', '', *ptr_ini )
			url = IniReadPvt( 'Settings', 'url', '', *ptr_ini )
			timestamp = IniReadPvt( 'Settings', 'timestamp', '', *ptr_ini )
			;Initialize Edit box controls with data from ini
			if filetosign != '' then DialogControlSet( Sign_Handle, 006, DC_EDITBOX, filetosign )
			if friendlyname != '' then DialogControlSet( Sign_Handle, 013, DC_EDITBOX, friendlyname )
			if desc != '' then DialogControlSet( Sign_Handle, 008, DC_EDITBOX, desc )
			if url != '' then DialogControlSet( Sign_Handle, 010, DC_EDITBOX, url )
			if timestamp != '' then DialogControlSet( Sign_Handle, 012, DC_EDITBOX, timestamp )

         return(RET_DO_DEFAULT)

     case MSG_BUTTONPUSHED
        switch Sign_ID
           case 001                                        ; ID 001  Sign EXE

			     filetosign = DialogControlGet( Sign_Handle, 006, DC_EDITBOX )  ;get all text in editbox.
              ;Check if file name specified
              If !FileExist(filetosign) 
                  Message("Notice!","Please specify a valid file name.")
                  return(RET_DO_NOT_EXIT)
              Endif
              
              ;Check if friendlyname specified
              friendlyname = DialogControlGet( Sign_Handle, 013, DC_EDITBOX )  ;get all text in editbox.
              If friendlyname == "" || StrLen(friendlyname)<4
                  Message("Notice!","Please specify a valid friendlyname. Should be at least 4 characters or more.")
                  return(RET_DO_NOT_EXIT)
              Endif

              ;Read in Description of certificate if any
              desc = DialogControlGet( Sign_Handle, 008, DC_EDITBOX )  ;get all text in editbox.

              ;Read in URL of certificate if any
              url = DialogControlGet( Sign_Handle, 010, DC_EDITBOX )  ;get all text in editbox.

				  ;Read in Timestamp of certificate if any
              timestamp = DialogControlGet( Sign_Handle, 012, DC_EDITBOX )  ;get all text in editbox.

	            ; Performs Code Signing.
	            ;
	            ;    p1   Meaning
	            ;    --   -------
	            ;     0   Sign a file (Windows XP/Vista only)
	            ;           p2 specifies a tab-delimited string with the following parameters:
	            ;             file to sign
	            ;             certificate friendly name
	            ;             description of file being signed (or blank string)
	            ;             location (URL) of information about file (or blank string)
	            ;             URL of timestamp server !Important! (or blank string)
	            ;

              ; Sign file based on certificate data like friendly name 
              certdata =  filetosign : @tab : friendlyname : @tab : desc : @tab : url : @tab : timestamp
              IntControl(93, 0, certdata, 0,  0)
              Message("Notice!", "The file has been signed successfully.")

				  ;Update ini file with new settings
				  ;Define ini file 
				  ptr_ini = PtrGlobal( ini )
				  IniWritePvt( 'Settings', 'filetosign', filetosign, *ptr_ini )
				  IniWritePvt( 'Settings', 'friendlyname', friendlyname, *ptr_ini )
				  IniWritePvt( 'Settings', 'desc', desc, *ptr_ini )
				  IniWritePvt( 'Settings', 'url', url, *ptr_ini )
				  IniWritePvt( 'Settings', 'timestamp', timestamp, *ptr_ini )
				  
              return(RET_DO_DEFAULT)

           case 002                                        ; ID 002  Cancel
              return(RET_DO_DEFAULT)

           case 005                                        ; ID 005  Browse...
              dafile = AskFileName( "Choose files to sign", "C:\", "All Files|*.*", "", 2 )              
              DialogControlSet( Sign_Handle, 006, DC_EDITBOX, dafile )
              return(RET_DO_NOT_EXIT)

			  case 015                                        ; ID 005  Browse...
              dafile = AskFileName( "Choose Certificate file (.SPC)", "C:\", "SPC files|*.SPC", "", 2 )              
              DialogControlSet( Sign_Handle, 014, DC_EDITBOX, dafile )
              return(RET_DO_NOT_EXIT)

        endswitch                                          ; Sign_ID
        return(RET_DO_DEFAULT)

   endswitch                                                ; Sign_Message
   return(RET_DO_DEFAULT)
   :Cancel
   Exit
#EndFunction                                                ; End of Dialog Callback SignCallbackProc



;***************************************************************************
;** 
;**       Sign Code
;** 
;** 
;** Purpose: This code can be used to code sign an EXE.
;** Inputs:  SPC file, PVK File, Private key password, Friendly name for certificate.
;** Outputs: NONE
;** Revisions: 1001
;***************************************************************************


; Check if running 2007A or newer
ver = VersionDLL( )
If ver < "2007A"
   Message("Sorry", "WIL version 2007A or newer required")
   Exit
EndIf

;Define Ini File
ini = ShortCutDir('AppData') : '\WinBatch\Settings\signcode.ini'
PtrGlobalDefine( ini )

timestamp = 'http://timestamp.verisign.com/scripts/timstamp.dll'

SignFormat=`WWWDLGED,6.1`

SignCaption=`Sign Code`
SignX=240
SignY=237
SignWidth=302
SignHeight=160
SignNumControls=014
SignProcedure=`SignCallbackProc`
SignFont=`DEFAULT`
SignTextColor=`DEFAULT`
SignBackground=`DEFAULT,DEFAULT`
SignConfig=0

Sign001=`105,132,030,011,PUSHBUTTON,DEFAULT,"Sign EXE",1,7,32,DEFAULT,DEFAULT,DEFAULT`
Sign002=`148,132,033,011,PUSHBUTTON,DEFAULT,"Cancel",0,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign003=`018,015,261,024,STATICTEXT,DEFAULT,"You have successfully installed your private key and certificate. If you would like to test sign a file please choose the file you would like to sign.",DEFAULT,9,0,DEFAULT,DEFAULT,DEFAULT`
Sign004=`022,046,049,009,STATICTEXT,DEFAULT,"File to sign:",DEFAULT,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign005=`244,044,030,011,PUSHBUTTON,DEFAULT,"Browse...",2,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign006=`079,044,158,011,EDITBOX,filetosign,DEFAULT,DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign007=`022,063,049,009,STATICTEXT,DEFAULT,"Friendly name",DEFAULT,12,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign008=`079,079,158,011,EDITBOX,desc,DEFAULT,DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign009=`022,081,049,009,STATICTEXT,DEFAULT,"Description",DEFAULT,13,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign010=`079,097,158,011,EDITBOX,url,DEFAULT,DEFAULT,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign011=`022,116,049,009,STATICTEXT,DEFAULT,"Timestamp server",DEFAULT,15,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign012=`079,114,158,011,EDITBOX,timestamp,DEFAULT,DEFAULT,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign013=`079,062,158,010,EDITBOX,friendlyname,DEFAULT,DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign014=`022,098,049,010,STATICTEXT,DEFAULT,"Website",DEFAULT,14,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("Sign")




:Cancel
Exit








