-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeneric.d
1801 lines (1399 loc) · 52.2 KB
/
generic.d
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
/**
Code from generic.h and generic.d
Ported to the D Programming Language by Laeeth Isharc (2015)
what works: everything except fDance doesn't change active cell, and fShowDialog
-----
File: GENERIC.H
Purpose: Header file for Generic.c
Platform: Microsoft Windows
Updated by Microsoft Product Support Services, Windows Developer Support.
From the Microsoft Excel Developer's Kit, Version 14
Copyright (c) 1996-2010 Microsoft Corporation. All rights reserved.
*/
/**
File: GENERIC.C
Purpose: Template for creating XLLs for Microsoft Excel.
This file contains sample code you can use as
a template for writing your own Microsoft Excel XLLs.
An XLL is a DLL that stands alone, that is, you
can open it by choosing the Open command from the
File menu. This code demonstrates many of the features
of the Microsoft Excel C API.
When you open GENERIC.XLL, it
creates a new Generic menu with four
commands:
Dialog... displays a Microsoft Excel dialog box
Dance moves the selection around
until you press ESC
Native Dialog... displays a Windows dialog box
Exit Closes GENERIC.XLL and
removes the menu
GENERIC.XLL also provides three functions,
Func1, FuncSum and FuncFib, which can be used whenever
GENERIC.XLL is open.
GENERIC.XLL can also be added with the
Add-in Manager.
This file uses the framework library
(FRMWRK32.LIB).
Platform: Microsoft Windows
Functions:
DllMain
xlAutoOpen
xlAutoClose
lpstricmp
xlAutoRegister12
xlAutoAdd
xlAutoRemove
xlAddInManagerInfo12
DIALOGMsgProc
ExcelCursorProc
HookExcelWindow
UnhookExcelWindow
fShowDialog
GetHwnd
Func1
FuncSum
fDance
fDialog
fExit
FuncFib
*/
import win32.winuser:PostMessage,CallWindowProc,GetWindowLongPtr,SetWindowLongPtr,DialogBox;
//import std.c.windows.windows;
import core.sys.windows.windows;
import xlcall;
import framework;
import core.stdc.wchar_ : wcslen;
import core.stdc.wctype:towlower;
import std.format;
import xlld.wrap;
enum GWLP_WNDPROC=-4;
enum MAXWORD = 0xFFFF;
debug=0;
extern(Windows)
{
pragma(lib, "gdi32");
pragma(lib, "kernel32");
pragma(lib, "user32");
pragma(lib, "gdi32");
pragma(lib, "winspool");
pragma(lib, "comdlg32");
pragma(lib, "advapi32");
pragma(lib, "shell32");
pragma(lib, "ole32");
pragma(lib, "oleaut32");
pragma(lib, "uuid");
pragma(lib, "odbc32");
pragma(lib, "xlcall32d");
//pragma(lib, "odbccp32");
//pragma(lib, "msvcrt32");
enum GMEM_MOVEABLE = 0x02;
void* GlobalAlloc(uint, size_t);
void* GlobalLock(void*);
bool GlobalUnlock(void*);
void cwCenter(HWND, int);
//INT_PTR /*CALLBACK*/ DIALOGMsgProc(HWND hWndDlg, UINT message, WPARAM wParam, LPARAM lParam);
}
// identifier for controls
enum FREE_SPACE =104;
enum EDIT =101;
enum TEST_EDIT =106;
/**
Later, the instance handle is required to create dialog boxes.
g_hInst holds the instance handle passed in by DllMain so that it is
available for later use. hWndMain is used in several routines to
store Microsoft Excel's hWnd. This is used to attach dialog boxes as
children of Microsoft Excel's main window. A buffer is used to store
the free space that DIALOGMsgProc will put into the dialog box.
*/
// Global Variables
__gshared HWND g_hWndMain = null;
__gshared HANDLE g_hInst = null;
wchar[20] g_szBuffer = ""w;
/**
Syntax of the Register Command:
REGISTER(module_text, procedure, type_text, function_text,
argument_text, macro_type, category, shortcut_text,
help_topic, function_help, argument_help1, argument_help2,...)
g_rgWorksheetFuncs will use only the first 11 arguments of
the Register function.
This is a table of all the worksheet functions exported by this module.
These functions are all registered (in xlAutoOpen) when you
open the XLL. Before every string, leave a space for the
byte count. The format of this table is the same as
arguments two through eleven of the REGISTER function.
g_rgWorksheetFuncsRows define the number of rows in the table. The
g_rgWorksheetFuncsCols represents the number of columns in the table.
*/
enum g_rgWorksheetFuncsRows =5;
enum g_rgWorksheetFuncsCols =10;
__gshared wstring[g_rgWorksheetFuncsCols][g_rgWorksheetFuncsRows] g_rgWorksheetFuncs =
[
[ "Func1"w, // Procedure
"UU"w, // type_text
"Func1"w, // function_text
"Arg"w, // argument_text
"1"w, // macro_type
"Generic Add-In"w, // category
""w, // shortcut_text
""w, // help_topic
"Always returns the string 'Func1'"w, // function_help
"Argument ignored"w // argument_help1
],
[ "FuncSum"w,
"UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"w, // up to 255 args in Excel 2007 and later,
// upto 29 args in Excel 2003 and earlier versions
"FuncSum"w,
"number1,number2,..."w,
"1"w,
"Generic Add-In"w,
""w,
""w,
"Adds the arguments"w,
"Number1,number2,... are 1 to 29 arguments for which you want to sum."w
],
[ "lastErrorMessage"w,
"Q"w, // up to 255 args in Excel 2007 and later,
// upto 29 args in Excel 2003 and earlier versions
"lastErrorMessage"w,
""w,
"1"w,
"Generic Add-In"w,
""w,
""w,
"Return last D error message"w,
""w,
],
[ "WrapSquare3"w,
"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ"w, // up to 255 args in Excel 2007 and later,
// upto 29 args in Excel 2003 and earlier versions
"WrapSquare3"w,
"number1,number2,..."w,
"1"w,
"Generic Add-In"w,
""w,
""w,
"Sum of squares of the arguments"w,
"Number1,number2,... are 1 to 29 arguments for which you want to sum."w
],
[ "FuncFib"w,
"UU"w,
"FuncFib"w,
"Compute to..."w,
"1"w,
"Generic Add-In"w,
""w,
""w,
"Number to compute to"w
"Computes the nth fibonacci number"w,
],
];
/**
g_rgCommandFuncs
This is a table of all the command functions exported by this module.
These functions are all registered (in xlAutoOpen) when you
open the XLL. Before every string, leave a space for the
byte count. The format of this table is the same as
arguments two through eight of the REGISTER function.
g_rgFuncsRows define the number of rows in the table. The
g_rgCommandFuncsCols represents the number of columns in the table.
*/
enum g_rgCommandFuncsRows =4;
enum g_rgCommandFuncsCols =7;
__gshared wstring g_rgCommandFuncs[g_rgCommandFuncsRows][g_rgCommandFuncsCols] =
[
[ "fDialog"w, // Procedure
"A"w, // type_text
"fDialog"w, // function_text
""w, // argument_text
"2"w, // macro_type
"Generic Add-In"w, // category
"l"w // shortcut_text
],
[ "fDance"w,
"A"w,
"fDance"w,
""w,
"2"w,
"Generic Add-In"w,
"m"w
],
[ "fShowDialog"w,
"A"w,
"fShowDialog"w,
""w,
"2"w,
"Generic Add-In"w,
"n"w],
[ "fExit"w,
"A"w,
"fExit"w,
""w,
"2"w,
"Generic Add-In"w,
"o"w
],
];
/**
g_rgMenu
This is a table describing the Generic drop-down menu. It is in
the same format as the Microsoft Excel macro language menu tables.
The first column contains the name of the menu or command, the
second column contains the function to be executed, the third
column contains the (Macintosh only) shortcut key, the fourth
column contains the help text for the status bar, and
the fifth column contains the help text index. Leave a space
before every string so the byte count can be inserted. g_rgMenuRows
defines the number of menu items. 5 represents the number of
columns in the table.
*/
enum g_rgMenuRows =5;
enum g_rgMenuCols =5;
__gshared wstring[g_rgMenuCols][g_rgMenuRows] g_rgMenu =
[
["&Generic"w, ""w, ""w,
"The Generic XLL Add-In"w, ""w],
["&Dialog..."w, "fDialog"w, ""w,
"Run a sample generic dialog"w, ""w],
["D&ance"w, "fDance"w, ""w,
"Make the selection dance around"w, ""w],
["&Native Dialog..."w, "fShowDialog"w, ""w,
"Run a sample native dialog"w, ""w],
["E&xit"w, "fExit"w, ""w,
"Close the Generic X"w, "ww"w],
];
/**
g_rgTool
This is a table describing the toolbar. It is in the same format
as the Microsoft Excel macro language toolbar tables. The first column
contains the ID of the tool, the second column contains the function
to be executed, the third column contains a logical value specifying
the default image of the tool, the fourth column contains a logical
value specifying whether the tool can be used, the fifth column contains
a face for the tool, the sixth column contains the help_text that
is displayed in the status bar, the seventh column contains the Balloon
text (Macintosh Only), and the eighth column contains the help topics
as quoted text. Leave a space before every string so the byte count
can be inserted. g_rgToolRows defines the number of tools on the toolbar.
8 represents the number of columns in the table.
*/
enum g_rgToolRows =3;
enum g_rgToolCols =8;
__gshared wstring[g_rgToolCols][g_rgToolRows] g_rgTool=
[
["211"w, "fDance"w, "false"w, "true"w, ""w, "Dance the Selection"w, ""w, ""w],
["0"w, ""w, ""w, ""w, ""w, ""w, ""w, ""w],
["212"w, "fExit"w, "false"w, "true"w, ""w, "Exit this example"w, ""w, ""w],
];
/**
g_rgDialog
This is a table describing the sample dialog box used in the fDialog()
function. Admittedly, it would be more efficient to use ints for
the first 5 columns, but that makes the code much more complicated.
Storing the text in string tables is another method that could be used.
Each string is byte counted, but you can also use normal strings and
copy them over into allocated memory with byte countes appended to the front.
Alternatively, you can call TempStr12 on a normal string and use the
string the call allocates to pass into the Excel functions.
g_rgDialogRows determines the number of rows in the dialog box.
7 represents the number of columns in the table.
*/
enum g_rgDialogRows =16;
enum g_rgDialogCols =7;
__gshared wstring[g_rgDialogCols][g_rgDialogRows] g_rgDialog =
[
[""w, ""w, ""w, "494"w, "210"w, "Generic Sample Dialog"w, ""w],
["1"w, "330"w, "174"w, "88"w, ""w, "OK"w, ""w],
["2"w, "225"w, "174"w, "88"w, ""w, "Cancel"w, ""w],
["5"w, "19"w, "11"w, ""w, ""w, "&Name:"w, ""w],
["6"w, "19"w, "29"w, "251"w, ""w, ""w, ""w],
["14"w, "305"w, "15"w, "154"w, "73"w, "&College"w, ""w],
["11"w, ""w, ""w, ""w, ""w, ""w, "1"w],
["12"w, ""w, ""w, ""w, ""w, "&Harvard"w, "1"w],
["12"w, ""w, ""w, ""w, ""w, "&Other"w, ""w],
["5"w, "19"w, "50"w, ""w, ""w, "&Reference:"w, ""w],
["10"w, "19"w, "67"w, "253"w, ""w, ""w, ""w],
["14"w, "209"w, "93"w, "250"w, "63"w, "&Qualifications"w, ""w],
["13"w, ""w, ""w, ""w, ""w, "&BA / BS"w, "1"w],
["13"w, ""w, ""w, ""w, ""w, "&MA / MS"w, "1"w],
["13"w, ""w, ""w, ""w, ""w, "&PhD / Other Grad"w, "0"w],
["15"w, "19"w, "99"w, "160"w, "96"w, "GENERIC_List1"w, "1"w],
];
/**
DllMain()
Purpose:
Windows calls DllMain, for both initialization and termination.
It also makes calls on both a per-process and per-thread basis,
so several initialization calls can be made if a process is multithreaded.
This function is called when the DLL is first loaded, with a dwReason
of DLL_PROCESS_ATTACH.
Parameters:
HANDLE hDLL Module handle.
DWORD dwReason, Reason for call
LPVOID lpReserved Reserved
Returns:
The function returns true (1) to indicate success. If, during
per-process initialization, the function returns zero,
the system cancels the process.
Comments:
History: Date Author Reason
*/
extern(Windows) BOOL /*APIENTRY*/ DllMain( HANDLE hDLL, DWORD dwReason, LPVOID lpReserved )
{
import core.runtime;
import std.c.windows.windows;
import core.sys.windows.dll;
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
Runtime.initialize();
// The instance handle passed into DllMain is saved
// in the global variable g_hInst for later use.
g_hInst = hDLL;
dll_process_attach( hDLL, true );
break;
case DLL_PROCESS_DETACH:
Runtime.terminate();
dll_process_detach( hDLL, true );
break;
case DLL_THREAD_ATTACH:
dll_thread_attach( true, true );
break;
case DLL_THREAD_DETACH:
dll_thread_detach( true, true );
break;
default:
break;
}
return true;
}
/**
xlAutoOpen()
Purpose:
Microsoft Excel call this function when the DLL is loaded.
Microsoft Excel uses xlAutoOpen to load XLL files.
When you open an XLL file, the only action
Microsoft Excel takes is to call the xlAutoOpen function.
More specifically, xlAutoOpen is called:
- when you open this XLL file from the File menu,
- when this XLL is in the XLSTART directory, and is
automatically opened when Microsoft Excel starts,
- when Microsoft Excel opens this XLL for any other reason, or
- when a macro calls REGISTER(), with only one argument, which is the
name of this XLL.
xlAutoOpen is also called by the Add-in Manager when you add this XLL
as an add-in. The Add-in Manager first calls xlAutoAdd, then calls
REGISTER("EXAMPLE.XLL"), which in turn calls xlAutoOpen.
xlAutoOpen should:
- register all the functions you want to make available while this
XLL is open,
- add any menus or menu items that this XLL supports,
- perform any other initialization you need, and
- return 1 if successful, or return 0 if your XLL cannot be opened.
Parameters:
Returns:
int 1 on success, 0 on failure
Comments:
History: Date Author Reason
*/
extern(Windows) int /*WINAPI*/ xlAutoOpen()
{
import std.conv;
import core.runtime:rt_init;
rt_init();
static XLOPER12 xDLL, // name of this DLL //
xMenu, // xltypeMulti containing the menu //
xTool, // xltypeMulti containing the toolbar //
xTest; // used for menu test //
LPXLOPER12 pxMenu; // Points to first menu item //
LPXLOPER12 px; // Points to the current item //
LPXLOPER12 pxTool; // Points to first toolbar item //
int i, j; // Loop indices //
HANDLE hMenu; // global memory holding menu //
HANDLE hTool; // global memory holding toolbar //
/**
In the following block of code the name of the XLL is obtained by
calling xlGetName. This name is used as the first argument to the
REGISTER function to specify the name of the XLL. Next, the XLL loops
through the g_rgWorksheetFuncs[] table, and the g_rgCommandFuncs[]
tableregistering each function in the table using xlfRegister.
Functions must be registered before you can add a menu item.
*/
Excel12f(xlGetName, &xDLL, []);
foreach(row;g_rgWorksheetFuncs)
Excel12f(xlfRegister, cast(LPXLOPER12)0, [cast(LPXLOPER12) &xDLL] ~ TempStr12(row[]));
foreach(row;g_rgCommandFuncs)
Excel12f(xlfRegister, cast(LPXLOPER12)0, [cast(LPXLOPER12) &xDLL] ~ TempStr12(row[]));
/**
In the following block of code, the Generic drop-down menu is created.
Before creation, a check is made to determine if Generic already
exists. If not, it is added. If the menu needs to be added, memory is
allocated to hold the array of menu items. The g_rgMenu[] table is then
transferred into the newly created array. The array is passed as an
argument to xlfAddMenu to actually add the drop-down menu before the
help menu. As a last step the memory allocated for the array is
released.
This block uses TempStr12() and TempNum12(). Both create a temporary
XLOPER12. The XLOPER12 created by TempStr12() contains the string passed to
it. The XLOPER12 created by TempNum12() contains the number passed to it.
The Excel12f() function frees the allocated temporary memory. Both
functions are part of the framework library.
*/
Excel12f(xlfGetBar, &xTest, [TempInt12(10), TempStr12("Generic"w), TempInt12(0)]);
if (xTest.xltype == xltypeErr)
{
hMenu = GlobalAlloc(GMEM_MOVEABLE,XLOPER12.sizeof * g_rgMenuCols * g_rgMenuRows);
px = pxMenu = cast(LPXLOPER12) GlobalLock(hMenu);
for (i=0; i < g_rgMenuRows; i++)
{
for (j=0; j < g_rgMenuCols; j++)
{
px.xltype = xltypeStr;
px.val.str = TempStr12(g_rgMenu[i][j]).val.str;
px++;
}
}
xMenu.xltype = xltypeMulti;
xMenu.val.array.lparray = pxMenu;
xMenu.val.array.rows = g_rgMenuRows;
xMenu.val.array.columns = g_rgMenuCols;
Excel12f(xlfAddMenu,cast(XLOPER12*)0,[TempNum12(10),cast(LPXLOPER12)&xMenu,TempStr12("Help"w)]);
GlobalUnlock(hMenu);
GlobalFree(hMenu);
}
/**
In this block of code, the Test toolbar is created. Before
creation, a check is made to ensure that Test doesn't already
exist. If it does not, it is created. Memory is allocated to hold
the array containing the toolbar. The information from the g_rgTool[]
table is then transferred into this array. The toolbar is added with
xlfAddToolbar and subsequently displayed with xlcShowToolbar. Finally,
the memory allocated for the toolbar and the XLL filename is released.
This block uses TempInt12(), TempBool12(), and TempMissing12(). All three
create a temporary XLOPER12. The XLOPER12 created by TempInt() contains
the integer passed to it. TempBool12() creates an XLOPER12 containing the
boolean value passed to it. TempMissing12() creates an XLOPER12 that
simulates a missing argument. The Excel12f() function frees the temporary
memory associated with these functions. All three are part of the
framework library.
*/
Excel12f(xlfGetToolbar, &xTest, [TempInt12(1), TempStr12("Test"w)]);
if (xTest.xltype == xltypeErr)
{
hTool = GlobalAlloc(GMEM_MOVEABLE, XLOPER12.sizeof * g_rgToolCols * g_rgToolRows);
px = pxTool = cast(LPXLOPER12) GlobalLock(hTool);
for (i = 0; i < g_rgToolRows; i++)
{
for (j = 0; j < g_rgToolCols; j++)
{
px.xltype = xltypeStr;
px.val.str = TempStr12(g_rgTool[i][j]).val.str; //.makePascalString;
px++;
}
}
xTool.xltype = xltypeMulti;
xTool.val.array.lparray = pxTool;
xTool.val.array.rows = g_rgToolRows;
xTool.val.array.columns = g_rgToolCols;
Excel12f(xlfAddToolbar,cast(XLOPER12*)0,[TempStr12("Test"w),cast(LPXLOPER12)&xTool]);
Excel12f(xlcShowToolbar, cast(XLOPER12*)0, [TempStr12("Test"w), TempBool12(1),
TempInt12(5), TempMissing12(), TempMissing12(), TempInt12(999)]);
GlobalUnlock(hTool);
GlobalFree(hTool);
}
// Free the XLL filename //
Excel12f(xlFree, cast(XLOPER12*)0, [cast(LPXLOPER12) &xTest, cast(LPXLOPER12) &xDLL]);
return 1;
}
/**
xlAutoClose()
Purpose: Microsoft Excel call this function when the DLL is unloaded.
xlAutoClose is called by Microsoft Excel:
- when you quit Microsoft Excel, or
- when a macro sheet calls UNREGISTER(), giving a string argument
which is the name of this XLL.
xlAutoClose is called by the Add-in Manager when you remove this XLL from
the list of loaded add-ins. The Add-in Manager first calls xlAutoRemove,
then calls UNREGISTER("GENERIC.XLL"), which in turn calls xlAutoClose.
xlAutoClose is called by GENERIC.XLL by the function fExit. This function
is called when you exit Generic.
xlAutoClose should:
- Remove any menus or menu items that were added in xlAutoOpen,
- do any necessary global cleanup, and
- delete any names that were added (names of exported functions, and
so on). Remember that registering functions may cause names to
be created.
xlAutoClose does NOT have to unregister the functions that were registered
in xlAutoOpen. This is done automatically by Microsoft Excel after
xlAutoClose returns.
xlAutoClose should return 1.
Parameters:
Returns:
int 1
Comments:
History: Date Author Reason
*/
extern(Windows) int /*WINAPI*/ xlAutoClose()
{
int i;
XLOPER12 xRes;
/**
This block first deletes all names added by xlAutoOpen or
xlAutoRegister12. Next, it checks if the drop-down menu Generic still
exists. If it does, it is deleted using xlfDeleteMenu. It then checks
if the Test toolbar still exists. If it is, xlfDeleteToolbar is
used to delete it.
*/
/**
Due to a bug in Excel the following code to delete the defined names
does not work. There is no way to delete these
names once they are Registered
The code is left in, in hopes that it will be
fixed in a future version.
*/
for (i = 0; i < g_rgWorksheetFuncsRows; i++)
Excel12f(xlfSetName, cast(XLOPER12*)0,[TempStr12(g_rgWorksheetFuncs[i][2])]);
for (i = 0; i < g_rgCommandFuncsRows; i++)
Excel12f(xlfSetName, cast(XLOPER12*)0, [TempStr12(g_rgCommandFuncs[i][2])]);
// Everything else works as documented
Excel12f(xlfGetBar, &xRes, [TempInt12(10), TempStr12("Generic"w), TempInt12(0)]);
if (xRes.xltype != xltypeErr)
{
Excel12f(xlfDeleteMenu, cast(XLOPER12*)0, [TempNum12(10), TempStr12("Generic"w)]);
// Free the XLOPER12 returned by xlfGetBar //
Excel12f(xlFree, cast(XLOPER12*)0, [cast(LPXLOPER12) &xRes]);
}
Excel12f(xlfGetToolbar, &xRes, [TempInt12(7), TempStr12("Test"w)]);
if (xRes.xltype != xltypeErr)
{
Excel12f(xlfDeleteToolbar, cast(XLOPER12*)0, [TempStr12("Test"w)]);
// Free the XLOPER12 returned by xlfGetToolbar //
Excel12f(xlFree, cast(XLOPER12*)0, [cast(LPXLOPER12) &xRes]);
}
import core.runtime:rt_term;
rt_term();
return 1;
}
/**
lpwstricmp()
Purpose:
Compares a pascal string and a null-terminated C-string to see
if they are equal. Method is case insensitive
Parameters:
LPWSTR s First string (null-terminated)
LPWSTR t Second string (byte counted)
Returns:
int 0 if they are equal
Nonzero otherwise
Comments:
Unlike the usual string functions, lpwstricmp
doesn't care about collating sequence.
History: Date Author Reason
*/
int lpwstricmp(const(wchar*) s, const(wchar*) t)
{
int i;
if (wcslen(s) != *t)
return 1;
for (i = 1; i <= s[0]; i++)
{
if (towlower(s[i-1]) != towlower(t[i]))
return 1;
}
return 0;
}
/**
xlAutoRegister12()
Purpose:
This function is called by Microsoft Excel if a macro sheet tries to
register a function without specifying the type_text argument. If that
happens, Microsoft Excel calls xlAutoRegister12, passing the name of the
function that the user tried to register. xlAutoRegister12 should use the
normal REGISTER function to register the function, only this time it must
specify the type_text argument. If xlAutoRegister12 does not recognize the
function name, it should return a #VALUE! error. Otherwise, it should
return whatever REGISTER returned.
Parameters:
LPXLOPER12 pxName xltypeStr containing the
name of the function
to be registered. This is not
case sensitive.
Returns:
LPXLOPER12 xltypeNum containing the result
of registering the function,
or xltypeErr containing #VALUE!
if the function could not be
registered.
Comments:
History: Date Author Reason
*/
extern(Windows) LPXLOPER12 /*WINAPI*/ xlAutoRegister12(LPXLOPER12 pxName)
{
static XLOPER12 xDLL, xRegId;
int i;
/**
This block initializes xRegId to a #VALUE! error first. This is done in
case a function is not found to register. Next, the code loops through
the functions in g_rgFuncs[] and uses lpwstricmp to determine if the
current row in g_rgFuncs[] represents the function that needs to be
registered. When it finds the proper row, the function is registered
and the register ID is returned to Microsoft Excel. If no matching
function is found, an xRegId is returned containing a #VALUE! error.
*/
xRegId.xltype = xltypeErr;
xRegId.val.err = xlerrValue;
for (i=0; i<g_rgWorksheetFuncsRows; i++)
{
if (!lpwstricmp(g_rgWorksheetFuncs[i][0].ptr, pxName.val.str))
{
Excel12f(xlfRegister, cast(XLOPER12*)0,
[cast(LPXLOPER12) &xDLL,
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][0]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][1]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][2]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][3]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][4]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][5]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][6]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][7]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][8]),
cast(LPXLOPER12) TempStr12(g_rgWorksheetFuncs[i][9])]);
/// Free oper returned by xl //
Excel12f(xlFree, cast(XLOPER12*)0,[cast(LPXLOPER12) &xDLL]);
return cast (LPXLOPER12) &xRegId;
}
}
for (i=0; i<g_rgCommandFuncsRows; i++)
{
if (!lpwstricmp(g_rgCommandFuncs[i][0].ptr, pxName.val.str))
{
Excel12f(xlfRegister, cast(XLOPER12*)0,
[cast(LPXLOPER12) &xDLL,
cast(LPXLOPER12) TempStr12(g_rgCommandFuncs[i][0]),
cast(LPXLOPER12) TempStr12(g_rgCommandFuncs[i][1]),
cast(LPXLOPER12) TempStr12(g_rgCommandFuncs[i][2]),
cast(LPXLOPER12) TempStr12(g_rgCommandFuncs[i][3]),
cast(LPXLOPER12) TempStr12(g_rgCommandFuncs[i][4]),
cast(LPXLOPER12) TempStr12(g_rgCommandFuncs[i][5]),
cast(LPXLOPER12) TempStr12(g_rgCommandFuncs[i][6])]);
/// Free oper returned by xl //
Excel12f(xlFree, cast(XLOPER12*)0, [cast(LPXLOPER12) &xDLL]);
return cast(LPXLOPER12) &xRegId;
}
}
return cast(LPXLOPER12) &xRegId;
}
/**
xlAutoAdd()
Purpose:
This function is called by the Add-in Manager only. When you add a
DLL to the list of active add-ins, the Add-in Manager calls xlAutoAdd()
and then opens the XLL, which in turn calls xlAutoOpen.
Parameters:
Returns:
int 1
Comments:
History: Date Author Reason
*/
extern(Windows) int /*WINAPI*/ xlAutoAdd()
{
enum szBuf=format("Thank you for adding GENERIC.XLL\n built on %s at %s",__DATE__,__TIME__);
// Display a dialog box indicating that the XLL was successfully added //
Excel12f(xlcAlert, cast(XLOPER*)0, [TempStr12(szBuf), TempInt12(2)]);
return 1;
}
/**
xlAutoRemove()
Purpose:
This function is called by the Add-in Manager only. When you remove
an XLL from the list of active add-ins, the Add-in Manager calls
xlAutoRemove() and then UNREGISTER("GENERIC.XLL").
You can use this function to perform any special tasks that need to be
performed when you remove the XLL from the Add-in Manager's list
of active add-ins. For example, you may want to delete an
initialization file when the XLL is removed from the list.
Parameters:
Returns:
int 1
Comments:
History: Date Author Reason
*/
extern(Windows) int /*WINAPI*/ xlAutoRemove()
{
// Show a dialog box indicating that the XLL was successfully removed //
Excel12f(xlcAlert, cast(XLOPER*)0,[TempStr12("Thank you for removing GENERIC.XLL!"w),
TempInt12(2)]);
return 1;
}
/**
xlAddInManagerInfo12()
Purpose:
This function is called by the Add-in Manager to find the long name
of the add-in. If xAction = 1, this function should return a string
containing the long name of this XLL, which the Add-in Manager will use
to describe this XLL. If xAction = 2 or 3, this function should return
#VALUE!.
Parameters:
LPXLOPER12 xAction What information you want. One of:
1 = the long name of the
add-in
2 = reserved
3 = reserved
Returns:
LPXLOPER12 The long name or #VALUE!.
Comments:
History: Date Author Reason
*/
extern(Windows) LPXLOPER12 /*WINAPI*/ xlAddInManagerInfo12(LPXLOPER12 xAction)
{
static XLOPER12 xInfo, xIntAction;
//
// This code coerces the passed-in value to an integer. This is how the
// code determines what is being requested. If it receives a 1,
// it returns a string representing the long name. If it receives
// anything else, it returns a #VALUE! error.
//
Excel12f(xlCoerce, &xIntAction,[xAction, TempInt12(xltypeInt)]);
if (xIntAction.val.w == 1)
{
xInfo.xltype = xltypeStr;
xInfo.val.str = TempStr12("Generic Standalone DLL"w).val.str;
}
else
{
xInfo.xltype = xltypeErr;
xInfo.val.err = xlerrValue;
}
//Word of caution - returning static XLOPERs/XLOPER12s is not thread safe
//for UDFs declared as thread safe, use alternate memory allocation mechanisms
return cast(LPXLOPER12) &xInfo;
}
/**
DIALOGMsgProc()
Purpose:
This procedure is associated with the native Windows dialog box that
fShowDialog displays. It provides the service routines for the events
(messages) that occur when the user operates one of the dialog
box's buttons, entry fields, or controls.
Parameters:
HWND hWndDlg Contains the HWND of the dialog box
UINT message The message to respond to
WPARAM wParam Arguments passed by Windows
LPARAM lParam
Returns:
INT_PTR true if message processed, false if not.
Comments:
History: Date Author Reason
*/
extern(Windows) INT_PTR /*CALLBACK*/ DIALOGMsgProc(HWND hWndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
/**
This block is a very simple message loop for a dialog box. It checks for
only three messages. When it receives WM_INITDIALOG, it uses a buffer to