Alexan
Alexan

Reputation: 8625

referencing ActiveX in F#

If I want to use ActiveX in .NET project, I add it as reference using Add Reference... dialog box and it generates interop assembly, which actually is referenced.

But if I want to use ActiveX from F# Interactive, should I first create F# project, add ActiveX reference and then reference generated interop assembly from F#:

#r "obj\\Interop.MyActiveX.dll" 

or it's possible to reference it directly, for example as in VB Script:

Set mydoc = CreateObject("MyActiveX")

or in PowerShell:

$mydoc = new-object -comobject MyActiveX

Upvotes: 1

Views: 202

Answers (1)

Petr
Petr

Reputation: 4280

You can create an ActiveX control directly like this:

let actxtype = Type.GetTypeFromProgID("MyActiveX")
let instance = Activator.CreateInstance(actxtype)

but your instance will be of type 'obj = System.__ComObject' and you will have to call its methods using reflection something like this:

actxtype.InvokeMember("MethodName", BindingFlags.InvokeMethod | BindingFlags.Public, 
    null, instance, new[] { (*method parameters*) } );

Here is doc on Invoke: http://msdn.microsoft.com/en-us/library/de3dhzwy(v=vs.110).aspx

If you reference an interop assembly you'll have 'normal' type with properties and methods.

Upvotes: 5

Related Questions