
#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 InstallCallbackProc(Install_Handle,Install_Message,Install_ID,Install_EventInfo,rsvd)
   InitDialogConstants()                                    ; Initialize Dialog Constants
   switch Install_Message                                   ; Switch based on Dialog Message type
     case MSG_INIT                                         ; Standard Initialization message
         DialogProcOptions(Install_Handle,MSG_BUTTONPUSHED,@TRUE)
         return(RET_DO_DEFAULT)

     case MSG_BUTTONPUSHED
        switch Install_ID
           case 001                                        ; ID 001  Install
              spcfile = DialogControlGet( Install_Handle, 008, DC_EDITBOX )  ;get all text in editbox.
              pvkfile = DialogControlGet( Install_Handle, 009, DC_EDITBOX )  ;get all text in editbox.
				  friendlyname = DialogControlGet( Install_Handle, 011, DC_EDITBOX )  ;get all text in editbox.
              If !FileExist(spcfile) 
                  Message("Notice!","Please specify a valid file name for the Certificate (.spc).")
                  return(RET_DO_NOT_EXIT)
              Endif
              If !FileExist(pvkfile) 
                  Message("Notice!","Please specify a valid file name for the Private key (.pvk)!")
                  return(RET_DO_NOT_EXIT)
              Endif
              if FileLocate("PVKIMPRT.EXE")  == ""
                  Message("Notice!","PVKIMPRT.EXE is not installed. Please install it now.")
                  exit
              Endif

              ;Message("Notice!","You will be prompted for the your Private key password.": @crlf : "Make sure to choose all the 'default' settings in the Certificate Import Wizard.")

              ;Disable WIL DIalog temporarily while pvkimport runs
              DialogProcOptions( Install_Handle, DPO_DISABLESTATE, 1 )

              ;Import files
              RunHideWait(DirWindows(0):"PVKIMPRT.EXE", '-IMPORT "' : spcfile : '" "' : pvkfile :'"')

              ;Install files
              ShellExecute(spcfile, "", "", @HIDDEN, "Install Certificate") 

				  ; Get thumbprint from spc file
				  thumb = IntControl(93, 1, spcfile, 0, 0)
				  
				  ; Set friendly name based on user input
				  IntControl(93, 2, thumb, friendlyname,  0)


              ;Re-enable WIL Dialog 
              DialogProcOptions( Install_Handle, DPO_DISABLESTATE, 0 )
              return(RET_DO_DEFAULT)

           case 002                                        ; ID 002  Cancel
              return(RET_DO_CANCEL)

           case 005                                        ; ID 005  Browse...
              spcfile = AskFileName( "Choose .spc file", "C:\", "Certificate Files|*.spc", "", 1 )
              DialogControlSet( Install_Handle, 008, DC_EDITBOX, spcfile )
              return(RET_DO_NOT_EXIT)

           case 007                                        ; ID 007  Browse...
              pvkfile = AskFileName( "Choose .pvk file", "C:\", "Private Key Files|*.pvk", "", 1 )
              DialogControlSet( Install_Handle, 009, DC_EDITBOX, pvkfile )
              return(RET_DO_NOT_EXIT)

        endswitch                                          ; Install_ID
        return(RET_DO_DEFAULT)

   endswitch                                                ; Install_Message
   return(RET_DO_DEFAULT)

   :Cancel
   return(RET_DO_DEFAULT)
#EndFunction    ; End of Dialog Callback InstallCallbackProc

;============================================================
;============================================================
;============================================================

#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)
         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, 008, 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, 010, DC_EDITBOX )  ;get all text in editbox.

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

              ;Sign exe using IntControl 93
              ; Get spcfile (global pointer)
              ptr_spcfile=PtrGlobal(spcfile)
              spcfile = *ptr_spcfile
              If !FileExist(spcfile) 
                  Message("Error","Unable to locate Certificate (.spc).")
                  return(RET_DO_CANCEL)
              Endif

            ; Performs certificate operations.
            ;
            ;    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)
            ;
            ;     1   Get the thumbprint of a certificate in a file
            ;           p2 specifies a .CER or .SPC file
            ;
            ;     2   Set the friendly name of a certificate
            ;           p2 specifies the thumbprint of a certificate
            ;           p3 specifies the friendly name to set

              
              ; Get thumbprint from spc file
              thumb = IntControl(93, 1, spcfile, 0, 0)
              
              ; Sign file based on certificate data like friendly name 
              certdata =  filetosign : @tab : friendlyname : @tab : desc : @tab : url : @tab : "http://timestamp.verisign.com/scripts/timstamp.dll"
              IntControl(93, 0, certdata, 0,  0)
              Message("Notice!", "The file has been signed successfully.")
              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)

        endswitch                                          ; Sign_ID
        return(RET_DO_DEFAULT)

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



