-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
wwDotnetBridge.PRG
2168 lines (1807 loc) · 69.1 KB
/
wwDotnetBridge.PRG
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
SET PROCEDURE TO wwDotnetBridge ADDITIVE
*** This flag defines West Wind's commercial version
*** The open source version should be set to .F.
#DEFINE IS_WESTWIND .F.
#DEFINE IS_LEGACY .T.
#IF IS_WESTWIND
#DEFINE WWC_CLR_HOSTDLL wwipstuff.dll
*** Other dependencies
* .NET Runtime 4.0+ or .NET Core 5+
*** DLLs
* wwIpStuff.dll
* wwDotnetBridge.dll
* NewtonSoft.Json.dll - ToJson()/FromJson() only
#ELSE
#DEFINE WWC_CLR_HOSTDLL clrhost.dll
*** Other dependencies
* .NET Runtime 4.0+ or .NET Core 5+
*** DLLs
* clrhost.dll
* wwDotnetBridge.dll
#ENDIF
************************************************************************
* GetwwDotNetBridge
****************************************
*** Function: Factory class to feed a reusable and stored instance
*** of the wwDotNetBridge class.
*** Assume: Depends on wwutils.prg
*** Pass:
*** Return:
************************************************************************
FUNCTION GetwwDotNetBridge(lcVersion, llUseCom)
IF VARTYPE(__DOTNETBRIDGE) != "O"
IF EMPTY(lcVersion)
lcVersion = "V4"
ENDIF
PUBLIC __DOTNETBRIDGE
__DOTNETBRIDGE = CREATEOBJECT("wwDotNetBridge",lcVersion, llUseCom)
IF VARTYPE(__DOTNETBRIDGE) # "O"
RETURN NULL
ENDIF
ENDIF
RETURN __DOTNETBRIDGE
ENDFUNC
************************************************************************
* InitializeDotnetVersion()
****************************************
*** Function: Explicit method that simply creates a wwDotnetBridge
*** instance. Call this method at the beginning of your
*** app to force the .NET Runtime version that will be
*** used throughout the entire application.
*** Assume: Call this method in your application startup
*** Pass: "V4" or "V2"
*** Return: wwDotnetBridge instance
*** (you don't have to do anything with it)
************************************************************************
FUNCTION InitializeDotnetVersion(lcVersion,llUseCom)
RETURN GetwwDotnetBridge(lcVersion,llUseCom)
ENDFUNC
*************************************************************
DEFINE CLASS wwDotNetBridge AS Custom
*************************************************************
*: Author: Rick Strahl
*: (c) West Wind Technologies, 2007-2016
*:Contact: http://www.west-wind.com
*:Created: 05/10/2007
*:Updated: 6/06/2016
*************************************************************
#IF .F.
*:Help Documentation
*:Topic:
Class wwDotNetBridge
*:Description:
http://www.west-wind.com/webconnection/docs?page=_24n1cfw3a.htm
*:Example:
*:Remarks:
*:SeeAlso:
*:ENDHELP
#ENDIF
*** Custom Properties
*** Stock Properties
oDotNetBridge = null
cErrorMsg = ""
lError = .f.
oLastException = null
FUNCTION oLastException_Access()
RETURN this.oDotNetBridge.LastException
ENDFUNC
*** Default CLR Version. Note: you can override
*** this by specify V2,V4 in the Init() of the class
cClrVersion = "v4.0.30319"
lUseCom = .F.
lThrowOnError = .F.
FUNCTION lThrowOnError_Assign(llValue)
THIS.lThrowOnError = llValue
IF !ISNULL(THIS.oDotnetBridge)
this.oDotNetBridge.IsThrowOnErrorEnabled = llValue
ENDIF
ENDFUNC
*** Don't set here: Use CREATEOBJECT("wwDotNetBridge","V4")
*cClrVersion = "v4.0.30319"
************************************************************************
* wwDotnetBridge :: Init
****************************************
*** Function: Initializes the .NET runtime inside of FoxPro
*** Assume: Calls Load() to actual perform load operation
*** Pass: lcVersion - specific .NET Version or
*** simplified "V2", "V4"
*** llUseCom - if .T. loads .NET Bridge via
*** plain COM interop rather than
*** hosting .NET runtime itself.
*** If you do COM
*** Return: nothing
************************************************************************
FUNCTION Init(lcVersion, llUseCom)
IF !EMPTY(lcVersion)
LOCAL lcShortVer
lcShortVer = UPPER(lcVersion)
DO CASE
CASE lcShortVer == "V4"
lcVersion = "v4.0.30319"
CASE lcShortVer == "V2"
lcVersion = "v2.0.50727"
ENDCASE
this.cClrVersion = lcVersion
ENDIF
this.lUseCom = llUseCom
*** Fail creation if the object couldn't be created
IF ISNULL(this.Load())
ERROR this.cErrorMsg
RETURN .F.
ENDIF
ENDFUNC
* wwDotnetBridge :: Init
************************************************************************
* Load
****************************************
*** Function: Creates an instance of the .NET Bridge component
*** that allows loading of content.
*** Assume: Define as LOCAL obridge as Westwind.WebConnection.wwDotNetBridge
*** Return: nothing
************************************************************************
FUNCTION Load()
LOCAL lcError, lnSize, lnDispHandle, lcPath, lcCmd
IF VARTYPE(this.oDotNetBridge) != "O"
this.SetClrVersion(this.cClrVersion)
IF this.lUseCom
this.oDotNetBridge = CREATEOBJECT("Westwind.wwDotNetBridge")
ELSE
*** Remove the Zone Identifier to 'Unblock'
DECLARE INTEGER DeleteFile IN WIN32API STRING
DeleteFile(FULLPATH("wwDotNetBridge.dll") + ":Zone.Identifier")
#IF IS_WESTWIND
DeleteFile(FULLPATH("Newtonsoft.Json.dll") + ":Zone.Identifier")
DeleteFile(FULLPATH("Markdig.dll") + ":Zone.Identifier")
DeleteFile(FULLPATH("Renci.SshNet.dll") + ":Zone.Identifier")
DeleteFile(FULLPATH("FluentFTP.dll") + ":Zone.Identifier")
#ENDIF
*** Load by filename - assumes the wwDotNetBridge.dll is in the Fox path
DECLARE Integer ClrCreateInstanceFrom IN WWC_CLR_HOSTDLL string, string, string@, integer@
lcError = SPACE(2048)
lnSize = 0
lnDispHandle = ClrCreateInstanceFrom(FULLPATH("wwDotNetBridge.dll"),;
"Westwind.WebConnection.wwDotNetBridge",@lcError,@lnSize)
IF lnDispHandle < 1
lcError = LEFT(lcError,lnSize)
this.SetError( "Unable to load Clr Instance. " + lcError + CHR(13) +;
"Most likely wwDotnetBridge.dll is blocked or you don't have network permissions to load the assembly." + CHR(13)+;
"Search for: 'Unable to load CLR Instance' at: https://webconnection.west-wind.com/docs" )
RETURN NULL
ENDIF
*** Turn handle into IDispatch COM object
this.oDotNetBridge = SYS(3096, lnDispHandle)
*** Explicitly AddRef here - otherwise weird shit happens when objects are released
SYS(3097, this.oDotNetBridge)
IF ISNULL(this.oDotNetBridge)
this.SetError("COM Load Error: Can't access CLR COM reference.")
RETURN null
ENDIF
ENDIF
this.oDotNetBridge.LoadAssembly("System")
this.oDotNetBridge.IsThrowOnErrorEnabled = this.lThrowOnError
ENDIF
RETURN this.oDotNetBridge
ENDFUNC
* CreateDotNetBridge
************************************************************************
* SetClrVersion
****************************************
*** Function: Sets the CLR Version that this runtime uses. Note
*** this method must be called before this.Load() is
*** called and the runtime is instantiated for the very
*** first time.
*** Pass: lcVersion - Clr version in the format of "v4.0.30319"
*** Return: nothing
************************************************************************
PROTECTED FUNCTION SetClrVersion(lcVersion)
DECLARE Integer SetClrVersion IN WWC_CLR_HOSTDLL AS SetClrVersionApi string
SetClrVersionApi(lcVersion)
ENDFUNC
* SetClrVersion
************************************************************************
* Unload
****************************************
*** Function: Unloads the CLR and AppDomain
*** Assume: Don't call this unless you want to explicity force
*** the AppDomain to be unloaded and a new one to be spun
*** up. Generally a single domain should be sufficient.
*** Pass:
*** Return:
************************************************************************
FUNCTION Unload()
IF VARTYPE(this.oDotNetBridge) == "O"
this.oDotNetBridge = NULL
DECLARE Integer ClrUnload IN WWC_CLR_HOSTDLL
ClrUnload()
ENDIF
ENDFUNC
* Unload
FUNCTION EnableThrowOnError()
this.oDotNetBridge.IsThrowOnErrorEnabled = .T.
ENDFUNC
************************************************************************
* CreateInstance
****************************************
*** Function: Creates an instance of a .NET class by full assembly
*** name or local file name.
*** Assume: Relies on wwDotNetBridgeW32.dll and wwDotNetBridge.dll
*** Pass: lcClass - MyNamespace.MyClass
*** lvParmN - Up to 5 Constructor Parameters
*** Return: Instance or NULL
************************************************************************
FUNCTION CreateInstance(lcClass,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
LOCAL lnDispHandle, lnSize, loBridge, loObject, lnParmCount
this.SetError()
loBridge = this.oDotnetBridge
IF ISNULL(loBridge)
RETURN NULL
ENDIF
lnParmCount = PCOUNT()
DO CASE
CASE lnParmCount = 1
loObject = loBridge.CreateInstance(lcClass)
CASE lnParmCount = 2
loObject = loBridge.CreateInstance_OneParm(lcClass,lvParm1)
CASE lnParmCount = 3
loObject = loBridge.CreateInstance_TwoParms(lcClass,lvParm1, lvParm2)
CASE lnParmCount = 4
loObject = loBridge.CreateInstance_ThreeParms(lcClass,lvParm1, lvParm2, lvParm3)
CASE lnParmCount = 5
loObject = loBridge.CreateInstance_FourParms(lcClass,lvParm1, lvParm2, lvParm3, lvParm4)
CASE lnParmCount = 6
loObject = loBridge.CreateInstance_FiveParms(lcClass,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
OTHERWISE
loObject = loBridge.CreateInstance(lcClass)
ENDCASE
IF ISNULL(loObject) OR loBridge.Error
this.SetError( loBridge.ErrorMessage )
RETURN NULL
ENDIF
RETURN loObject
ENDFUNC
************************************************************************
* CreateInstanceOnType
****************************************
*** Function: Creates an instance of a .NET class by full assembly
*** name or local file name.
*** Assume: Relies on wwDotNetBridgeW32.dll and wwDotNetBridge.dll
*** Pass: lcClass - MyNamespace.MyClass
*** lvParmN - Up to 5 Constructor Parameters
*** Return: Instance or NULL
************************************************************************
FUNCTION CreateInstanceOnType(loInstance, lcProperty, lcClass,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
LOCAL lnDispHandle, lnSize, loBridge, loObject, lnParmCount
this.SetError()
loBridge = this.oDotnetBridge
IF ISNULL(loBridge)
RETURN NULL
ENDIF
lnParmCount = PCOUNT()
DO CASE
CASE lnParmCount = 4
loObject = loBridge.CreateInstanceOnType_OneParm(loInstance,lcProperty, lcClass,lvParm1)
CASE lnParmCount = 5
loObject = loBridge.CreateInstanceOnType_TwoParms(loInstance,lcProperty,lcClass,lvParm1, lvParm2)
CASE lnParmCount = 6
loObject = loBridge.CreateInstanceOnType_ThreeParms(loInstance,lcProperty,lcClass,lvParm1, lvParm2, lvParm3)
CASE lnParmCount = 7
loObject = loBridge.CreateInstanceOnType_FourParms(loInstance,lcProperty,lcClass,lvParm1, lvParm2, lvParm3, lvParm4)
CASE lnParmCount = 8
loObject = loBridge.CreateInstanceOnType_FiveParms(loInstance,lcProperty,lcClass,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
OTHERWISE
loObject = loBridge.CreateInstanceOnType(loInstance,lcProperty,lcClass)
ENDCASE
IF ISNULL(loObject) OR loBridge.Error
this.SetError( loBridge.ErrorMessage )
RETURN .F.
ENDIF
RETURN .T.
ENDFUNC
************************************************************************
* CreateInstanceFromFile
****************************************
*** Function: Creates an instance of an assembly from a file on disk
*** without having to call LoadAssembly first.
*** Assume: Be careful with this as it doesn't load dependencies.
*** The safer path is to use LoadAssembly then load the type.
*** Mainly useful for loading things like addins out of diff
*** non-application folders.
*** Pass:
*** Return: object instance on success null or exception on failure
************************************************************************
FUNCTION CreateInstanceFromFile(lcAssemblyFilename, lcTypename, lvParm1, lvParm2, lvParm3)
this.SetError()
loBridge = this.oDotnetBridge
IF ISNULL(loBridge)
RETURN NULL
ENDIF
lcAssemblyFileName = FULLPATH(lcAssemblyFileName)
lnParmCount = PCOUNT()
DO CASE
CASE lnParmCount = 3
loObject = loBridge.CreateAssemblyInstanceFromFile_OneParm(lcAssemblyFilename, lcTypename, lvParm1)
CASE lnParmCount = 4
loObject = loBridge.CreateAssemblyInstanceFromFile_TwoParms(lcAssemblyFilename, lcTypename, lvParm1, lvParm2)
CASE lnParmCount = 5
loObject = loBridge.CreateAssemblyInstanceFromFile_ThreeParms(lcAssemblyFilename, lcTypename, lvParm1, lvParm2, lvParm3)
OTHERWISE
loObject = loBridge.CreateAssemblyInstanceFromFile(lcAssemblyFilename, lcTypename)
ENDCASE
RETURN loObject
ENDFUNC
* CreateInstanceFromFile
************************************************************************
* DisposeInstance
****************************************
*** Function: Disposes an instance of a .NET object by calling
*** IDisposable.Dispose()
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION DisposeInstance(loObject)
THIS.oBridge.DisposeInstance(loObject)
ENDFUNC
* DisposeInstance
************************************************************************
* InvokeMethod
****************************************
*** Function: Invokes an instance method on an object
*** Assume: Use when direct COM access doesn't work for things like collections/generics etc.
*** Pass:
*** Return:
************************************************************************
FUNCTION InvokeMethod(loObject, lcMethod, lvParm1, lvParm2, lvParm3, lvParm4, lvParm5,;
lvParm6, lvParm7, lvParm8, lvParm9, lvParm10,;
lvParm11, lvParm12, lvParm13, lvParm14, lvParm15,;
lvParm16, lvParm17, lvParm18, lvParm19, lvParm20,;
lvParm21, lvParm22, lvParm23, lvParm24)
LOCAL loBridge, lnParms, loResult
this.SetError()
loBridge = this.oDotNetBridge
lnParms = PCOUNT()
loResult = NULL
DO CASE
CASE lnParms = 2
loResult = loBridge.InvokeMethod(loObject, lcMethod)
CASE lnParms = 3
loResult = loBridge.InvokeMethod_OneParm(loObject, lcMethod, lvParm1)
CASE lnParms = 4
loResult = loBridge.InvokeMethod_TwoParms(loObject, lcMethod,lvParm1, lvParm2)
CASE lnParms = 5
loResult = loBridge.InvokeMethod_ThreeParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3)
CASE lnParms = 6
loResult = loBridge.InvokeMethod_FourParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4)
CASE lnParms = 7
loResult = loBridge.InvokeMethod_FiveParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
CASE lnParms = 8
loResult = loBridge.InvokeMethod_SixParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6)
CASE lnParms = 9
loResult = loBridge.InvokeMethod_SevenParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7)
CASE lnParms = 10
loResult = loBridge.InvokeMethod_EightParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8)
CASE lnParms = 11
loResult = loBridge.InvokeMethod_NineParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9)
CASE lnParms = 12
loResult = loBridge.InvokeMethod_TenParms(loObject, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
OTHERWISE
LOCAL loArray as Westwind.WebConnection.ComArray
loArray = this.CreateArray("System.Object")
LOCAL lvParm
FOR lnX = 1 TO lnParms-2
lvParm = EVALUATE("lvParm" + TRANSFORM(lnX))
loArray.AddItem(lvParm)
ENDFOR
RETURN THIS.InvokeMethod_ParameterArray(loObject, lcMethod, loArray)
ENDCASE
IF loBridge.Error
this.SetError(loBridge.ErrorMessage)
ENDIF
RETURN loResult
ENDFUNC
* InvokeMethod
************************************************************************
* InvokeMethod_ParameterArray
****************************************
*** Function: Invokes an instance method on an object
*** Assume: Use when direct COM access doesn't work for things like collections/generics etc.
*** Pass:
*** Return:
************************************************************************
FUNCTION InvokeMethod_ParameterArray
LPARAMETERS loObject, lcMethod, laParms
LOCAL loBridge, loResult, lnX
IF .F.
LOCAL ARRAY laParms[1] && Compiler needs this or compile error
ENDIF
this.SetError()
LOCAL loBridge as Westwind.wwDotNetBridge
loBridge = this.oDotNetBridge
LOCAL loArray as Westwind.WebConnection.ComArray
loArray = this.CreateArray("System.Object")
DO CASE
CASE TYPE("laParms.Instance") # "U"
*** It's already a parameter array
loArray = laParms
CASE TYPE("ALEN(laParms)") = "N"
*** It's a Fox array
lnsize = ALEN(laParms)
FOR lnX = 1 TO lnSize
loArray.AddItem(laParms[lnX])
ENDFOR
CASE TYPE("laParms.Count") = "N"
*** It's a collection
FOR lnX=1 TO laParms.Count
loArray.AddItem(laParms[lnX])
ENDFOR
ENDCASE
loResult = loBridge.InvokeMethod_ParameterArray(loObject, lcMethod, loArray)
IF loBridge.Error
this.SetError(loBridge.ErrorMessage)
ENDIF
RETURN loResult
ENDFUNC
* InvokeMethod
************************************************************************
* InvokeMethodAsync
****************************************
*** Function: Invokes a method in .NET Asynchronously on a new thread
*** Pass: loCallbackEvents - CallbackAsyncEvents object
*** Implement OnCompleted() and OnError()
*** loInstance - Instance of object to call
*** lcMethod - Method on instance to call
*** lvParm1-10 - 0 - 10 parameters
*** Return: nothing
************************************************************************
FUNCTION InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod, lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
LOCAL lnParms
lnParms = PCOUNT()
LOCAL loBridge as wwDotNetBridge
loBridge = this.oDotnetBridge
DO CASE
CASE lnParms = 3
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod)
CASE lnParms = 4
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1)
CASE lnParms = 5
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1,lvParm2)
CASE lnParms = 6
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3)
CASE lnParms = 7
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4)
CASE lnParms = 8
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
CASE lnParms = 9
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6)
CASE lnParms = 10
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7)
CASE lnParms = 11
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8)
CASE lnParms = 12
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9)
CASE lnParms = 13
loBridge.InvokeMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
ENDCASE
ENDFUNC
* InvokeMethodAsync
************************************************************************
* InvokeTaskMethodAsync
****************************************
*** Function:
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod, lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9, lvParm10))
lnParms = PCOUNT()
LOCAL loBridge as wwDotNetBridge
loBridge = this.oDotnetBridge
DO CASE
CASE lnParms = 3
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod)
CASE lnParms = 4
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1)
CASE lnParms = 5
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1,lvParm2)
CASE lnParms = 6
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3)
CASE lnParms = 7
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4)
CASE lnParms = 8
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
CASE lnParms = 9
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6)
CASE lnParms = 10
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7)
CASE lnParms = 11
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8)
CASE lnParms = 12
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9)
CASE lnParms = 13
loBridge.InvokeTaskMethodAsync(loCallbackEvents,loInstance,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
ENDCASE
ENDFUNC
* InvokeTaskMethodAsync
************************************************************************
* InvokeMethodAsync
****************************************
*** Function: Invokes a static method in .NET Asynchronously on a new thread
*** Pass: loCallbackEvents - CallbackAsyncEvents object
*** Implement OnCompleted() and OnError()
*** lcTypeName - .NET Type on which static method lives
*** lcMethod - Method on instance to call
*** lvParm1-10 - 0 - 10 parameters
*** Return: nothing
************************************************************************
FUNCTION InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod, lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
LOCAL lnParms, loBridge as wwDotNetBridge
lnParms = PCOUNT()
loBridge = this.oDotnetBridge
DO CASE
CASE lnParms = 3
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod)
CASE lnParms = 4
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1)
CASE lnParms = 5
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1,lvParm2)
CASE lnParms = 6
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3)
CASE lnParms = 7
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4)
CASE lnParms = 8
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
CASE lnParms = 9
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6)
CASE lnParms = 10
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7)
CASE lnParms = 11
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8)
CASE lnParms = 12
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9)
CASE lnParms = 13
loBridge.InvokeStaticMethodAsync(loCallbackEvents,lcTypeName,lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5, lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
ENDCASE
ENDFUNC
* InvokeMethodAsync
************************************************************************
* GetProperty
****************************************
*** Function: Gets a property from a .NET object
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION GetProperty(loInstance,lcProperty)
RETURN this.oDotNetBridge.GetProperty(loInstance, lcProperty)
ENDFUNC
* GetProperty
************************************************************************
* GetPropertyRaw
****************************************
*** Function: Gets a property from a .NET object without FoxPro
*** type fixups
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION GetPropertyRaw(loInstance,lcProperty, llNoInstanceFixup)
RETURN this.oDotNetBridge.GetPropertyRaw(loInstance, lcProperty,llNoInstanceFixup)
ENDFUNC
* GetPropertyRaw
************************************************************************
* GetIndexedProperty
****************************************
*** Function: Returns a value from an indexed property of an array
*** or a list.
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION GetIndexedProperty(loInstance, lnIndex)
RETURN this.oDotnetBridge.GetIndexedProperty(loInstance,lnIndex)
ENDFUNC
* GetIndexedProperty
************************************************************************
* GetDictionaryItem
****************************************
*** Function: Retrieves an item from an IDictionary instance
*** Assume:
*** Pass: loDictionary
*** lcKey
*** Return: Value for the key or an exception if value doesn't exist
************************************************************************
FUNCTION GetDictionaryItem(loDictionary, lcKey)
RETURN this.oDotnetBridge.GetDictionaryItem(loDictionary, lcKey)
ENDFUNC
* GetDictionaryItem
************************************************************************
* SetProperty
****************************************
*** Function: Sets a property on a .NET object
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION SetProperty(loInstance, lcProperty, lvValue)
this.oDotNetBridge.SetProperty(loInstance, lcProperty, lvValue)
ENDFUNC
* SetProperty
************************************************************************
* InvokeStaticMethod
****************************************
*** Function: Calls a static .NET method with up to 5 parameters
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION InvokeStaticMethod(lcTypeName, lcMethod, lvParm1, lvParm2, lvParm3, lvParm4, lvParm5,;
lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
LOCAL loBridge, lnParms, loResult
this.SetError()
loBridge = this.oDotNetBridge
lnParms = PCOUNT()
loResult = NULL
DO CASE
CASE lnParms = 3
loResult = loBridge.InvokeStaticMethod_OneParm(lcTypeName, lcMethod, lvParm1)
CASE lnParms = 4
loResult = loBridge.InvokeStaticMethod_TwoParms(lcTypeName, lcMethod,lvParm1, lvParm2)
CASE lnParms = 5
loResult = loBridge.InvokeStaticMethod_ThreeParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3)
CASE lnParms = 6
loResult = loBridge.InvokeStaticMethod_FourParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4)
CASE lnParms = 7
loResult = loBridge.InvokeStaticMethod_FiveParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)
CASE lnParms = 8
loResult = loBridge.InvokeStaticMethod_SixParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5,lvParm6)
CASE lnParms = 9
loResult = loBridge.InvokeStaticMethod_SevenParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5,lvParm6, lvParm7)
CASE lnParms = 10
loResult = loBridge.InvokeStaticMethod_EightParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5,lvParm6, lvParm7, lvParm8)
CASE lnParms = 11
loResult = loBridge.InvokeStaticMethod_NineParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5,lvParm6, lvParm7, lvParm8, lvParm9)
CASE lnParms = 12
loResult = loBridge.InvokeStaticMethod_TenParms(lcTypeName, lcMethod,lvParm1, lvParm2, lvParm3, lvParm4, lvParm5,lvParm6, lvParm7, lvParm8, lvParm9, lvParm10)
OTHERWISE
loResult = loBridge.InvokeStaticMethod(lcTypeName, lcMethod)
ENDCASE
IF loBridge.Error
this.SetError(loBridge.ErrorMessage)
ENDIF
RETURN loResult
ENDFUNC
* InvokeStaticMethod
************************************************************************
* GetStaticProperty
****************************************
*** Function: Retrieves a static property value from a type
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION GetStaticProperty(lcType, lcProperty)
LOCAL loBridge, lvResult
this.SetError()
loBridge = this.oDotNetBridge
lvResult = loBridge.GetStaticProperty(lcType, lcProperty)
IF loBridge.Error
this.SetError(loBridge.ErrorMessage)
RETURN NULL
ENDIF
RETURN lvResult
ENDFUNC
* GetStaticProperty
************************************************************************
* GetEnumValue
****************************************
*** Function: Retrieves an Enum value as a number from a string representation
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION GetEnumValue(lcType, lcProperty)
IF !EMPTY(lcProperty)
RETURN this.GetStaticProperty(lcType, lcProperty)
ENDIF
*** Assume we're passing the type and enum name as one string
lnAt = RAT(".",lcType)
IF lnAt < 1
RETURN NULL
ENDIF
lcProperty = SUBSTR(lcType,lnAt+1)
lcType = SUBSTR(lcType,1,lnAt-1)
RETURN this.GetStaticProperty(lcType, lcProperty)
ENDFUNC
* GetEnumValue
************************************************************************
* wwDotnetBridge :: GetEnumString
****************************************
*** Function: Retrieves the enum field name from an enum value
*** Assume:
*** Pass: lvEnumValue - The Enum value to retrieve string for
*** Return:
************************************************************************
FUNCTION GetEnumString(lcEnumTypeName,lvEnumValue)
LOCAL lcValue
lcValue = THIS.oDotNetBridge.GetEnumString(lcEnumTypeName,lvEnumValue)
IF ISNULL(lcValue)
this.SetError(this.oDotNetBridge.ErrorMessage)
RETURN NULL
ENDIF
RETURN lcValue
ENDFUNC
* wwDotnetBridge :: GetEnumString
************************************************************************
* SetStaticProperty
****************************************
*** Function: Sets a static property value on a .NET type
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION SetStaticProperty(lcType,lcProperty,lcValue)
LOCAL loBridge, lvResult
loBridge = this.oDotNetBridge
IF !loBridge.SetStaticProperty(lcType, lcProperty, lcValue)
this.SetError(loBridge.ErrorMessage)
RETURN .F.
ENDIF
RETURN .T.
ENDFUNC
* SetStaticProperty
************************************************************************
* CreateArray
****************************************
*** Function: Creates an array of a given type and size on the specified
*** object instance.
*** Assume:
*** Pass:
*** Return: ComArray object with Instance,Length properties or null
************************************************************************
FUNCTION CreateArray(lvArrayInstanceOrElementTypeString)
LOCAL loComArray, lcType
lcType = VARTYPE(lvArrayInstanceOrElementTypeString)
DO CASE
CASE lcType = "X" OR EMPTY(lvArrayInstanceOrElementTypeString)
loComArray = this.oDotnetBridge.CreateInstance("Westwind.WebConnection.ComArray")
CASE lcType = "C"
loComArray = this.oDotnetBridge.CreateArray(lvArrayInstanceOrElementTypeString)
CASE lcType = "O"
loComArray = this.oDotnetBridge.CreateArrayFromInstance(lvArrayInstanceOrElementTypeString)
OTHERWISE
ERROR "Invalid parameter passed to CreateArray"
ENDCASE
IF ISNULL(loComArray)
THIS.SetError(this.oDotnetBridge.ErrorMessage)
RETURN null
ENDIF
RETURN loComArray
ENDFUNC
* CreateArray
************************************************************************
* CreateComValue
****************************************
*** Function: Creates a COM Value structure
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION CreateComValue(lvValue)
LOCAL loVal
loVal = this.CreateInstance("Westwind.WebConnection.ComValue")
if(PCOUNT()=1)
loVal.SetValue(lvValue)
ENDIF
RETURN loVal
ENDFUNC
* CreateComValue
************************************************************************
* ComArrayToCollection
****************************************
*** Function: Converts a ComArray to a FoxPro collection
*** Pass: loComArray
*** Return: Collection. If null returns empty collection
************************************************************************
FUNCTION ComArrayToCollection(loComArray)
LOCAL loCol, lnX
loCol = CREATEOBJECT("Collection")
IF VARTYPE(loComArray) # "O"
RETURN loCol
ENDIF
FOR lnX = 0 TO loComArray.Count -1
loCol.Add(loComArray.Item(lnX))
ENDFOR
RETURN loCol
ENDFUNC
* ComArrayToCollection
************************************************************************
* CreateArrayOnInstnace
****************************************
*** Function: Creates an array of a given type and size on the specified
*** object instance.
*** Assume:
*** Pass:
*** Return:
************************************************************************
FUNCTION CreateArrayOnInstance(loBaseType, lcType, lnSize)
IF VARTYPE(lnSize) # "N"
IF !this.oDotNetBridge.CreateArrayOnInstanceWithObject(loBaseType, lcType, lnSize)
THIS.SetError(this.oDotnetBridge.ErrorMessage)
RETURN .F.
ENDIF
RETURN .T.
ENDIF
IF !this.oDotNetBridge.CreateArrayOnInstance(loBaseType, lcType, lnSize)
THIS.SetError(this.oDotnetBridge.ErrorMessage)
RETURN .F.
ENDIF
RETURN .T.
ENDFUNC