|
-
Jul 12th, 2019, 06:03 AM
#13
Fanatic Member
Re: Has anyone had any success implementing IDispatch / IDispatchEx?
 Originally Posted by Schmidt
Well, and for those who want to add such stuff in a "super-easy" way, there's always IActiveScripting-interfaces,
which would then allow even simple Function-adding in a dynamic fashion.
Into an empty VB-Project with a Form1 - add a new Class with its default-name Class1 - and paste the following:
Code:
Option Explicit
Private SC As Object
Private Sub Class_Initialize()
Set SC = CreateObject("MSScriptControl.ScriptControl")
SC.Language = "VBScript"
SC.AddCode "Public foo: foo = ""foo"""
SC.AddCode "Public bar: bar = ""bar"""
End Sub
Public Function Dyn() As Object
Set Dyn = SC.CodeObject
End Function
Public Sub AddProp(PropName$, Optional InitVal)
SC.AddCode "Public " & PropName: If IsMissing(InitVal) Then Exit Sub
If IsObject(InitVal) Then CallByName Dyn, PropName, VbSet, InitVal Else _
CallByName Dyn, PropName, VbLet, InitVal
End Sub
Public Sub AddFunc(FuncName$, Code$, Optional ParamList$)
SC.AddCode "Function " & FuncName & "(" & ParamList & ")" _
& vbCrLf & Code & vbCrLf & "End Function"
End Sub
Ok, and here's appropriate Test-Code for the Form:
Code:
Option Explicit
Private Sub Form_Load()
Dim C1 As New Class1
With C1.Dyn
Debug.Print .foo, .bar 'those were ensured already at class_initialize
C1.AddFunc "foobar", "foobar=foo+bar" 'add a new method
Debug.Print foo, .bar, .foobar
.foo = 1: .bar = 2 'Prop-Value-changes
Debug.Print .foo, .bar, .foobar
C1.AddProp "baz", "baz" 'add two more new properties
C1.AddProp "Form", Me
Debug.Print .baz, .Form.Caption
End With
End Sub
Et voilà - there it is - a dynamically expandable Dispatch-Prop...
Olaf
Hi Olaf,
I am trying to use your code in x64bit windows but the ScriptControl no longer works in x64bit.
Do you have any suggestions ?
Regards.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|