1

Using VS2012, I get the error "CreateObject function was not declared" in this line:

        _cMouseIndicator = CreateObject("twsMouseIndicator.clsMouseIndicator") 

Does anybody know what is going wrong here? I thought that CreateObject was a part of one of the standard libraries.

Thank you for the help!

1 Answer 1

2

If you add this import:

Imports Microsoft.VisualBasic.Interaction

then CreateObject should work.

CreateObject is a leftover from the classic Visual Basic like VB6, ASP, VBA and VBScript.

You can also achieve the same late-binding with this snippet:

Dim _cMouseIndicator As Object

Dim t As Type = Type.GetTypeFromProgID("twsMouseIndicator.clsMouseIndicator", True)
_cMouseIndicator = Activator.CreateInstance(t)

But it would be painful to invoke methods on this object with InvokeMember calls.

I'd advise you to add the library you want as a reference. Visual Studio should handle the ActiveX COM imports with Interop and you can use your library with the new keyword.

Sign up to request clarification or add additional context in comments.

5 Comments

In VB, for either option (CreateObject or Activator.CreateInstance) you need to have Option Strict Off. In C#, the 'dynamic' keyword gives you slack on the relevant object, but without tossing the rest of the code in Option Strict Off coding horror land.
@DaveDoknjas dynamic is very convenient but if you use InvokeMember, CreateInstance should work even with Option Strict On although it causes my eyes to bleed. A working VB.NET example for the type "SAPI.SpVoice" would be: SAPI.GetType().InvokeMember("Speak", Reflection.BindingFlags.InvokeMethod, Nothing, SAPI, New Object() {"Hello world!", 0})
Right - I overlooked that. Totally agree on the bleeding eyes.
@FurkanOmay Thanks for the advice about adding the library. Unfortunately I can not do this because then VS does not use my class as out-of-proc anymore. I am only able to run my class with a different ProcessID, if I use CreateObject.
But I think the Activator approach also works great. Thank you very much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.