4

I am trying to use the functionality of new ActiveXObject() found in JScript.NET in C#. How can I do this?

And don't say that anything you can do with COM objects can be done in C#. If I wanted to do it that way, I already would have.

Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
Bob
  • 185
  • 1
  • 4
  • 13
  • 2
    I think you simply have to reference the COM dll in the Add Reference dialog, and Visual Studio will create a Wrapper for you. in the wrapper, you'll find a class you can instantiate. – Steve B Apr 10 '13 at 14:54
  • Any way to do this just using Notepad? I know I shouldn't but I do. – Bob Apr 10 '13 at 14:54
  • There's so much plumbing that I would highly suggest to use VS. Even if you switch to one of the free version (maybe SharpDevelop or Mono develop can help?) – Steve B Apr 10 '13 at 14:57
  • Ok. I will consider that. Then is it possible without VS? – Bob Apr 10 '13 at 15:17
  • With .Net SDK maybe, and the [`tlbimp.exe` command line](http://msdn.microsoft.com/en-us/library/tt0cf3sx.aspx) – Steve B Apr 10 '13 at 15:20
  • So there is no way to do this purely in the C# code? – Bob Apr 10 '13 at 16:41

1 Answers1

8

You can create instances of COM objects using

Activator.CreateInstance(Type.GetTypeFromProgID(ProgID))

and then work with them using late binding. For example:

using System.Reflection;
...

Type wshType = Type.GetTypeFromProgID("WScript.Shell");
object wshShell = Activator.CreateInstance(wshType);
wshType.InvokeMember("Popup", BindingFlags.InvokeMethod, null, wshShell, new object[] {"Hello, world!"});

or, using C# 4's dynamic keyword:

// NB: Add reference to Microsoft.CSharp.dll
dynamic wshShell = Activator.CreateInstance(Type.GetTypeFromProgID("WScript.Shell"));
wshShell.Popup("Hello, world!");
Helen
  • 87,344
  • 17
  • 243
  • 314