-
Notifications
You must be signed in to change notification settings - Fork 9
/
SandboxToys2.ahk
4809 lines (4382 loc) · 166 KB
/
SandboxToys2.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#UseHook Off
#Persistent
#SingleInstance Ignore
version = 2.5.4
; SandboxToys: Main Menu
; Author: r0lZ updated by blap and others
; Developed and compiled with AHK2Exe(Unicode 64-bit.bin) with no compression v 1.1.36.02.
; Tested under Win10 x64 with Sandboxie-Plus v1.9.3.
;
; AutoHotkey script to show a menu with several tools related to Sandboxie,
; and a menu to launch applications installed in any sandbox.
;
; The script can also create a sandboxed shortcut on the desktop if you
; Control-Left-Click on any entry of the menu, or if you call the script with
; an existing file, directory or shortcut (.LNK) file as argument.
;
; Known bugs:
; - The icons used by the script (in the menu and in the shortcuts created
; on the desktop) are not always correct.
; - The Internet Shortcut (.URL) files cannot currently be launched sandboxed.
; (It's a Sandboxie bug.)
;
; To do:
; - Option to automatically launch all apps supposed to be launched at Windows
; startup (from Start Menu\Programs\Startup and RUN keys in registry).
; - Options to clone the sandboxed Start Menu, Desktop or QuickLaunch shortcuts
; as sandboxed shortcuts in the real equivalent folders.
; - Add an "Explore" item in each sub-menu of the Start Menu, Desktop and
; QuickLaunch menus (like in the standard Sandboxie Start Menu).
; - Add _ask_ box name in the list of boxes for the Create Shortcut option.
SplitPath, A_ScriptName, , , , nameNoExt
; Settings
; Note: these values are overwritten if SandboxToys.ini exists in the directrory
; containing the script, in %appdata% or in %appdata%\SandboxToys2\
smalliconsize = 16 ; other icons
largeiconsize = 32 ; sandbox icons
seperatedstartmenus = 0
includeboxnames = 1
trayiconfile =
trayiconnumber = 1
sbcommandpromptdir = `%userprofile`%
inidir = %A_ScriptDir%
sbtini = %A_ScriptDir%\%nameNoExt%.ini
regconfig = %A_ScriptDir%\%nameNoExt%_RegConfig.cfg
ignorelist = %A_ScriptDir%\%nameNoExt%_Ignore_
usertoolsdir = %A_ScriptDir%\%nameNoExt%_UserTools
if (NOT FileExist(sbtini))
{
inidir = %appdata%\SandboxToys2
sbtini = %inidir%\%nameNoExt%.ini
regconfig = %inidir%\%nameNoExt%_RegConfig.cfg
ignorelist = %inidir%\%nameNoExt%_Ignore_
usertoolsdir = %inidir%\%nameNoExt%_UserTools
if (NOT FileExist(inidir))
FileCreateDir, %inidir%
}
if (NOT FileExist(usertoolsdir))
FileCreateDir, %usertoolsdir%
if (FileExist(sbtini)) {
IniRead, largeiconsize, %sbtini%, AutoConfig, LargeIconSize, %largeiconsize%
IniRead, smalliconsize, %sbtini%, AutoConfig, SmallIconSize, %smalliconsize%
IniRead, seperatedstartmenus, %sbtini%, AutoConfig, SeperatedStartMenus, %seperatedstartmenus%
IniRead, includeboxnames, %sbtini%, AutoConfig, IncludeBoxNames, %includeboxnames%
IniRead, trayiconfile, %sbtini%, UserConfig, TrayIconFile, %trayiconfile%
IniRead, trayiconnumber, %sbtini%, UserConfig, TrayIconNumber, %trayiconnumber%
IniRead, sbcommandpromptdir, %sbtini%, UserConfig, SandboxedCommandPromptDir, %sbcommandpromptdir%
}
else
{
IniWrite, %largeiconsize%, %sbtini%, AutoConfig, LargeIconSize
IniWrite, %smalliconsize%, %sbtini%, AutoConfig, SmallIconSize
IniWrite, %seperatedstartmenus%, %sbtini%, AutoConfig, SeperatedStartMenus
IniWrite, %includeboxnames%, %sbtini%, AutoConfig, IncludeBoxNames
IniWrite, %trayiconfile%, %sbtini%, UserConfig, TrayIconFile
IniWrite, %trayiconnumber%, %sbtini%, UserConfig, TrayIconNumber
IniWrite, %sbcommandpromptdir%, %sbtini%, UserConfig, SandboxedCommandPromptDir
}
if (trayiconfile == "ERROR")
trayiconfile =
if (NOT A_IsCompiled && trayiconfile == "") {
tmp = %A_ScriptDir%\SandboxToys2.ico
if (FileExist(tmp))
trayiconfile := tmp
tmp = %A_ScriptDir%\%nameNoExt%.ico
if (FileExist(tmp))
trayiconfile := tmp
}
; some useful constants
setWorkingDir %A_ScriptDir%
title = SandboxToys v%version% by r0lZ updated by blap
if (nameNoExt != "SandboxToys")
title = %title% (%nameNoExt%)
A_nl = `n
A_Quotes = "
shell32 = %A_WinDir%\system32\shell32.dll
imageres = %A_WinDir%\system32\imageres.dll
cmdRes = %A_WinDir%\system32\cmd.exe
explorerImg = %A_WinDir%\system32\explorer.exe
if (! FileExist(explorerImg)) {
explorerImg = %A_WinDir%\explorer.exe
}
explorerRes = %A_WinDir%\system32\explorer.exe
if (! FileExist(explorerRes)) {
explorerRes = %A_WinDir%\explorer.exe
}
explorer = %explorerImg% /e`,::`{20D04FE0-3AEA-1069-A2D8-08002B30309D`}
explorerE = %explorerImg% /e
explorerER = %explorerImg% /e /root
explorerERArg = %explorerImg% /e /root`,::`{20D04FE0-3AEA-1069-A2D8-08002B30309D`}
explorerArg = `,::`{20D04FE0-3AEA-1069-A2D8-08002B30309D`}
explorerArgE = /e`,::`{20D04FE0-3AEA-1069-A2D8-08002B30309D`}
explorerArgER = /e /root`,::`{20D04FE0-3AEA-1069-A2D8-08002B30309D`}
regeditImg = %A_WinDir%\system32\regedit.exe
if (! FileExist(regeditImg)) {
regeditImg = %A_WinDir%\regedit.exe
}
regeditRes = %A_WinDir%\system32\regedit.exe
if (! FileExist(regeditRes)) {
regeditRes = %A_WinDir%\regedit.exe
}
; we need the %SID% and %SESSION% variables, supported by Sandboxie,
; but not directly available as Windows environment variables.
; Get them from the registry.
; %SID%:
Loop, HKEY_CURRENT_USER, Software\Microsoft\Protected Storage System Provider, 1, 0
{
if (A_LoopRegType == "KEY") {
SID = %A_LoopRegName%
break
}
}
; %SESSION%:
RegRead, SESSION, HKEY_CURRENT_USER, Volatile Environment, SESSION
if (SESSION == "" || SESSION == "Console")
SESSION = 0
if (NOT A_IsUnicode) {
msgBox, 16, %title%, This program must be run under the AutoHotkey_L (AHK_Lw) build of AutoHotkey.
ExitApp
}
; find Sandboxie's installation dir
RegRead, imagepath, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\services\SbieSvc, ImagePath
imagepath := Trim(imagepath,A_Quotes)
splitpath, imagepath, , sbdir
start = %sbdir%\Start.exe
SbieCtrl = %sbdir%\SbieCtrl.exe
SbieMngr = %sbdir%\SandMan.exe
SbieAgent = %SbieMngr%
if (! FileExist(SbieAgent)) {
SbieAgent = %sbdir%\SbieCtrl.exe
if (! FileExist(SbieAgent)) {
MsgBox 16, %title%, Can't find Sandboxie installation folder. Sorry.
ExitApp
}
}
;@Ahk2Exe-AddResource Resources\IconFull.ico ; Id=6
;@Ahk2Exe-AddResource Resources\IconEmpty.ico ; Id=7
if (SbieAgent == SbieCtrl) {
SbieAgentResMain = %SbieAgent%
SbieAgentResMainId := 1
SbieAgentResMainText = Sandboxie Control
SbieAgentResFull = %SbieCtrl%
SbieAgentResFullId := 3
SbieAgentResEmpty = %SbieCtrl%
SbieAgentResEmptyId = 10
if (A_IsCompiled) {
SbieAgentResMain = %SbieCtrl%
SbieAgentResMainId := 1
SbieAgentResMainText = Sandboxie Control
SbieAgentResFull = %SbieCtrl%
SbieAgentResFullId := 3
SbieAgentResEmpty = %SbieCtrl%
SbieAgentResEmptyId := 10
}
}
else {
SbieAgentResMain = %SbieAgent%
SbieAgentResMainId := 1
SbieAgentResMainText = Sandboxie-Plus Manager
SbieAgentResFull = Resources\IconFull.ico
SbieAgentResFullId := 1
SbieAgentResEmpty = Resources\IconEmpty.ico
SbieAgentResEmptyId := 1
if (A_IsCompiled) {
SbieAgentResMain = %SbieMngr%
SbieAgentResMainId := 1
SbieAgentResMainText = Sandboxie-Plus Manager
SbieAgentResFull = %A_ScriptFullPath%
SbieAgentResFullId := 6
SbieAgentResEmpty = %A_ScriptFullPath%
SbieAgentResEmptyId := 7
}
}
; find Sandboxie's INI file in %A_WinDir% and in Sandboxie's install dir
RegRead, IniPathO, HKLM\SYSTEM\CurrentControlSet\Services\SbieDrv, IniPath ; check custom config location in registry
IniPath = %IniPathO%
ini =
if ((IniPath != "") && (SubStr(IniPath, 1, 4) == "\??\") && (SubStr(IniPath, 8) != "") && (FileExist(SubStr(IniPath, 5)))) {
IniPath := (SubStr(IniPath, 5))
ini = %IniPath%
}
else {
IniPath =
ini = %sbdir%\Sandboxie.ini
if (! FileExist(ini))
{
ini = %A_WinDir%\Sandboxie.ini
if (! FileExist(ini))
{
MsgBox, 16, %title%, Can't find Sandboxie.ini.
ExitApp
}
}
}
; get current Sandboxes installation bpath in the INI file.
; If it is not defined, assumes the default bpath.
IniRead, sandboxes_path, %ini%, GlobalSettings, FileRootPath, %systemdrive%\Sandbox\`%USER`%\`%SANDBOX`%
sandboxes_path := expandEnvVars(sandboxes_path)
; Get the array of sandboxes (requires AHK_L)
sandboxes_array := Object()
getSandboxesArray(sandboxes_array,ini)
; parse command line
; If one argument is passed and it's a file or folder,
; creates a sandboxed shortcut to it on the desktop and exit
traymode = 0
singlebox = ""
singleboxmode = 0
startupfile := ""
if 0 >= 1
{
mainarg = %1%
if (SubStr(mainarg, 1, 5) == "/box:") {
singlebox := SubStr(mainarg, 6)
singleboxmode = 1
if 0 >= 2
mainarg = %2%
else
mainarg =
}
if (mainarg == "/tray") {
traymode = 1
} else if (mainarg == "/makeregconfig") {
err = 0
if (singleboxmode == 0)
err = 1
if (err)
MsgBox, 16, %title%, Required box argument missing.`nUsage to recreate the registry config file:`n%nameNoExt% /box:boxname /makeregconfig`nThe box MUST be empty!
else
MakeRegConfig(singlebox)
ExitApp
} else {
Menu, Tray, NoIcon
startupfile := mainarg
}
}
if (startupfile != "")
{
startupfile = %1%
startupfile := Trim(startupfile, A_Quotes)
If (NOT FileExist(startupfile))
{
GoSub, CmdLineHelp
ExitApp
}
numboxes := sandboxes_array[0]
if (singleboxmode) {
NewShortcut(singlebox, startupfile)
ExitApp
}
box := getSandboxName(sandboxes_array, "Target sandbox for shortcut:", true)
if (box != "")
NewShortcut(box, startupfile)
ExitApp
}
; If the RegConfig.cfg file doesn't exist and an empty box is available,
; generate it using that empty box.
if (! FileExist(regconfig))
{
numboxes := sandboxes_array[0]
emptybox =
Loop, %numboxes%
{
b := sandboxes_array[A_Index,"name"]
exist := sandboxes_array[b,"exist"]
benabled := sandboxes_array[b,"Enabled"]
bneverdelete := sandboxes_array[b,"NeverDelete"]
busefileimage := sandboxes_array[b,"UseFileImage"]
buseramdisk := sandboxes_array[b,"UseRamDisk"]
if (benabled != 1 || bneverdelete != 0 || busefileimage != 0 || buseramdisk != 0) { ; Skip disabled, neverdelete, usefileimage, or useramdisk boxes
Continue
}
if (NOT exist)
{
emptybox := b
break
}
}
if (emptybox != "")
{
MakeRegConfig(emptybox)
msg = SandboxToys has generated the registry configuration file`n"%regconfig%"`n`n
msg = %msg%That file is necessary to exclude the registry keys and values
msg = %msg% that Sandboxie needs to create in the sandbox for its own use
msg = %msg% from the output of the "Registry List and Export" and "Watch
msg = %msg% Registry Changes" functions.`n`n
msg = %msg%SandboxToys needs an EMPTY sandbox to create that file. The box
msg = %msg% "%emptybox%" is empty, and has been used to generate the file.`n`n
msg = %msg%If you need to recreate that file, just delete the file, be sure
msg = %msg% to delete a sandbox, and launch SandboxToys again. You should see
msg = %msg% this dialog again.
MsgBox, 64, %title%, %msg%
runWait, %start% /box:%emptybox% /terminate, , UseErrorLevel
runWait, %start% /box:%emptybox% delete_sandbox, , UseErrorLevel
}
}
if (traymode) {
Menu, Tray, Nostandard
Menu, Tray, Add, About and Help, MainHelpMenuHandler
setMenuIcon("Tray", "About and Help", shell32, 24, smalliconsize)
Menu, Tray, Add, Exit, ExitMenuHandler
setMenuIcon("Tray", "Exit", shell32, 28, smalliconsize)
Menu, Tray, Add
Menu, Tray, Add, SandboxToys Menu, BuildMainMenu
setMenuIcon("Tray", "SandboxToys Menu", SbieAgentResMain, SbieAgentResMainId, smalliconsize)
if (trayiconfile != "") {
if (trayiconnum == "")
trayiconnum = 1
menu, %menu%, UseErrorLevel, on
Menu, Tray, Icon, %trayiconfile%, %trayiconnum%, 1
menu, %menu%, UseErrorLevel, off
}
Menu, Tray, Default, SandboxToys Menu
Menu, Tray, Click, 1
if (singleboxmode)
Menu, Tray, Tip, %title%`nBox : %singlebox%
else
Menu, Tray, Tip, %title%
} else {
GoSub, BuildMainMenu
ExitApp
}
Return
; ######################################################
; No arguments, or called from tray: build the main menu
; ######################################################
BuildMainMenu:
if (traymode)
{
sandboxes_array := Object()
getSandboxesArray(sandboxes_array,ini)
}
; Init the arrays of menu commands and icons (requires AHK_L)
menucommands := Object()
menuicons := Object()
Menu, ST2MainMenu, Add
Menu, ST2MainMenu, DeleteAll
; Main loop: process all sandboxes
numboxes := sandboxes_array[0]
if (numboxes == 1) {
singleboxmode = 1
singlebox := sandboxes_array[1,"name"]
}
; Build the Main menu
loop, %numboxes%
{
box := sandboxes_array[A_Index,"name"]
boxpath := sandboxes_array[box,"bpath"]
boxexist := sandboxes_array[box,"exist"]
dropadminrights := sandboxes_array[box,"DropAdminRights"]
benabled := sandboxes_array[box,"Enabled"]
if (!benabled) { ; Hide disabled boxes from box list
Continue
}
if (boxexist) {
boxlabel = %box%
}
else {
boxlabel = %box% (empty)
}
if (singleboxmode && box != singlebox)
continue
Menu, %box%_ST2MenuBox, Add
Menu, %box%_ST2MenuBox, DeleteAll
Menu, %box%_ST2StartMenu, Add
Menu, %box%_ST2StartMenu, DeleteAll
Menu, %box%_ST2StartMenuAU, Add
Menu, %box%_ST2StartMenuAU, DeleteAll
Menu, %box%_ST2StartMenuCU, Add
Menu, %box%_ST2StartMenuCU, DeleteAll
Menu, %box%_ST2Desktop, Add
Menu, %box%_ST2Desktop, DeleteAll
Menu, %box%_ST2QuickLaunch, Add
Menu, %box%_ST2QuickLaunch, DeleteAll
Menu, %box%_ST2MenuExplore, Add
Menu, %box%_ST2MenuExplore, DeleteAll
Menu, %box%_ST2MenuReg, Add
Menu, %box%_ST2MenuReg, DeleteAll
Menu, %box%_ST2MenuTools, Add
Menu, %box%_ST2MenuTools, DeleteAll
Menu, SBMenuSetup, Add
Menu, SBMenuSetup, DeleteAll
if (singleboxmode) {
Menu, %singlebox%_ST2MenuBox, Add, Box %boxlabel%, DummyMenuHandler
Menu, %singlebox%_ST2MenuBox, Disable, Box %boxlabel%
if (boxexist) {
setMenuIcon(singlebox "_ST2MenuBox", "Box " boxlabel, SbieAgentResFull, SbieAgentResFullId, smalliconsize)
} else {
setMenuIcon(singlebox "_ST2MenuBox", "Box " boxlabel, SbieAgentResEmpty, SbieAgentResEmptyId, smalliconsize)
}
Menu, %singlebox%_ST2MenuBox, Add
}
added_menus = 0
if (boxexist) {
; build bpath to the Public (All Users) directory (and removes the ":")
public_dir = %public%
if (public_dir != "") {
idx := InStr(public_dir, ":")
if (idx) {
public_dir := substr(public_dir,1,idx-1) . substr(public_dir,idx+1)
}
}
; Build the Box / Start Menu(s)
if (seperatedstartmenus) {
; get shortcut files from the All Users StartMenu (top section)
tmp1 = %boxpath%\user\all\Microsoft\Windows\Start Menu
topicons := getFilenames(tmp1, 0)
topicons := Trim(topicons, A_nl)
Sort, topicons, CL D`n
if (topicons) {
numtopicons := addCmdsToMenu(box, "ST2StartMenuAU", topicons)
Menu, %box%_ST2MenuBox, Add, Start Menu (all users), :%box%_ST2StartMenuAU
setMenuIcon(box "_ST2MenuBox", "Start Menu (all users)", shell32, 20, largeiconsize)
added_menus = 1
}
; and from the Programs section
tmp1 = %boxpath%\user\all\Microsoft\Windows\Start Menu\Programs
files1 := getFilenames(tmp1, 1)
if (files1 && topicons)
Menu, %box%_ST2StartMenuAU, Add
menunum = 0
numicons := buildProgramsMenu1(box, "ST2StartMenuAU", tmp1)
if (numicons)
added_menus = 1
if (topicons == "" && numicons > 0) {
Menu, %box%_ST2MenuBox, Add, Start Menu (all users), :%box%_ST2StartMenuAU
setMenuIcon(box "_ST2MenuBox", "Start Menu (all users)", shell32, 20, largeiconsize)
}
; get shortcut files from the Current User StartMenu (top section)
tmp1 = %boxpath%\user\current\AppData\Roaming\Microsoft\Windows\Start Menu
topicons := getFilenames(tmp1, 0)
topicons := Trim(topicons, A_nl)
Sort, topicons, CL D`n
if (topicons) {
numtopicons := addCmdsToMenu(box, "ST2StartMenuCU", topicons)
Menu, %box%_ST2MenuBox, Add, Start Menu (current user), :%box%_ST2StartMenuCU
setMenuIcon(box "_ST2MenuBox", "Start Menu (current user)", shell32, 20, largeiconsize)
added_menus = 1
}
; and from the Programs section
tmp1 = %boxpath%\user\current\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
files1 := getFilenames(tmp1, 1)
if (files1 && topicons)
Menu, %box%_ST2StartMenuCU, Add
menunum = 0
numicons := buildProgramsMenu1(box, "ST2StartMenuCU", tmp1)
if (numicons)
added_menus = 1
if (topicons == "" && numicons > 0) {
Menu, %box%_ST2MenuBox, Add, Start Menu (current user), :%box%_ST2StartMenuCU
setMenuIcon(box "_ST2MenuBox", "Start Menu (current user)", shell32, 20, largeiconsize)
}
; process Public Desktop
tmp1 = %boxpath%\drive\%public_dir%\Desktop
menunum = 0
m := buildProgramsMenu1(box, "ST2DesktopAU", tmp1)
if (m) {
added_menus = 1
Menu, %box%_ST2MenuBox, Add, Desktop (all users), :%box%_%m%
setMenuIcon(box "_ST2MenuBox", "Desktop (all users)", shell32, 35, largeiconsize)
}
; process User's Desktop
tmp1 = %boxpath%\user\current\Desktop
menunum = 0
m := buildProgramsMenu1(box, "ST2DesktopCU", tmp1)
if (m) {
added_menus = 1
Menu, %box%_ST2MenuBox, Add, Desktop (current user), :%box%_%m%
setMenuIcon(box "_ST2MenuBox", "Desktop (current user)", shell32, 35, largeiconsize)
}
} else {
; get shortcut files from the StartMenu (top section)
tmp1 = %boxpath%\user\all\Microsoft\Windows\Start Menu
files1 := getFilenames(tmp1, 0)
tmp2 = %boxpath%\user\current\AppData\Roaming\Microsoft\Windows\Start Menu
files2 := getFilenames(tmp2, 0)
topicons = %files1%`n%files2%
topicons := Trim(topicons, A_nl)
Sort, topicons, CL D`n
if (topicons) {
numtopicons := addCmdsToMenu(box, "ST2StartMenu", topicons)
Menu, %box%_ST2MenuBox, Add, Start Menu, :%box%_ST2StartMenu
setMenuIcon(box "_ST2MenuBox", "Start Menu", shell32, 20, largeiconsize)
added_menus = 1
}
; and from the Programs section
tmp1 = %boxpath%\user\all\Microsoft\Windows\Start Menu\Programs
files1 := getFilenames(tmp1, 1)
tmp2 = %boxpath%\user\current\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
files2 := getFilenames(tmp2, 1)
if ((files1 || files2) && topicons)
Menu, %box%_ST2StartMenu, Add
menunum = 0
numicons := buildProgramsMenu2(box, "ST2StartMenu", tmp1, tmp2)
if (numicons)
added_menus = 1
if (topicons == "" && numicons > 0) {
Menu, %box%_ST2MenuBox, Add, Start Menu, :%box%_ST2StartMenu
setMenuIcon(box "_ST2MenuBox", "Start Menu", shell32, 20, largeiconsize)
}
; process Desktop
tmp1 = %boxpath%\drive\%public_dir%\Desktop
files1 := getFilenames(tmp1, 1)
tmp2 = %boxpath%\user\current\Desktop
files2 := getFilenames(tmp2, 1)
if ((files1 || files2) && topicons)
Menu, %box%_ST2MenuBox, Add
menunum = 0
m := buildProgramsMenu2(box, "ST2Desktop", tmp1, tmp2)
if (m) {
added_menus = 1
Menu, %box%_ST2MenuBox, Add, Desktop, :%box%_%m%
setMenuIcon(box "_ST2MenuBox", "Desktop", shell32, 35, largeiconsize)
}
}
; process QuickLaunch
tmp1 = %boxpath%\user\current\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch
menunum = 0
m := buildProgramsMenu1(box, "ST2QuickLaunch", tmp1)
if (m) {
added_menus = 1
Menu, %box%_ST2MenuBox, Add, QuickLaunch, :%box%_%m%
setMenuIcon(box "_ST2MenuBox", "QuickLaunch", shell32, 215, largeiconsize)
}
if (added_menus)
Menu, %box%_ST2MenuBox, Add
}
; add Sandboxie's start menu and run dialog in all boxes
Menu, %box%_ST2MenuBox, Add, Sandboxie's Start Menu, StartMenuMenuHandler
setMenuIcon(box "_ST2MenuBox", "Sandboxie's Start Menu", SbieAgentResMain, SbieAgentResMainId, largeiconsize)
Menu, %box%_ST2MenuBox, Add, Sandboxie's Run Dialog, RunDialogMenuHandler
setMenuIcon(box "_ST2MenuBox", "Sandboxie's Run Dialog", SbieAgentResMain, SbieAgentResMainId, largeiconsize)
Menu, %box%_ST2MenuBox, Add
if (NOT boxexist) {
Menu, %box%_ST2MenuBox, Add, Explore (Sandboxed), SExploreMenuHandler
setMenuIcon(box "_ST2MenuBox", "Explore (Sandboxed)", explorerRes, 1, largeiconsize)
Menu, %box%_ST2MenuBox, Add, New Sandboxed Shortcut, NewShortcutMenuHandler
setMenuIcon(box "_ST2MenuBox", "New Sandboxed Shortcut", imageres, 155, largeiconsize)
}
if (boxexist) {
; Add the Explore items to the Box menu
Menu, %box%_ST2MenuExplore, Add, Unsandboxed, UExploreMenuHandler
setMenuIcon(box "_ST2MenuExplore", "Unsandboxed", explorerRes, 1, smalliconsize)
Menu, %box%_ST2MenuExplore, Add, Unsandboxed`, restricted, URExploreMenuHandler
setMenuIcon(box "_ST2MenuExplore", "Unsandboxed, restricted", explorerRes, 1, smalliconsize)
Menu, %box%_ST2MenuExplore, Add, Sandboxed, SExploreMenuHandler
setMenuIcon(box "_ST2MenuExplore", "Sandboxed", explorerRes, 1, smalliconsize)
Menu, %box%_ST2MenuExplore, Add
Menu, %box%_ST2MenuExplore, Add, Files List and Export, ListFilesMenuHandler
setMenuIcon(box "_ST2MenuExplore", "Files List and Export", shell32, 172, smalliconsize)
Menu, %box%_ST2MenuExplore, Add, Watch Files Changes, WatchFilesMenuHandler
setMenuIcon(box "_ST2MenuExplore", "Watch Files Changes", shell32, 172, smalliconsize)
Menu, %box%_ST2MenuBox, Add, Explore, :%box%_ST2MenuExplore
setMenuIcon(box "_ST2MenuBox", "Explore", explorerRes, 1, largeiconsize)
; Add the Registry items to the Box menu
Menu, %box%_ST2MenuReg, Add, Registry Editor (unsandboxed), URegEditMenuHandler
setMenuIcon(box "_ST2MenuReg", "Registry Editor (unsandboxed)", regeditRes, 1, smalliconsize)
if (NOT dropadminrights) {
Menu, %box%_ST2MenuReg, Add, Registry Editor (sandboxed), SRegEditMenuHandler
setMenuIcon(box "_ST2MenuReg", "Registry Editor (sandboxed)", regeditRes, 1, smalliconsize)
}
Menu, %box%_ST2MenuReg, Add
Menu, %box%_ST2MenuReg, Add, Registry List and Export, ListRegMenuHandler
setMenuIcon(box "_ST2MenuReg", "Registry List and Export", regeditRes, 3, smalliconsize)
Menu, %box%_ST2MenuReg, Add, Watch Registry Changes, WatchRegMenuHandler
setMenuIcon(box "_ST2MenuReg", "Watch Registry Changes", regeditRes, 3, smalliconsize)
Menu, %box%_ST2MenuBox, Add, Registry, :%box%_ST2MenuReg
setMenuIcon(box "_ST2MenuBox", "Registry", regeditRes, 1, largeiconsize)
Menu, %box%_ST2MenuReg, Add
Menu, %box%_ST2MenuReg, Add, Autostart programs in registry, ListAutostartsMenuHandler
setMenuIcon(box "_ST2MenuReg", "Autostart programs in registry", regeditRes, 2, smalliconsize)
; Build the Tools menu
Menu, %box%_ST2MenuTools, Add, New Sandboxed Shortcut, NewShortcutMenuHandler
setMenuIcon(box "_ST2MenuTools", "New Sandboxed Shortcut", imageres, 155, smalliconsize)
Menu, %box%_ST2MenuTools, Add
Menu, %box%_ST2MenuTools, Add, Watch Files and Registry Changes, WatchFilesRegMenuHandler
setMenuIcon(box "_ST2MenuTools", "Watch Files and Registry Changes", shell32, 172, smalliconsize)
Menu, %box%_ST2MenuTools, Add
Menu, %box%_ST2MenuTools, Add, Command Prompt (unsandboxed), UCmdMenuHandler
setMenuIcon(box "_ST2MenuTools", "Command Prompt (unsandboxed)", cmdRes, 1, smalliconsize)
Menu, %box%_ST2MenuTools, Add, Command Prompt (sandboxed), SCmdMenuHandler
setMenuIcon(box "_ST2MenuTools", "Command Prompt (sandboxed)", cmdRes, 1, smalliconsize)
if (NOT dropadminrights) {
Menu, %box%_ST2MenuTools, Add
Menu, %box%_ST2MenuTools, Add, Programs and Features, UninstallMenuHandler
setMenuIcon(box "_ST2MenuTools", "Programs and Features", A_WinDir "\system32\appmgr.dll", 1, smalliconsize)
}
Menu, %box%_ST2MenuTools, Add
Menu, %box%_ST2MenuTools, Add, Terminate Sandboxed Programs!, TerminateMenuHandler
setMenuIcon(box "_ST2MenuTools", "Terminate Sandboxed Programs!", shell32, 220, smalliconsize)
Menu, %box%_ST2MenuTools, Add, Delete Sandbox!, DeleteBoxMenuHandler
setMenuIcon(box "_ST2MenuTools", "Delete Sandbox!", shell32, 132, smalliconsize)
Menu, %box%_ST2MenuBox, Add, Tools, :%box%_ST2MenuTools
setMenuIcon(box "_ST2MenuBox", "Tools", shell32, 36, largeiconsize)
}
; Build the Main menu
if (! singleboxmode) {
Menu, ST2MainMenu, Add, %boxlabel%, :%box%_ST2MenuBox
if (boxexist) {
setMenuIcon("ST2MainMenu", boxlabel, SbieAgentResFull, SbieAgentResFullId, largeiconsize)
} else {
setMenuIcon("ST2MainMenu", boxlabel, SbieAgentResEmpty, SbieAgentResEmptyId, largeiconsize)
}
}
}
if (singleboxmode)
mainmenu = %singlebox%_ST2MenuBox
else
mainmenu = ST2MainMenu
; process User Tools
menunum = 0
m := buildProgramsMenu1("", "ST2UserTools", usertoolsdir)
if (m) {
Menu, %mainmenu%, Add
Menu, %mainmenu%, Add, User Tools, :_%m%
setMenuIcon(mainmenu, "User Tools", imageres, 118, largeiconsize)
}
; add Launch Sandboxie Agent if it is not already running
if SbieAgent Contains SandMan
{
process, Exist, SandMan.exe
if (ErrorLevel == 0) {
Menu, %mainmenu%, Add
Menu, %mainmenu%, Add, Launch %SbieAgentResMainText%, LaunchSbieAgentMenuHandler
setMenuIcon(mainmenu, "Launch " SbieAgentResMainText, SbieAgentResMain, SbieAgentResMainId, largeiconsize)
}
}
if SbieAgent Contains SbieCtrl
{
process, Exist, SbieCtrl.exe
if (ErrorLevel == 0) {
Menu, %mainmenu%, Add
Menu, %mainmenu%, Add, Launch %SbieAgentResMainText%, LaunchSbieAgentMenuHandler
setMenuIcon(mainmenu, "Launch " SbieAgentResMainText, SbieAgentResMain, SbieAgentResMainId, largeiconsize)
}
}
; add Help & Options menu
Menu, SBMenuSetup, Add, About and Help, MainHelpMenuHandler
setMenuIcon("SBMenuSetup", "About and Help", shell32, 24, 16)
Menu, SBMenuSetup, Add
Menu, SBMenuSetup, Add, Large main-menu and box icons?, SetupMenuMenuHandler1
if (largeiconsize > 16)
Menu, SBMenuSetup, Check, Large main-menu and box icons?
Menu, SBMenuSetup, Add, Large sub-menu icons?, SetupMenuMenuHandler2
if (smalliconsize > 16)
Menu, SBMenuSetup, Check, Large sub-menu icons?
Menu, SBMenuSetup, Add, Seperated All Users menus?, SetupMenuMenuHandler3
if (seperatedstartmenus)
Menu, SBMenuSetup, Check, Seperated All Users menus?
Menu, SBMenuSetup, Add
Menu, SBMenuSetup, Add, Include [#BoxName] in shortcut names?, SetupMenuMenuHandler4
if (includeboxnames)
Menu, SBMenuSetup, Check, Include [#BoxName] in shortcut names?
Menu, %mainmenu%, Add
Menu, %mainmenu%, Add, Options, :SBMenuSetup
setMenuIcon(mainmenu, "Options", shell32, 24, 16)
; show the menu and wait for user action
Menu, %mainmenu%, Show
Return
; ###################################################################################################
; Functions
; ###################################################################################################
; get sandbox names, paths and properties from Sandboxie's INI file
; and from the current state of the sandboxes.
; Arguments:
; array: an initialized Object
; ini: filename of Sandboxie's ini file
; Fills the object with the array:
; Object[0] = number of sandboxes.
; Object[N,"name"] = sandbox N name.
; Object["boxname","bpath"] = complete, absolute bpath to the sandbox folder.
; Object["boxname","exist"] = flag: true if the sandbox is not empty at the time of the check.
; Object["boxname","DropAdminRights"] = flag: state of the DropAdminRights flag in the INI.
; Returns the number of sandboxes.
getSandboxesArray(array,ini)
{
IniRead, sandboxes_path, %ini%, GlobalSettings, FileRootPath, %systemdrive%\Sandbox\`%USER`%\`%SANDBOX`%
sandboxes_path := expandEnvVars(sandboxes_path)
IniRead, sandboxeskey_path, %ini%, GlobalSettings, KeyRootPath, \REGISTRY\USER\Sandbox_`%USER`%_`%SANDBOX`%
sandboxeskey_path := expandEnvVars(sandboxeskey_path)
; Requires AHK_Lw
old_encoding = %A_FileEncoding%
FileEncoding, UTF-16
; get the list of boxes in the INI and sort it
boxes =
Loop, Read, %ini%
{
if (SubStr(A_LoopReadLine, 1, 1) == "[" && SubStr(A_LoopReadLine, 0) == "]" && A_LoopReadLine != "[GlobalSettings]" && SubStr(A_LoopReadLine, 1, 14) != "[UserSettings_" && SubStr(A_LoopReadLine, 1, 10) != "[Template_" && A_LoopReadLine != "[TemplateSettings]") {
box := SubStr(A_LoopReadLine, 2, -1)
boxes = %boxes%%box%,
}
}
boxes := Trim(boxes, ",") ; requires AHK_L
Sort, boxes, CL D`,
; Requires AHK_Lw
FileEncoding, %old_encoding%
; fills the array
Loop, Parse, boxes, CSV
{
array[0] := A_Index
array[A_Index,"name"] := A_LoopField
IniRead, bfilerootpath, %ini%, %A_LoopField%, FileRootPath, %sandboxes_path%
StringReplace, bfilerootpathR, bfilerootpath, %username%, `%USER`%
if (bfilerootpath != sandboxes_path) {
array[A_LoopField,"FileRootPath"] := bfilerootpath
sandboxes_path := expandEnvVars(bfilerootpath)
}
else {
IniRead, sandboxes_path, %ini%, GlobalSettings, FileRootPath, %systemdrive%\Sandbox\`%USER`%\`%SANDBOX`%
array[A_LoopField,"FileRootPath"] := sandboxes_path
sandboxes_path := expandEnvVars(sandboxes_path)
}
StringReplace, bpath, sandboxes_path, `%SANDBOX`%, %A_LoopField%
array[A_LoopField,"bpath"] := bpath
IniRead, bkeyrootpath, %ini%, %A_LoopField%, KeyRootPath, %sandboxeskey_path%
StringReplace, bkeyrootpathR, bkeyrootpath, %username%, `%USER`%
if (bkeyrootpath != sandboxeskey_path) {
array[A_LoopField,"KeyRootPath"] := bkeyrootpath
sandboxeskey_path := expandEnvVars(bkeyrootpath)
regspos := InStr(bkeyrootpathR, "\", false, 0, 1)
regepos := InStr(bkeyrootpathR, "%", false, 1, 1)
regstr := SubStr(bkeyrootpathR, regspos+1, regepos-regspos-2)
array[A_LoopField,"RegStr_"] := regstr
}
else {
IniRead, sandboxeskey_path, %ini%, GlobalSettings, KeyRootPath, \REGISTRY\USER\Sandbox_`%USER`%_`%SANDBOX`%
array[A_LoopField,"KeyRootPath"] := sandboxeskey_path
regspos := InStr(sandboxeskey_path, "\", false, 0, 1)
regepos := InStr(sandboxeskey_path, "%", false, 1, 1)
regstr := SubStr(sandboxeskey_path, regspos+1, regepos-regspos-2)
array[A_LoopField,"RegStr_"] := regstr
sandboxeskey_path := expandEnvVars(sandboxeskey_path)
}
StringReplace, bkey, sandboxeskey_path, `%SANDBOX`%, %A_LoopField%
array[A_LoopField,"bkey"] := bkey
test = %bpath%\RegHive
array[A_LoopField,"exist"] := FileExist(test)
; since running the registry editor works with elevated privileges,
; checks if the box has DropAdminRights=y in the INI
IniRead, dropadminrights, %ini%, %A_LoopField%, DropAdminRights, n
if (dropadminrights == "y") {
array[A_LoopField,"DropAdminRights"] := 1
}
else {
array[A_LoopField,"DropAdminRights"] := 0
}
; checks if the box has Enabled=n in the INI
IniRead, benabled, %ini%, %A_LoopField%, Enabled, y
if (benabled == "n") {
array[A_LoopField,"Enabled"] := 0
}
else {
array[A_LoopField,"Enabled"] := 1
}
; checks if the box has NeverDelete=y in the INI
IniRead, bneverdelete, %ini%, %A_LoopField%, NeverDelete, n
if (bneverdelete == "y") {
array[A_LoopField,"NeverDelete"] := 1
}
else {
array[A_LoopField,"NeverDelete"] := 0
}
; checks if the box has UseFileImage=y in the INI
IniRead, busefileimage, %ini%, %A_LoopField%, UseFileImage, n
if (busefileimage == "y") {
array[A_LoopField,"UseFileImage"] := 1
}
else {
array[A_LoopField,"UseFileImage"] := 0
}
; checks if the box has UseRamDisk=y in the INI
IniRead, buseramdisk, %ini%, %A_LoopField%, UseRamDisk, n
if (buseramdisk == "y") {
array[A_LoopField,"UseRamDisk"] := 1
}
else {
array[A_LoopField,"UseRamDisk"] := 0
}
}
Return % array[0]
}
; Prompts the user for a sandbox name.
; Returns "" if the user selects cancel or discard the menu.
getSandboxName(sandboxes_array, title, include_ask=false)
{
global __box__, SbieAgent, largeiconsize
numboxes := sandboxes_array[0]
Menu, Menu, Add
Menu, Menu, DeleteAll
Menu, Menu, Add, %title%, DummyMenuHandler
Menu, Menu, Disable, %title%
Menu, Menu, Add
loop, %numboxes%
{
box := sandboxes_array[A_Index,"name"]
if (sandboxes_array[box,"exist"]) {
Menu, Menu, Add, %box%, getSandboxNameBoxMenuHandler
setMenuIcon("Menu", box, SbieAgentResFull, SbieAgentResFullId, largeiconsize)
} else {
Menu, Menu, Add, %box% (empty), getSandboxNameBoxMenuHandler
setMenuIcon("Menu", box " (empty)", SbieAgentResEmpty, SbieAgentResEmptyId, largeiconsize)
}
}
if (include_ask)
{
Menu, Menu, Add
Menu, Menu, Add, Ask box at run time, getSandboxNameAskMenuHandler
setMenuIcon("Menu", "Ask box at run time", SbieAgentResMain, SbieAgentResMainId, largeiconsize)
}
Menu, Menu, Add
Menu, Menu, Add, Cancel, getSandboxNameCancelMenuHandler
Menu, Menu, Show
return %__box__%
}
getSandboxNameBoxMenuHandler:
__box__ := sandboxes_array[A_ThisMenuItemPos -2,"name"]
Return
getSandboxNameAskMenuHandler:
__box__ = __ask__
Return
getSandboxNameCancelMenuHandler:
__box__ =
Return
setMenuIcon(menu, item, iconfile, iconindex, largeiconsize)
{
menu, %menu%, UseErrorLevel, on
Menu, %menu%, Icon, %item%, %iconfile%, %iconindex%, %largeiconsize%
rc := ErrorLevel
menu, %menu%, UseErrorLevel, off
return %rc%
}
getFilenames(directory, includeFolders)
{
files =
Loop, %directory%\*, %includeFolders%, 0
{
; Excludes the hidden and system files from list
attributes := A_LoopFileAttrib
IfInString, Attributes, H
Continue
IfInString, Attributes, S
Continue
; Excludes also the files deleted in the sandbox, but present in the "real world".
; They have a "magic" creation date of May 23, 1986, 17:47:02
FileGetTime, creationTime, %A_LoopFileLongPath%, C
if (creationTime == "19860523174702")
Continue
; and keep regular directories and files
IfInString, Attributes, D
{
SplitPath, A_LoopFileName, OutDirName
files = %files%%OutDirName%:%A_LoopFileLongPath%`n
} else {
SplitPath, A_LoopFileName, , , , OutNameNoExt,
files = %files%%OutNameNoExt%:%A_LoopFileLongPath%`n
}
}
StringTrimRight, files, files, 1
if (files)
Sort, files, CL D`n Z
Return %files%
}
; Build a menu with the files from a specific directory
buildProgramsMenu1(box, menuname, bpath)
{
global smalliconsize, menunum
if (menunum > 0)
thismenuname = %menuname%_%menunum%
else
thismenuname = %menuname%
numfiles = 0
; bpath = %bpath%\*
menufiles := getFilenames(bpath, 0)
if (menufiles) {
Sort, menufiles, CL D`n Z
numfiles := addCmdsToMenu(box, thismenuname, menufiles)
}
; else if (numfiles == 0) {
; numfiles = 1
; }
; recurse
menudirs := getFilenames(bpath, 2)
if (menudirs) {
Sort, menudirs, CL D`n Z
Loop, parse, menudirs, `n
{
entry = %A_LoopField%
idx := InStr(entry, ":")
label := subStr(entry, 1, idx-1)
dir := subStr(entry, idx+1)
menunum ++
newmenuname := buildProgramsMenu1(box, menuname, dir)
if (newmenuname != "") {
Menu, %box%_%thismenuname%, Add, %label%, :%box%_%newmenuname%
setMenuIcon(box "_" thismenuname, label, shell32, 4, smalliconsize)
numfiles ++
}
}
}
if (numfiles)
return %thismenuname%
else
return ""
}
; Build a menu with the files from two specific directories by merging them together
buildProgramsMenu2(box, menuname, path1, path2)
{
global smalliconsize, menunum
A_Return = `n
if (menunum > 0)