-
Notifications
You must be signed in to change notification settings - Fork 4
/
ActiveKOL.pas
2815 lines (2555 loc) · 80.4 KB
/
ActiveKOL.pas
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
{This version is compatible with KOL 3.00+ -- VK}
unit ActiveKOL;{$IFDEF FPC}{$mode delphi}{$H+}{$ENDIF}
interface
uses windows
,messages
,KOL
,ActiveX
,{$IFDEF FPC}ComObj{$ELSE}KOLComObj{$ENDIF}
,err
;
{$I KOLDEF.INC}
{$IFDEF _D6orHigher}
//{$WARN SYMBOL_DEPRECATED OFF}
{$WARN SYMBOL_PLATFORM OFF}
{$IFDEF _D7orHigher}
{$IFnDEF FPC}
{$WARN UNSAFE_TYPE OFF}
{$WARN UNSAFE_CAST OFF}
{$WARN UNSAFE_CODE OFF}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$IFNDEF _D5orHigher}
const
sNoRunningObject = 'Unable to retrieve a pointer to a running object registered with OLE for %s/%s';
{$ENDIF}
type
POleCtl = ^TOleCtl;
TEventDispatch = class(TObject, IUnknown, IDispatch)
private
FControl: POleCtl;
protected
{ IUnknown }
function QueryInterface({$IFDEF FPC}constref{$ELSE}const{$ENDIF}IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
{ IDispatch }
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(dispId: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
property Control: POleCtl read FControl;
public
constructor Create(aControl: POleCtl);
end;
{$IFNDEF _D5orHigher}
TOleEnum = type Integer;
//{$NODEFINE TOleEnum}
{$ENDIF}
TGetStrProc = procedure(const S: string) of object;
TEnumValue = record
Value: Longint;
Ident: {$IFDEF FPC}WideString{$ELSE}string{$ENDIF};
end;
PEnumValueList = ^TEnumValueList;
TEnumValueList = array[0..32767] of TEnumValue;
PEnumPropDesc = ^TEnumPropDesc;
TEnumPropDesc = object(TObj)
private
FDispID: Integer;
FValueCount: Integer;
FValues: PEnumValueList;
public
constructor Create(dispId, ValueCount: Integer;
const TypeInfo: ITypeInfo);
destructor Destroy; virtual;
procedure GetStrings(Proc: TGetStrProc);
function StringToValue(const S: string): Integer;
function ValueToString(V: Integer): string;
end;
PControlData = ^TControlData;
TControlData = record
ClassID: TGUID;
EventIID: TGUID;
EventCount: Longint;
EventDispIDs: Pointer;
LicenseKey: Pointer;
Flags: DWORD;
Version: Integer;
FontCount: Integer;
FontIDs: PDispIDList;
PictureCount: Integer;
PictureIDs: PDispIDList;
Reserved: Integer;
InstanceCount: Integer;
EnumPropDescs: PList;
end;
PControlData2 = ^TControlData2;
TControlData2 = record
ClassID: TGUID;
EventIID: TGUID;
EventCount: Longint;
EventDispIDs: Pointer;
LicenseKey: Pointer;
Flags: DWORD;
Version: Integer;
FontCount: Integer;
FontIDs: PDispIDList;
PictureCount: Integer;
PictureIDs: PDispIDList;
Reserved: Integer;
InstanceCount: Integer;
EnumPropDescs: PList;
FirstEventOfs: Cardinal;
end;
TOleCtlIntfClass = class of TOleCtlIntf;
TOleCtlIntf = class
( TObject
, IUnknown
, IOleClientSite
, IOleControlSite
, IOleInPlaceSite
, IOleInPlaceFrame
, IDispatch
, IPropertyNotifySink
, ISimpleFrameSite
)
private
FRefCount:Integer;
fOleCtl:POleCtl;
procedure GetEventMethod(dispId: TDispID; var Method: TMethod);
protected
{ IUnknown }
function QueryInterface({$IFDEF FPC}constref{$ELSE}const{$ENDIF}IID: TGUID; out Obj): HResult; stdcall; //override;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
{ IOleClientSite }
function SaveObject: HResult; stdcall;
function GetMoniker(dwAssign: Longint; dwWhichMoniker: Longint; out mk: IMoniker): HResult; stdcall;
function GetContainer(out container: IOleContainer): HResult; stdcall;
function ShowObject: HResult; stdcall;
function OnShowWindow(fShow: BOOL): HResult; stdcall;
function RequestNewObjectLayout: HResult; stdcall;
{ IOleControlSite }
function OnControlInfoChanged: HResult; stdcall;
function LockInPlaceActive(fLock: BOOL): HResult; stdcall;
function GetExtendedControl(out disp: IDispatch): HResult; stdcall;
function TransformCoords{$IFDEF FPC}
( var pPtlHimetric:_POINTL;
var pPtfContainer:tagPOINTF;
dwFlags:LongWord
):{$ELSE}
( var ptlHimetric: TPoint;
var ptfContainer: TPointF;
flags: Longint
):{$ENDIF}HResult; stdcall;
function IOleControlSite.TranslateAccelerator = OleControlSite_TranslateAccelerator;
function OleControlSite_TranslateAccelerator{$IFDEF FPC}
( var pMsg:tagMSG;
grfModifiers:LongWord
):{$ELSE}
( msg: PMsg;
grfModifiers:Longint
):{$ENDIF}HResult; stdcall;
function OnFocus(fGotFocus: BOOL): HResult; stdcall;
function ShowPropertyFrame: HResult; stdcall;
{ IOleWindow }
function ContextSensitiveHelp(fEnterMode: BOOL): HResult; stdcall;
{ IOleInPlaceSite }
function IOleInPlaceSite.GetWindow = OleInPlaceSite_GetWindow;
function OleInPlaceSite_GetWindow(out wnd: HWnd): HResult; stdcall;
function CanInPlaceActivate: HResult; stdcall;
function OnInPlaceActivate: HResult; stdcall;
function OnUIActivate: HResult; stdcall;
function GetWindowContext{$IFDEF FPC}
( out ppframe:IOleInPlaceFrame;
out ppdoc:IOleInPlaceUIWindow;
lprcposrect:LPRECT;
lprccliprect:LPRECT;
lpframeinfo:LPOLEINPLACEFRAMEINFO
):{$ELSE}
( out frame: IOleInPlaceFrame;
out doc: IOleInPlaceUIWindow;
out rcPosRect: TRect;
out rcClipRect: TRect;
out frameInfo: TOleInPlaceFrameInfo
):{$ENDIF}HResult; stdcall;
function Scroll{$IFDEF FPC}
( scrollExtant:TSIZE
):{$ELSE}
( scrollExtent:TPoint
):{$ENDIF}HResult; stdcall;
function OnUIDeactivate(fUndoable: BOOL): HResult; stdcall;
function OnInPlaceDeactivate: HResult; stdcall;
function DiscardUndoState: HResult; stdcall;
function DeactivateAndUndo: HResult; stdcall;
function OnPosRectChange{$IFDEF FPC}
( lprcPosRect:LPRect
):{$ELSE}
( const rcPosRect:TRect
):{$ENDIF}HResult; stdcall;
{ IOleInPlaceUIWindow }
function GetBorder(out rectBorder: TRect): HResult; stdcall;
function RequestBorderSpace(const borderwidths: TRect): HResult; stdcall;
function SetBorderSpace{$IFDEF FPC}
( const borderwidths:TRect
):{$ELSE}
( pborderwidths:PRect
):{$ENDIF}HResult; stdcall;
function SetActiveObject(const activeObject: IOleInPlaceActiveObject; pszObjName: POleStr): HResult; stdcall;
{ IOleInPlaceFrame }
function IOleInPlaceFrame.GetWindow = OleInPlaceFrame_GetWindow;
function OleInPlaceFrame_GetWindow(out wnd: HWnd): HResult; stdcall;
function InsertMenus(hmenuShared: HMenu; var menuWidths: TOleMenuGroupWidths): HResult; stdcall;
function SetMenu(hmenuShared: HMenu; holemenu: HMenu; hwndActiveObject: HWnd): HResult; stdcall;
function RemoveMenus(hmenuShared: HMenu): HResult; stdcall;
function SetStatusText(pszStatusText: POleStr): HResult; stdcall;
function EnableModeless(fEnable: BOOL): HResult; stdcall;
function IOleInPlaceFrame.TranslateAccelerator = OleInPlaceFrame_TranslateAccelerator;
function OleInPlaceFrame_TranslateAccelerator(var msg: Windows.TMsg; wID: Word): HResult; stdcall;
{ IDispatch }
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(dispId: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
{ ISimpleFrameSite }
function PreMessageFilter{$IFDEF FPC}
( hwnd:wireHWND;
msg:UInt;
wp:UINT_PTR;
lp:LONG_PTR;
out plResult:LONG_PTR;
out pdwCookie:LongWord
):{$ELSE}
( wnd: HWnd;
msg,wp,lp:Integer;
out res: Integer;
out Cookie: Longint
):{$ENDIF}HResult; stdcall;
function PostMessageFilter{$IFDEF FPC}
( hwnd:wireHWND;
msg:UInt;
wp:UINT_PTR;
lp:LONG_PTR;
out plResult:LONG_PTR;
dwCookie:LongWord
):{$ELSE}
( wnd: HWnd;
msg, wp, lp: Integer;
out res: Integer;
Cookie: Longint
):{$ENDIF}HResult; stdcall;
{ IPropertyNotifySink }
function OnChanged(dispId: TDispID): HResult; virtual; stdcall;
function OnRequestEdit(dispId: TDispID): HResult; virtual; stdcall;
public
property OleCtl: POleCtl read fOleCtl;
constructor Create; virtual;
end;
TOnGetIntfClass = function(): TOleCtlIntfClass of object;
TOleCtl = object(TControl)
private
FOnGetIntfClass: TOnGetIntfClass;
function GetOleObject: Variant;
procedure CreateInstance;
function GetOnLeave: TOnEvent;
procedure SetOnLeave(const Value: TOnEvent);
procedure HookControlWndProc;
procedure SetUIActive(Active: Boolean);
procedure CreateControl;
procedure DestroyStorage;
procedure DestroyControl;
procedure StandardEvent(dispId: TDispID; var Params: TDispParams);
//procedure SetMouseDblClk(const Value: TOnMouse);
procedure SetOnChar(const Value: TOnChar);
protected
//{$IFDEF DELPHI_CODECOMPLETION_BUG}
fNotAvailable: Boolean;
//{$ENDIF}
{$IFNDEF USE_NAMES}
fName: String;
{$ENDIF}
FControlData: PControlData;
FOleObject: IOleObject;
FMiscStatus:{$IFDEF FPC}LongWord{$ELSE}Longint{$ENDIF};
FFonts: PList;
FPictures: PList;
FEventDispatch: TEventDispatch;
fOleCtlIntf: TOleCtlIntf;
FPersistStream: IPersistStreamInit;
FOleInPlaceObject: IOleInPlaceObject;
FOleInPlaceActiveObject: IOleInPlaceActiveObject;
FOleControl: IOleControl;
FUpdatingColor: Boolean;
FUpdatingFont: Boolean;
FUpdatingEnabled: Boolean;
FObjectData: HGlobal;
FControlDispatch: IDispatch;
FPropBrowsing: IPerPropertyBrowsing;
FPropConnection:{$IFDEF FPC}LongWord{$ELSE}Longint{$ENDIF};
FEventsConnection:{$IFDEF FPC}LongWord{$ELSE}Longint{$ENDIF};
fCreatingWnd: Boolean;
procedure Init; virtual;
procedure InitControlData; virtual;
procedure InitControlInterface(const Obj: IUnknown); virtual;
property ControlData: PControlData read FControlData write FControlData;
function GetMainMenu: HMenu;
procedure InvokeEvent(dispId: TDispID; var Params: TDispParams);
procedure D2InvokeEvent(dispId: TDispID; var Params: TDispParams);
procedure DoHandleException;
procedure CreateEnumPropDescs;
procedure DestroyEnumPropDescs;
property OnGetIntfClass: TOnGetIntfClass read FOnGetIntfClass write FOnGetIntfClass;
public
function GetByteProp(Index: Integer): Byte;
function GetColorProp(Index: Integer): TColor;
function GetTColorProp(Index: Integer): TColor;
function GetCompProp(Index: Integer): Comp;
function GetCurrencyProp(Index: Integer): Currency;
function GetDoubleProp(Index: Integer): Double;
function GetIDispatchProp(Index: Integer): IDispatch;
function GetIntegerProp(Index: Integer): Integer;
function GetIUnknownProp(Index: Integer): IUnknown;
function GetWordBoolProp(Index: Integer): WordBool;
function GetTDateTimeProp(Index: Integer): TDateTime;
function GetTFontProp(Index: Integer): PGraphicTool;
function GetOleBoolProp(Index: Integer): TOleBool;
function GetOleDateProp(Index: Integer): TOleDate;
function GetOleEnumProp(Index: Integer): TOleEnum;
function GetTOleEnumProp(Index: Integer): TOleEnum;
function GetOleVariantProp(Index: Integer): OleVariant;
//function GetTPictureProp(Index: Integer): TPicture;
procedure GetProperty(Index: Integer; var Value: TVarData);
function GetShortIntProp(Index: Integer): ShortInt;
function GetSingleProp(Index: Integer): Single;
function GetSmallintProp(Index: Integer): Smallint;
function GetStringProp(Index: Integer): string;
function GetVariantProp(Index: Integer): Variant;
function GetWideStringProp(Index: Integer): WideString;
function GetWordProp(Index: Integer): Word;
procedure SetByteProp(Index: Integer; Value: Byte);
procedure SetColorProp(Index: Integer; Value: TColor);
procedure SetTColorProp(Index: Integer; Value: TColor);
procedure SetCompProp(Index: Integer; const Value: Comp);
procedure SetCurrencyProp(Index: Integer; const Value: Currency);
procedure SetDoubleProp(Index: Integer; const Value: Double);
procedure SetIDispatchProp(Index: Integer; const Value: IDispatch);
procedure SetIntegerProp(Index: Integer; Value: Integer);
procedure SetIUnknownProp(Index: Integer; const Value: IUnknown);
procedure SetName(const Value: String); virtual;
procedure SetWordBoolProp(Index: Integer; Value: WordBool);
procedure SetTDateTimeProp(Index: Integer; const Value: TDateTime);
procedure SetTFontProp(Index: Integer; Value:PGraphicTool);
procedure SetOleBoolProp(Index: Integer; Value: TOleBool);
procedure SetOleDateProp(Index: Integer; const Value: TOleDate);
procedure SetOleEnumProp(Index: Integer; Value: TOleEnum);
procedure SetTOleEnumProp(Index: Integer; Value: TOleEnum);
procedure SetOleVariantProp(Index: Integer; const Value: OleVariant);
procedure SetParent(AParent: PControl); virtual;
//procedure SetTPictureProp(Index: Integer; Value: TPicture);
procedure SetProperty(Index: Integer; const Value: TVarData);
procedure SetShortIntProp(Index: Integer; Value: Shortint);
procedure SetSingleProp(Index: Integer; const Value: Single);
procedure SetSmallintProp(Index: Integer; Value: Smallint);
procedure SetStringProp(Index: Integer; const Value: string);
procedure SetVariantProp(Index: Integer; const Value: Variant);
procedure SetWideStringProp(Index: Integer; const Value: WideString);
procedure SetWordProp(Index: Integer; Value: Word);
function GetEnumPropDesc(dispId: Integer): PEnumPropDesc;
property DragCursor: Boolean read fNotAvailable;
property DragMode : Boolean read fNotAvailable;
property ParentShowHint: Boolean read fNotAvailable;
property PopupMenu: Boolean read fNotAvailable;
property ShowHint: Boolean read fNotAvailable;
property OnDragDrop: Boolean read fNotAvailable;
property OnDragOver: Boolean read fNotAvailable;
property OnEndDrag: Boolean read fNotAvailable;
property OnStartDrag: Boolean read fNotAvailable;
property OnExit: TOnEvent read GetOnLeave write SetOnLeave;
property OleObject: Variant read GetOleObject;
property Name: String read fName write fName;
function CreateWindow: Boolean; virtual;
procedure DblClk;
procedure KeyDown(var Key: Longint; AShift: DWORD);
procedure KeyUp(var Key: Longint; AShift: DWORD);
procedure KeyPress(var Key: KOLChar);
procedure MouseDown(Button: TMouseButton; AShift: DWORD; X, Y: Integer);
procedure MouseMove(AShift: DWORD; X, Y: Integer);
procedure MouseUp(Button: TMouseButton; AShift: DWORD; X, Y: Integer);
property OnKeyPress: TOnChar
read {$IFDEF EVENTS_DYNAMIC} Get_OnChar {$ELSE} EV.fOnChar {$ENDIF}
write SetOnChar;
property OnDblClick: TOnMouse index idx_fOnMouseDblClk
read {$IFDEF EVENTS_DYNAMIC} Get_OnMouseEvent {$ELSE} EV.fOnMouseDblClk {$ENDIF}
write SetOnMouseEvent; // SetMouseDblClk;
destructor Destroy; virtual;
end;
{$IFNDEF _D2orD3}
type
TVariantArray = Array of OleVariant;
TOleServer = class;
TConnectKind = (ckRunningOrNew, // Attach to a running or create a new instance of the server
ckNewInstance, // Create a new instance of the server
ckRunningInstance, // Attach to a running instance of the server
ckRemote, // Bind to a remote instance of the server
ckAttachToInterface); // Don't bind to server, user will provide interface via 'CpnnectTo'
TServerEventDispatch = class(TObject, IUnknown, IDispatch)
private
FServer: TOleServer;
InternalRefCount : Integer;
protected
{ IUnknown }
function QueryInterface({$IFDEF FPC}constref{$ELSE}const{$ENDIF}IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
{ IDispatch }
function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
function GetIDsOfNames(const IID: TGUID; Names: Pointer; NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
function Invoke(dispId: Integer; const IID: TGUID; LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
property Server: TOleServer read FServer;
function ServerDisconnect :Boolean;
public
constructor Create(pServer: TOleServer);
end;
PServerData = ^TServerData;
TServerData = record
ClassID: TGUID; // CLSID of CoClass
IntfIID: TGUID; // IID of default interface
EventIID: TGUID; // IID of default source interface
LicenseKey: Pointer; // Pointer to license string (not implemented)
Version: Integer; // Version of this structure
InstanceCount: Integer; // Instance of the Server running
end;
TOleServer = class(TObject, IUnknown)
private
FServerData:PServerData;
FRefCount:Longint;
FEventDispatch: TServerEventDispatch;
FEventsConnection:{$IFDEF FPC}LongWord{$ELSE}Longint{$ENDIF};
FAutoConnect: Boolean;
FRemoteMachineName: string;
FConnectKind: TConnectKind;
protected
{ IUnknown }
function QueryInterface({$IFDEF FPC}constref{$ELSE}const{$ENDIF}IID: TGUID; out Obj): HResult; stdcall; //override;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
procedure Loaded; //override;
procedure InitServerData; virtual; abstract;
function GetServer: IUnknown; virtual;
procedure ConnectEvents(const Obj: IUnknown);
procedure DisconnectEvents(const Obj: Iunknown);
procedure InvokeEvent(dispId: TDispID; var Params: TVariantArray); virtual;
function GetConnectKind: TConnectKind;
procedure SetConnectKind(ck: TConnectKind);
function GetAutoConnect: Boolean;
procedure SetAutoConnect(flag: Boolean);
property ServerData: PServerData read FServerData write FServerData;
property EventDispatch: TServerEventDispatch read FEventDispatch write FEventDispatch;
public
constructor Create; //(AOwner: TComponent); override;
destructor Destroy; override;
// NOTE: If derived class is generated by TLIBIMP or ImportTypeLibraryCodeGenerator,
// the derived class will also expose a 'ConnectTo(interface)' function.
// You must invoke that method if you're using 'ckAttachToInterface' connection
// kind.
procedure Connect; virtual; abstract;
procedure Disconnect; virtual; abstract;
published
property AutoConnect: Boolean read GetAutoConnect write SetAutoConnect;
property ConnectKind: TConnectKind read GetConnectKind write SetConnectKind;
property RemoteMachineName: string read FRemoteMachineName write FRemoteMachineName;
end;
{$ENDIF}
var
EmptyParam: OleVariant; { "Empty parameter" standard constant which can be
passed as an optional parameter on a dual interface. }
implementation
uses
{$IFDEF FPC}SysUtils,variants;
resourcestring
SBadPropValue = '''%s'' - недопустимое значение свойства';
SCannotActivate = 'Ошибка активации элемента управления OLE';
SNoWindowHandle = 'Не могу получить дескриптор окна элемента управления OLE';
SOleError = 'Ошибка OLE %.8x';
SVarNotObject = 'Variant не ссылается на объект OLE';
SVarNotAutoObject = 'Variant не ссылается на объект automation';
SNoMethod = 'Метод ''%s'' не поддерживается объектом OLE';
SLinkProperties = 'Свойства соединения';
SInvalidLinkSource = 'Не могу подключиться к неверному источнику.';
SCannotBreakLink = 'Операция сброса соединения не поддерживается.';
SLinkedObject = 'Подключен %s';
SEmptyContainer = 'Операция не разрешена на пустом OLE контейнере';
SInvalidVerb = 'Invalid object verb';
SPropDlgCaption = '%s Свойства';
SInvalidStreamFormat = 'Неверный формат потока';
SInvalidLicense = 'Неверная информация о лицензии для %s';
SNotLicensed = 'Информация о лицензии для %s не найдена. Вы не можете использовать этот элемент управления в режиме разработки';
sNoRunningObject = 'Не могу получить ссылку на работающий объект, зарегистрированный в OLE, для %s/%s';
{$ELSE !FPC}
OleConst
;
{$ENDIF}
const
// The following flags may be or'd into the TControlData.Reserved field to override
// default behaviors.
// cdForceSetClientSite:
// Call SetClientSite early (in constructor) regardless of misc status flags
cdForceSetClientSite = 1;
// cdDeferSetClientSite:
// Don't call SetClientSite early. Takes precedence over cdForceSetClientSite and misc status flags
cdDeferSetClientSite = 2;
const
cfBackColor = $00000001;
cfForeColor = $00000002;
cfFont = $00000004;
cfEnabled = $00000008;
cfCaption = $00000010;
cfText = $00000020;
const
MaxDispArgs = 32;
type
PDispInfo = ^TDispInfo;
TDispInfo = packed record
dispId: TDispID;
ResType: Byte;
CallDesc: TCallDesc;
end;
TArgKind = (akDWord, akSingle, akDouble);
PEventArg = ^TEventArg;
TEventArg = record
Kind: TArgKind;
Data: array[0..1] of Integer;
end;
TEventInfo = record
Method: TMethod;
Sender: TObject;
ArgCount: Integer;
Args: array[0..MaxDispArgs - 1] of TEventArg;
end;
function StringToVarOleStr(const S: string): Variant;
begin
VarClear(Result);
TVarData(Result).VOleStr := StringToOleStr(S);
TVarData(Result).VType := varOleStr;
end;
{ TEnumPropDesc }
constructor TEnumPropDesc.Create(dispId, ValueCount: Integer; const TypeInfo: ITypeInfo);
var
I: Integer;
VarDesc: PVarDesc;
XName: WideString;
begin
FDispID := dispId;
FValueCount := ValueCount;
FValues := AllocMem(ValueCount * SizeOf(TEnumValue));
for I := 0 to ValueCount - 1 do
begin
OleCheck(TypeInfo.GetVarDesc(I, VarDesc));
try
OleCheck(TypeInfo.GetDocumentation(VarDesc^.memid, @XName,
nil, nil, nil));
with FValues^[I] do
begin
Value := TVarData(VarDesc^.lpVarValue^).VInteger;
Ident := XName;
while (Length(Ident) > 1) and (Ident[1] = '_') do
Delete(Ident, 1, 1);
end;
finally
TypeInfo.ReleaseVarDesc(VarDesc);
end;
end;
end;
destructor TEnumPropDesc.Destroy;
begin
if FValues <> nil then
begin
Finalize(FValues^[0], FValueCount);
FreeMem(FValues, FValueCount * SizeOf(TEnumValue));
end;
inherited;
end;
procedure TEnumPropDesc.GetStrings(Proc: TGetStrProc);
var
I: Integer;
begin
for I := 0 to FValueCount - 1 do
with FValues^[I] do Proc(Format('%d - %s', [Value, Ident]));
end;
function TEnumPropDesc.StringToValue(const S: string): Integer;
var
I: Integer;
begin
I := 1;
while (I <= Length(S)) and (S[I] in ['0'..'9', '-']) do Inc(I);
if I > 1 then
begin
Result := Str2Int(Copy(S, 1, I - 1));
for I := 0 to FValueCount - 1 do
if Result = FValues^[I].Value then Exit;
end else
for I := 0 to FValueCount - 1 do
with FValues^[I] do
if AnsiCompareText(S, ansiString(Ident)) = 0 then
begin
Result := Value;
Exit;
end;
raise {EOleError}err.exception.CreateResFmt(e_Ole, Integer( @SBadPropValue ), [S]);
end;
function TEnumPropDesc.ValueToString(V: Integer): string;
var
I: Integer;
begin
for I := 0 to FValueCount - 1 do
with FValues^[I] do
if V = Value then
begin
Result := Format('%d - %s', [Value, Ident]);
Exit;
end;
Result := Int2Str(V);
end;
{ TOleCtl }
procedure TOleCtl.CreateControl;
var
Stream: IStream;
CS: IOleClientSite;
X: Integer;
begin
if FOleControl = nil then
try
try // work around ATL bug
X := FOleObject.GetClientSite(CS);
except
X := -1;
end;
if (X <> 0) or (CS = nil) then
OleCheck(FOleObject.SetClientSite(fOleCtlIntf));
if FObjectData = 0 then OleCheck(FPersistStream.InitNew) else
begin
OleCheck(CreateStreamOnHGlobal(FObjectData, False, Stream));
OleCheck(FPersistStream.Load(Stream));
DestroyStorage;
end;
OleCheck(FOleObject.QueryInterface(IOleControl, FOleControl));
OleCheck(FOleObject.QueryInterface(IDispatch, FControlDispatch));
FOleObject.QueryInterface(IPerPropertyBrowsing, FPropBrowsing);
InterfaceConnect(FOleObject, IPropertyNotifySink, fOleCtlIntf, FPropConnection);
InterfaceConnect(FOleObject, FControlData^.EventIID, FEventDispatch, FEventsConnection);
if FControlData^.Flags and cfBackColor <> 0 then
fOleCtlIntf.OnChanged(DISPID_BACKCOLOR);
if FControlData^.Flags and cfEnabled <> 0 then
fOleCtlIntf.OnChanged(DISPID_ENABLED);
if FControlData^.Flags and cfFont <> 0 then
fOleCtlIntf.OnChanged(DISPID_FONT);
if FControlData^.Flags and cfForeColor <> 0 then
fOleCtlIntf.OnChanged(DISPID_FORECOLOR);
FOleControl.OnAmbientPropertyChange(DISPID_UNKNOWN);
fOleCtlIntf.RequestNewObjectLayout;
except
DestroyControl;
raise;
end;
end;
procedure TOleCtl.CreateEnumPropDescs;
function FindMember(dispId: Integer): Boolean;
begin
Result := GetEnumPropDesc(dispId) <> nil;
end;
{var
I: Integer;
begin
for I := 0 to FControlData^.EnumPropDescs^.Count - 1 do
if TEnumPropDesc(FControlData^.EnumPropDescs).FDispID = dispId then
begin
Result := True;
Exit;
end;
Result := False;
end;}
procedure CreateEnum(aTypeDesc:{$IFDEF FPC}TypeDesc{$ELSE}TTypeDesc{$ENDIF}; const TypeInfo: ITypeInfo; dispId: Integer);
var
RefInfo: ITypeInfo;
RefAttr: PTypeAttr;
epd: PEnumPropDesc;
begin
if aTypeDesc.vt <> VT_USERDEFINED then Exit;
OleCheck(TypeInfo.GetRefTypeInfo(aTypeDesc.hreftype, RefInfo));
OleCheck(RefInfo.GetTypeAttr(RefAttr));
try
if RefAttr^.typekind = TKIND_ENUM then
begin
new( epd, Create(dispId, RefAttr^.cVars, RefInfo) );
FControlData^.EnumPropDescs^.Add( epd );
end;
finally
RefInfo.ReleaseTypeAttr(RefAttr);
end;
end;
procedure ProcessTypeInfo(const TypeInfo: ITypeInfo);
var
I: Integer;
RefInfo: ITypeInfo;
TypeAttr: PTypeAttr;
VarDesc: PVarDesc;
FuncDesc: PFuncDesc;
RefType: HRefType;
begin
OleCheck(TypeInfo.GetTypeAttr(TypeAttr));
try
if IsEqualGUID(TypeAttr^.guid, IDispatch) then Exit;
if ((TypeAttr^.typekind = TKIND_INTERFACE) or
(TypeAttr^.wTypeFlags and TYPEFLAG_FDUAL <> 0)) and
(TypeAttr^.wTypeFlags and TYPEFLAG_FNONEXTENSIBLE <> 0) then
begin
OleCheck(TypeInfo.GetRefTypeOfImplType(0, RefType));
OleCheck(TypeInfo.GetRefTypeInfo(RefType, RefInfo));
ProcessTypeInfo(RefInfo);
end;
for I := 0 to TypeAttr^.cVars - 1 do
begin
OleCheck(TypeInfo.GetVarDesc(I, VarDesc));
try
CreateEnum(VarDesc^.elemdescVar.tdesc, TypeInfo, VarDesc^.memid);
finally
TypeInfo.ReleaseVarDesc(VarDesc);
end;
end;
for I := 0 to TypeAttr^.cFuncs - 1 do
begin
OleCheck(TypeInfo.GetFuncDesc(I, FuncDesc));
try
if not FindMember(FuncDesc^.memid) then
case FuncDesc^.invkind of
INVOKE_PROPERTYGET:
CreateEnum(FuncDesc^.elemdescFunc.tdesc, TypeInfo, FuncDesc^.memid);
INVOKE_PROPERTYPUT:
CreateEnum(FuncDesc^.lprgelemdescParam[FuncDesc^.cParams - 1].tdesc,
TypeInfo, FuncDesc^.memid);
INVOKE_PROPERTYPUTREF:
if FuncDesc^.lprgelemdescParam[FuncDesc^.cParams - 1].tdesc.vt = VT_PTR then
CreateEnum(FuncDesc^.lprgelemdescParam[FuncDesc^.cParams - 1].tdesc.{$IFDEF FPC}lptdesc{$ELSE}ptdesc{$ENDIF}^,
TypeInfo, FuncDesc^.memid);
end;
finally
TypeInfo.ReleaseFuncDesc(FuncDesc);
end;
end;
finally
TypeInfo.ReleaseTypeAttr(TypeAttr);
end;
end;
var
TypeInfo: ITypeInfo;
begin
CreateControl;
FControlData^.EnumPropDescs := NewList;
try
OleCheck(FControlDispatch.GetTypeInfo(0, 0, TypeInfo));
ProcessTypeInfo(TypeInfo);
except
DestroyEnumPropDescs;
raise;
end;
end;
procedure TOleCtl.CreateInstance;
var
ClassFactory2: IClassFactory2;
LicKeyStr: WideString;
procedure LicenseCheck(Status: HResult; const Ident: string);
const
{ IClassFactory2 status codes }
CLASS_E_NOTLICENSED = $80040110 { CLASSFACTORY_E_FIRST } + 2;
begin
if Status = CLASS_E_NOTLICENSED then
raise {EOleError}err.exception.CreateFmt(e_Ole, Ident, [SubClassName]);
OleCheck(Status);
end;
begin
if (FControlData^.LicenseKey <> nil) then
begin
OleCheck(CoGetClassObject(FControlData^.ClassID, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, nil, IClassFactory2, ClassFactory2));
LicKeyStr := PWideChar(FControlData^.LicenseKey);
LicenseCheck(ClassFactory2.CreateInstanceLic(nil, nil, IOleObject,
LicKeyStr, FOleObject), SInvalidLicense);
end else
LicenseCheck(CoCreateInstance(FControlData^.ClassID, nil,
CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IOleObject,
FOleObject), SNotLicensed);
end;
procedure CallEventMethod(const EventInfo: TEventInfo); assembler;
asm
PUSH EBX
PUSH ESI
PUSH EBP
MOV EBP,ESP
MOV EBX,EAX
MOV EDX,[EBX].TEventInfo.ArgCount
TEST EDX,EDX
JE @@5
XOR EAX,EAX
LEA ESI,[EBX].TEventInfo.Args
@@1: MOV AL,[ESI].TEventArg.Kind
CMP AL,1
JA @@2
JE @@3
TEST AH,AH
JNE @@3
MOV ECX,[ESI].Integer[4]
MOV AH,1
JMP @@4
@@2: PUSH [ESI].Integer[8]
@@3: PUSH [ESI].Integer[4]
@@4: ADD ESI,12
DEC EDX
JNE @@1
@@5: MOV EDX,[EBX].TEventInfo.Sender
MOV EAX,[EBX].TEventInfo.Method.Data
CALL [EBX].TEventInfo.Method.Code
MOV ESP,EBP
POP EBP
POP ESI
POP EBX
end;
type
PVarArg = ^TVarArg;
TVarArg = array[0..3] of DWORD;
function TOleCtl.CreateWindow: Boolean;
begin
Result := FALSE;
if fHandle <> 0 then
begin
Result := TRUE;
Exit;
end;
if fCreatingWnd then
Exit;
fCreatingWnd := TRUE;
try
CreateControl;
if FMiscStatus and OLEMISC_INVISIBLEATRUNTIME = 0 then
begin
FOleObject.DoVerb(OLEIVERB_INPLACEACTIVATE, nil, fOleCtlIntf, 0, ParentWindow, BoundsRect);
if FOleInPlaceObject = nil then
raise {EOleError}err.exception.CreateResFmt(e_Ole, Integer( @SCannotActivate ), [nil]);
HookControlWndProc;
if {$IFDEF USE_FLAGS} not(F3_Visible in fStyle.f3_Style)
{$ELSE} not fVisible {$ENDIF}
and IsWindowVisible(fHandle) then
ShowWindow(fHandle, SW_HIDE);
Result := TRUE;
end
else
Result := inherited CreateWindow;
finally
fCreatingWnd := FALSE;
end;
end;
procedure TOleCtl.D2InvokeEvent(dispId: TDispID; var Params: TDispParams);
type
TStringDesc = record
PStr: Pointer;
BStr: PBStr;
end;
var
I, J, K, ArgType, ArgCount, StrCount: Integer;
ArgPtr: PEventArg;
ParamPtr: PVarArg;
Strings: array[0..MaxDispArgs - 1] of TStringDesc;
EventInfo: TEventInfo;
begin
fOleCtlIntf.GetEventMethod(dispId, EventInfo.Method);
if Integer(EventInfo.Method.Code) >= $10000 then//Hmm...
begin
StrCount := 0;
try
ArgCount := Params.cArgs;
EventInfo.Sender := fOleCtlIntf;
EventInfo.ArgCount := ArgCount;
if ArgCount <> 0 then
begin
ParamPtr := @Params.rgvarg^[EventInfo.ArgCount];
ArgPtr := @EventInfo.Args;
I := 0;
repeat
Dec(Integer(ParamPtr), SizeOf(TVarArg));
ArgType := ParamPtr^[0] and $0000FFFF;
if ArgType and varTypeMask = varOleStr then
begin
ArgPtr^.Kind := akDWord;
with Strings[StrCount] do
begin
PStr := nil;
if ArgType and varByRef <> 0 then
begin
OleStrToStrVar(PBStr(ParamPtr^[2])^, string(PStr));
BStr := PBStr(ParamPtr^[2]);
ArgPtr^.Data[0] := Integer(@PStr);
end else
begin
OleStrToStrVar(TBStr(ParamPtr^[2]), string(PStr));
BStr := nil;
ArgPtr^.Data[0] := Integer(PStr);
end;
end;
Inc(StrCount);
end else
begin
case ArgType of
varSingle:
begin
ArgPtr^.Kind := akSingle;
ArgPtr^.Data[0] := ParamPtr^[2];
end;
varDouble..varDate:
begin
ArgPtr^.Kind := akDouble;
ArgPtr^.Data[0] := ParamPtr^[2];
ArgPtr^.Data[1] := ParamPtr^[3];
end;
varDispatch:
begin
ArgPtr^.Kind := akDWord;
ArgPtr^.Data[0] := Integer(ParamPtr)
end;
else