;***************************************************************************
;** 
;**       Import and Install Certificate
;**
;** Notes: PVKIMPRT must be installed before running.
;** 
;** Purpose: This code will run PVKIMPRT to import (register) the certificate and Private key and then it installs the certificate.
;** Inputs:  SPC file, PVK File, Private key password, Friendly name for certificate.
;** Outputs: NONE
;** Revisions: 
;** 1001 First release
;** 1002 Added support for defining friendlyname upon install
;***************************************************************************


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


;Check if PVKIMPRT is installed inthe windows directory
if FileLocate("PVKIMPRT.EXE")  == ""
      Message("Notice!","PVKIMPRT.EXE is not installed. Please install it now.")
      exit
Endif

InstallFormat=`WWWDLGED,6.1`

InstallCaption=`Install Code Signing Certificate`
InstallX=240
InstallY=237
InstallWidth=310
InstallHeight=110
InstallNumControls=011
InstallProcedure=`InstallCallbackProc`
InstallFont=`DEFAULT`
InstallTextColor=`DEFAULT`
InstallBackground=`DEFAULT,DEFAULT`
InstallConfig=0

Install001=`103,084,032,011,PUSHBUTTON,DEFAULT,"Install",1,1,32,DEFAULT,DEFAULT,DEFAULT`
Install002=`164,084,033,011,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install003=`017,010,275,018,STATICTEXT,DEFAULT,"This tool is used to import the private key and certificate into a system store. Please input the full file paths to the Certificate file and the Private key file you have stroed on your system.",DEFAULT,3,0,DEFAULT,DEFAULT,DEFAULT`
Install004=`018,033,050,011,STATICTEXT,DEFAULT,"Certificate (.spc) file",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install005=`239,033,033,011,PUSHBUTTON,DEFAULT,"Browse...",2,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install006=`018,047,053,011,STATICTEXT,DEFAULT,"Private key (.pvk) file",DEFAULT,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install007=`239,049,033,011,PUSHBUTTON,DEFAULT,"Browse...",3,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install008=`078,033,154,011,EDITBOX,spcfile,"",DEFAULT,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install009=`078,047,154,011,EDITBOX,pvkfile,"",DEFAULT,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install010=`018,062,050,010,STATICTEXT,DEFAULT,"Friendlyname",DEFAULT,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Install011=`078,062,154,010,EDITBOX,Friendlyname,"",DEFAULT,11,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("Install")



;Create global pointer to spcfile to be accessed in code signing callback procedure
PtrGlobalDefine( spcfile )



;-----------------------------------------------------------------------------



SignFormat=`WWWDLGED,6.1`

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

Sign001=`105,132,031,011,PUSHBUTTON,DEFAULT,"Sign EXE",1,1,32,DEFAULT,DEFAULT,DEFAULT`
Sign002=`148,132,033,011,PUSHBUTTON,DEFAULT,"Cancel",0,2,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,3,0,DEFAULT,DEFAULT,DEFAULT`
Sign004=`020,049,049,011,STATICTEXT,DEFAULT,"File to sign:",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign005=`249,049,030,011,PUSHBUTTON,DEFAULT,"Browse...",2,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign006=`078,049,158,011,EDITBOX,filetosign,"",DEFAULT,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign007=`020,065,049,014,STATICTEXT,DEFAULT,"Friendly name of the certificate:",DEFAULT,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign008=`078,066,158,011,EDITBOX,friendlyname,"",DEFAULT,6,8,DEFAULT,DEFAULT,DEFAULT`
Sign009=`020,084,049,014,STATICTEXT,DEFAULT,"Decription for Certificate",DEFAULT,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign010=`078,084,158,011,EDITBOX,desc,DEFAULT,DEFAULT,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign011=`020,103,049,011,STATICTEXT,DEFAULT,"URL for Certificate",DEFAULT,11,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
Sign012=`078,102,158,009,EDITBOX,url,DEFAULT,DEFAULT,12,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("Sign")


Exit

:Cancel
Exit








