-
- VBForums
- Visual Basic
- Visual Basic 6 and Before
- Has anyone had any success implementing IDispatch / IDispatchEx?
-
Jun 20th, 2019,03:13 PM #1 Thread Starter Fellow member Has anyone had whatever success implementing IDispatch / IDispatchEx? Recently I've found myself wanting to implement the IDispatchEx interface which would allow me to create objects 'on the wing'. The cadre benefit of which is for syntax sugar: Code: Dim person as object set person = new CDynamic person.age = 22 person.hair.color = "brown" person.hair.manner = "shaggy" Or even, a less ambitious dynamic IDispatch implementation: Code: Dim person as object prepare person = new CDynamic person.addProperties("age","pilus") gear up person.pilus = new CDynamic person.hair.addProperties("colour","style") '... person.age = 22 person.hair.color = "brown" person.hair.style = "shaggy" This would exist really handy as a developer'due south tool, especially in making dev friendly frameworks without bloating the codebase too much. Recently I've been trying to implement these interfaces using IFauxInterface but earlier I pursue this any further I figured I'd ask here to run into if anyone else has tried / got anywhere with this? -
Jun 20th, 2019,03:52 PM #2 Re: Has anyone had any success implementing IDispatch / IDispatchEx? Search this forum for the keyword: CreateStdDispatch. In my recent codebank submission, I apply the API quite a bit to create COM objects on the fly. But "the trick" posted some examples which may autumn into what you are attempting (search for that keyword to find his posts). CreateStdDispatch tin can exist quite circuitous, has limitations, and documentation/examples in the wild are sparse. -
Jun 20th, 2019,07:47 PM #3 Re: Has anyone had any success implementing IDispatch / IDispatchEx? -
Jun 20th, 2019,07:58 PM #4 Re: Has anyone had any success implementing IDispatch / IDispatchEx? @dreammanor. I think the OP wants to include ability to become/set properties and methods. -
Jun 20th, 2019,08:22 PM #5 Re: Has anyone had any success implementing IDispatch / IDispatchEx? Aye, he wants VB6 to gain the ability of JavaScript-like dynamic backdrop (even dynamic objects). If this can exist accomplished, the capabilities of VB6 will increase dramatically. Edit: I've been researching scripting languages (mainly JavaScript) recently. The ability of JavaScript to dynamically create objects is really useful. Last edited past dreammanor; Jun 20th, 2022 at 08:27 PM. -
Jun 20th, 2019,11:14 PM #half dozen Re: Has anyone had any success implementing IDispatch / IDispatchEx? -
Jun 21st, 2019,02:32 AM #seven Thread Starter Member Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted by LaVolpe Search this forum for the keyword: CreateStdDispatch. In my recent codebank submission, I apply the API quite a bit to create COM objects on the fly. But "the pull a fast one on" posted some examples which may fall into what y'all are attempting (search for that keyword to detect his posts). CreateStdDispatch can be quite circuitous, has limitations, and documentation/examples in the wild are thin. Ah you are totally right! I recall coming across CreateStdDispatch a few weeks agone, remember thinking "Man this looks like it'll practise what I need it to". I think I chickened out afterward seeing the full lack of documentation and examples though lol... Originally Posted by The play a joke on Thank you! This will be a super corking ground to work off of! Should make information technology pretty easy Thanks! -
Jun 21st, 2019,06:39 AM #8 Re: Has anyone had whatever success implementing IDispatch / IDispatchEx? Originally Posted by LaVolpe @dreammanor. I recall the OP wants to include ability to get/gear up properties and methods. I thought, that was exactly what example-folder vii (Dynamic usage of vbIDispatch) of the "vbFriendlyInterfaces"-Demo contains. Here the complete Code of that "Dyn-Props-Demo" as information technology exists in fTest.frm: Code: Private Sub Form_Click() AutoRedraw = True: Cls Dim PS As New cPropStorage With PS.Props 'PS.Props will accept any Property-Proper noun you throw at it, to "store things" (As Variant) .foo = "foo" .bar = "bar" .foobar = .foo & .bar '<- read-out-test for the dynamic props .foo and .bar .SomeLong = 123 'make full-in some long... .SomeLong = 456 '<- and now test for over-writing an existing Value under the same PropertyName Prepare .TheForm = Me 'test for storing an Object-Reference (in this instance in .TheForm) 'Ok, now the Examination-PrintOuts for the higher up Print "foo: "; .foo Print "bar: "; .bar Print "foobar: "; .foobar Impress "SomeLong: "; .SomeLong Impress "TheForm.Caption: "; .TheForm.Caption End With Terminate Sub And hither the consummate Code of cPropStorage.cls, which provides that dynamic IDispatch-Implementation: Code: Option Explicit Implements vbIUnknown Implements vbIDispatch Private mDict Every bit New Scripting.Dictionary, VStorage() As Variant Public Role Props() Equally Object 'our IDispatch-supporting DispObject, which allows LateBound Method-Calling "per Dot" vbI.NewInstance vbI.pVT(vtbl_IDispatch), Me, VarPtr(Props) Finish Role '************* IUnknown-Implementation ***************** Individual Sub vbIUnknown_QueryInterface(UserData Equally Long, ByVal pVTable As Long, RefCount As Long, sReqIID As String, Unk Every bit stdole.IUnknown) If vbI.IIDsEqual(sReqIID, vbI.sIID_IDispatch) Then RefCount = RefCount + 1 Finish Sub Private Sub vbIUnknown_Terminate(UserData Every bit Long, ByVal pVTable As Long) Cease Sub '************* IDispatch-Implementation **************** Private Part vbIDispatch_GetIDForMemberName(UserData As Long, ByVal pVTable As Long, MemberName Every bit String) Equally Long If mDict.Exists(MemberName) Then vbIDispatch_GetIDForMemberName = mDict(MemberName) Else mDict.Add MemberName, mDict.Count + 1 vbIDispatch_GetIDForMemberName = mDict.Count ReDim Preserve VStorage(1 To mDict.Count) End If Cease Part Private Function vbIDispatch_Invoke(UserData As Long, ByVal pVTable As Long, ByVal DispID Equally Long, ByVal CallType As VbCallType, VResult As Variant, ParamArray P() Every bit Variant) As vbInterfaces.HRESULT If DispID < one Or DispID > mDict.Count And so vbIDispatch_Invoke = DISP_E_MEMBERNOTFOUND: Exit Role If CallType And (VbGet Or VbMethod) Then 'handle the read-out-requests If IsObject(VStorage(DispID)) Then Prepare VResult = VStorage(DispID) Else VResult = VStorage(DispID) ElseIf CallType = VbLet Then VStorage(DispID) = P(0) ElseIf CallType = VbSet Then Set VStorage(DispID) = P(0) End If Terminate Role So that's the easy mode, as the vbFriendlyInterfaces.dll provides it (in a generically usable fashion). Olaf -
Jun 21st, 2019,06:48 AM #9 Re: Has anyone had any success implementing IDispatch / IDispatchEx? Well, and for those who desire to add such stuff in a "super-piece of cake" way, there'south always IActiveScripting-interfaces, which would then allow even simple Part-adding in a dynamic fashion. Into an empty VB-Project with a Form1 - add a new Grade with its default-name Class1 - and paste the following: Code: Selection Explicit Private SC Equally 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 Part Dyn() As Object Set Dyn = SC.CodeObject End Function Public Sub AddProp(PropName$, Optional InitVal) SC.AddCode "Public " & PropName: If IsMissing(InitVal) Then Go out 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 Office" Terminate Sub Ok, and here'southward appropriate Test-Code for the Grade: Code: Option Explicit Individual Sub Form_Load() Dim C1 As New Class1 With C1.Dyn Debug.Impress .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 backdrop C1.AddProp "Form", Me Debug.Print .baz, .Form.Caption End With Stop Sub Et voil� - there it is - a dynamically expandable Dispatch-Prop... Olaf -
Jun 21st, 2019,07:25 AM #10 Thread Starter Member Re: Has anyone had whatsoever success implementing IDispatch / IDispatchEx? Originally Posted by Schmidt ... And so that'southward the easy way, as the vbFriendlyInterfaces.dll provides it (in a generically usable fashion). Olaf Hullo Olaf, Gotta say your vbFriendlyInterfaces dll does indeed look very prissy! That said I'm not sure if you can easily attach methods to your IDispatch implementation without implementing TypeInfo also. (Although this is untested from me as of right at present) Regardless, in my item use case, I want to (equally and where possible) steer abroad from using any external DLLs, (easier distribution). I'm as well trying to steer abroad from ScriptControl specifically who's VBScript evaluation is actually disabled on most computers I have used, and whos JScript is unlikely to be supported forever either. This is why I was initially looking into IFauxInterface as this quite literally builds the implementation within VB. That said the DLL version is very neat. Perchance if the worst comes to the worst I can download the dll at runtime to Appdata. -
Jun 21st, 2019,02:02 PM #11 Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted by sancarn Gotta say your vbFriendlyInterfaces dll does indeed look very nice! That said I'k not sure if you can easily attach methods to your IDispatch implementation without implementing TypeInfo too. (Although this is untested from me as of right now) Since Properties are already "Methods" the Implementation of a "true Function" is of course equally possible: Why not brand a fast test with the Demo 7-Code: At the top of vbIDispatch_GetIDForMemberName add together a new, single line: Code: Private Function vbIDispatch_GetIDForMemberName(UserData As Long, ByVal pVTable As Long, MemberName Equally Cord) As Long If UCase$(MemberName) = "MYFUNC" Then vbIDispatch_GetIDForMemberName = 10000: Exit Function ... At the top of vbIDispatch_Invoke add together a new, unmarried line: Code: Private Function vbIDispatch_Invoke(UserData Equally Long, ByVal pVTable As Long, ByVal DispID Every bit Long, ByVal CallType Every bit VbCallType, VResult As Variant, ParamArray P() Equally Variant) As vbInterfaces.HRESULT If DispID = 10000 And then VResult = Join(P, "_"): Exit Function 'MyFunc Joins all passed Params with an UnderScore-Char ... And that's it... "MyFunc" was successfully implemented (without any extra TypeInfos). You lot tin test it in the Form inside the With-Block by doing: Debug.Print .MyFunc("abc", 1, two, 3, "xyz") '<-- returns and Prints out: abc_1_2_3_xyz Originally Posted past sancarn Regardless, in my particular use example, I want to (as and where possible) steer abroad from using any external DLLs, (easier distribution). I'm too trying to steer abroad from ScriptControl specifically who'due south VBScript evaluation is really disabled on most computers I have used, and whos JScript is unlikely to exist supported forever either. Did yous really deactivate VBScript on your Reckoner(s)? If yes, how did y'all do that - and why did yous get out JScript alone in that instance? Im asking, because a "deactivated VBScript" usually leaves the vbscript.dll untouched in the System32 or SysWOW64 folders (if you remove them, there's a good take chances MS volition re-install them on the side by side system-update or system-check). And I've mentioned IActiveScript in my first post, because when you lot implement these Scripting-interfaces by hand, yous will non demand the MS-ScriptControl - simply but an installed vbsript.dll or jscript.dll in the SysFolder. I still consider that solution the near promising i for your case, because when you lot think near information technology: - dynamic backdrop are like shooting fish in a barrel also with other methods (east.g. via vbFriendlyInterfaces.dll) - merely for "true dynamic functions" which do a chip more than "single-line math-ops or concats" to produce a issue, .. y'all'll need either a dynamic compiler, or an interpreter ... menstruum. - the Scripting-stuff is also interesting because information technology comes in both: 32Bit- and a 64Bit implementations (preinstalled on the organization) - also - you're not restricted to VBScript - the jscript.dll tin can provide basically the aforementioned stuff I've demonstrated with VbScript in my example Olaf -
Jun 21st, 2019,04:14 PM #12 Thread Starter Member Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted by Schmidt And that'due south it... "MyFunc" was successfully implemented (without any extra TypeInfos). Lol well that's adulterous! But indeed yous can do that. Originally Posted by Schmidt Did you actually deactivate VBScript on your Computer(s)? I did not practise anything of the sort. On all systems I have used the following code: Code: Dim sc every bit object: set sc = CreateObject("MSScriptControl.ScriptControl") sc.Language = "VBScript" I have got the error. Code: Run-fourth dimension fault '380': A script engine for the specified linguistic communication can not be created JScript seems to work, not sure why, but yeah... To be fair, both times I tried this on Win10 computers, and so that might also have something to do with it... If one doesn't have to rely on MSScriptControl.ScriptControl then mayhap that is indeed an choice also. Originally Posted past Schmidt - merely for "truthful dynamic functions" which do a bit more than "single-line math-ops or concats" to produce a result, .. yous'll need either a dynamic compiler, or an interpreter ... period. Yep I was actually planning on implementing a VB6-like language --> Bytecode compiler + interpreter. Preferably something that would let syntax customisation. Mainly for Lambda expressions... Anyhow. I'll definitely be giving all the suggestions a go and ultimately it'll be which always is more optimal (performance wise). This is a pretty fundamental slice of the architecture of the API so I want it to be as fast as possible. And so thanks all for suggestions! -
Jun 21st, 2019,07:44 PM #13 New Fellow member Re: Has anyone had any success implementing IDispatch / IDispatchEx? Sancarn, I've not tested every tutorial module, merely the fast majority of Olaf'due south vbFriendly interface's examples work perfectly from the VBA IDE (with simply a couple of minor tweaks to cover the differences in the langauges). I also dont think you need the ITypelib reference explicitly in your code, since (Olaf correct me if i'm wrong) you are really directly inserting the desired IID at runtime to ensure the correct blazon association by the car? -
Jul 12th, 2019,06:03 AM #14 Fanatic Member Re: Has anyone had whatsoever success implementing IDispatch / IDispatchEx? Originally Posted by Schmidt Well, and for those who desire to add such stuff in a "super-easy" fashion, there'south e'er IActiveScripting-interfaces, which would then allow even elementary Office-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: Lawmaking: Option Explicit Private SC Every bit Object Private Sub Class_Initialize() Ready 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 Finish Office Public Sub AddProp(PropName$, Optional InitVal) SC.AddCode "Public " & PropName: If IsMissing(InitVal) And then Exit Sub If IsObject(InitVal) Then CallByName Dyn, PropName, VbSet, InitVal Else _ CallByName Dyn, PropName, VbLet, InitVal Terminate Sub Public Sub AddFunc(FuncName$, Lawmaking$, Optional ParamList$) SC.AddCode "Function " & FuncName & "(" & ParamList & ")" _ & vbCrLf & Code & vbCrLf & "End Role" End Sub Ok, and here's appropriate Test-Code for the Class: Code: Option Explicit Individual Sub Form_Load() Dim C1 Equally 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 together two more than new backdrop C1.AddProp "Form", Me Debug.Print .baz, .Form.Caption End With End Sub Et voil� - at that place it is - a dynamically expandable Dispatch-Prop... Olaf Hi Olaf, I am trying to use your code in x64bit windows just the ScriptControl no longer works in x64bit. Do yous take any suggestions ? Regards. -
Jul 12th, 2019,02:28 PM #15 Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted by JAAFAR I am trying to utilize your code in x64bit windows just the ScriptControl no longer works in x64bit. I don't know where you got that idea. It works fine on the 64bit PC I just tried it on. -
Jul twelfth, 2019,08:07 PM #sixteen Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted by dilettante I don't know where you got that idea. It works fine on the 64bit PC I just tried it on. Sure... @JAAFAR Judging from your terminal postings, you're probably trying to use it from within a 64Bit- Procedure (every bit eastward.g. "64Bit-Excel-VBA"). First option: ...utilise the 32Bit-Script-COMponent with COM-Marshaling (either via ActiveX-Exe-Instancing or the DllSurrogate-mechanism). Second option: ...google for VB6-implementations of the IActiveScripting-interfaces (there are a few out in that location) - and adapt these sources for VBA-64. Olaf -
Jul 12th, 2019,08:56 PM #17 Re: Has anyone had whatsoever success implementing IDispatch / IDispatchEx? VBA questions belong in the Function Development forum. -
Jul 12th, 2019,11:30 PM #xviii Fanatic Member Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted past dilettante I don't know where you got that idea. It works fine on the 64bit PC I just tried information technology on. Deplorable, I meant to say 64bit process not 64 Bone. -
Jul 12th, 2019,11:38 PM #19 Fanatic Member Re: Has anyone had whatsoever success implementing IDispatch / IDispatchEx? Originally Posted past dilettante VBA questions belong in the Office Development forum. I know I am not supposed to ask questions related to vba in this forum but the truth is I inappreciably ever get whatever answers when I post in the Office Developent department.... Despite me not using vb6, most of my questions in this forum are API related. Regards. -
Jul 12th, 2019,11:39 PM #20 Fanatic Member Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted past Schmidt Sure... @JAAFAR Judging from your concluding postings, you're probably trying to use information technology from inside a 64Bit- Process (as e.g. "64Bit-Excel-VBA"). Outset option: ...use the 32Bit-Script-COMponent with COM-Marshaling (either via ActiveX-Exe-Instancing or the DllSurrogate-machinery). Second option: ...google for VB6-implementations of the IActiveScripting-interfaces (there are a few out at that place) - and adapt these sources for VBA-64. Olaf Cheers very much. The second option sounds interesting... I'll wait into it and see if I tin make information technology work for me. Regards. -
Jul 13th, 2019,10:06 AM #21 Re: Has anyone had whatsoever success implementing IDispatch / IDispatchEx? Some forums are less active than others...well, most, really. Even so, that's what happens if everybody decides to post questions in the wrong forum. My usual deadening signature: Nix -
Sep 2nd, 2020,06:03 PM #22 New Fellow member Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted past Schmidt I idea, that was exactly what instance-binder 7 (Dynamic usage of vbIDispatch) of the "vbFriendlyInterfaces"-Demo contains. Here the complete Code of that "Dyn-Props-Demo" equally information technology exists in fTest.frm: Code: Private Sub Form_Click() AutoRedraw = Truthful: Cls Dim PS Equally New cPropStorage With PS.Props 'PS.Props will accept whatsoever Holding-Name yous throw at it, to "shop things" (As Variant) .foo = "foo" .bar = "bar" .foobar = .foo & .bar '<- read-out-test for the dynamic props .foo and .bar .SomeLong = 123 'fill-in some long... .SomeLong = 456 '<- and now test for over-writing an existing Value under the same PropertyName Set .TheForm = Me 'test for storing an Object-Reference (in this case in .TheForm) 'Ok, now the Examination-PrintOuts for the to a higher place Print "foo: "; .foo Print "bar: "; .bar Impress "foobar: "; .foobar Impress "SomeLong: "; .SomeLong Impress "TheForm.Caption: "; .TheForm.Caption Finish With End Sub And here the complete Lawmaking of cPropStorage.cls, which provides that dynamic IDispatch-Implementation: Lawmaking: Selection Explicit Implements vbIUnknown Implements vbIDispatch Private mDict As New Scripting.Dictionary, VStorage() As Variant Public Role Props() As Object 'our IDispatch-supporting DispObject, which allows LateBound Method-Calling "per Dot" vbI.NewInstance vbI.pVT(vtbl_IDispatch), Me, VarPtr(Props) End Part '************* IUnknown-Implementation ***************** Private Sub vbIUnknown_QueryInterface(UserData Every bit Long, ByVal pVTable As Long, RefCount As Long, sReqIID As Cord, Unk Equally stdole.IUnknown) If vbI.IIDsEqual(sReqIID, vbI.sIID_IDispatch) And so RefCount = RefCount + 1 End Sub Private Sub vbIUnknown_Terminate(UserData As Long, ByVal pVTable Equally Long) Stop Sub '************* IDispatch-Implementation **************** Individual Function vbIDispatch_GetIDForMemberName(UserData Equally Long, ByVal pVTable As Long, MemberName Equally String) As Long If mDict.Exists(MemberName) Then vbIDispatch_GetIDForMemberName = mDict(MemberName) Else mDict.Add MemberName, mDict.Count + i vbIDispatch_GetIDForMemberName = mDict.Count ReDim Preserve VStorage(1 To mDict.Count) End If End Function Private Function vbIDispatch_Invoke(UserData As Long, ByVal pVTable Every bit Long, ByVal DispID As Long, ByVal CallType As VbCallType, VResult Every bit Variant, ParamArray P() As Variant) As vbInterfaces.HRESULT If DispID < 1 Or DispID > mDict.Count Then vbIDispatch_Invoke = DISP_E_MEMBERNOTFOUND: Exit Function If CallType And (VbGet Or VbMethod) Then 'handle the read-out-requests If IsObject(VStorage(DispID)) Then Fix VResult = VStorage(DispID) Else VResult = VStorage(DispID) ElseIf CallType = VbLet Then VStorage(DispID) = P(0) ElseIf CallType = VbSet Then Set VStorage(DispID) = P(0) Stop If End Function And so that'southward the easy way, as the vbFriendlyInterfaces.dll provides it (in a generically usable fashion). Olaf vbFriendlyInterfaces seems pretty helpful and demo #7 worked for me. Is there a way to handle "dynamic properties" without using the Props() Function? Lawmaking: Dim PS As New cPropStorage PS.foo = "foo" Print PS.foo -
Sep 3rd, 2020,05:41 AM #23 Re: Has anyone had any success implementing IDispatch / IDispatchEx? Originally Posted by ekinated vbFriendlyInterfaces seems pretty helpful and demo #7 worked for me. Is there a fashion to handle "dynamic properties" without using the Props() Function? Yeah, the cDynFactory-Class in posting #239 hither volition support that (forth with dynamic functions): https://world wide web.vbforums.com/showthread....=one#post5469025 HTH Olaf -
- VBForums
- Visual Basic
- Visual Basic 6 and Earlier
- Has anyone had whatsoever success implementing IDispatch / IDispatchEx?
Tags for this Thread Posting Permissions - You lot may not mail service new threads
- You may not postal service replies
- You may non post attachments
- You may not edit your posts
- BB lawmaking is On
- Smilies are On
- [IMG] lawmaking is On
- [VIDEO] code is On
- HTML lawmaking is Off
Forum Rules | Click Hither to Expand Forum to Full Width |
0 Response to "Vb6 Err 380 a Script Engine for the Specified Language Can Not Be Created"
Post a Comment