Search code examples
f#cominterop

F# & COM api downcasting issue


I have some C# COM Api example that works but fails when translated to F#.

The F# code is :

let list = api.CreateXXX() :?> XXX

The C# conversion is implicit (the object defines the variable as XXX and then initializes it with api.CreateXxxModule() without further conversion.

private XXX list;
[...]
list = api.CreateXXX()

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'ThomsonReuters.Interop.RTX.AdxRtList'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{057B736E-03DF-11D4-99C8-00105AF7EDAD}' failed due to the following error: Cette interface n’est pas prise en charge (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Using the XXXClass instead of XXX for the downcasting didn't work either :

Additional information: Unable to cast COM object of type 'System.__ComObject' to class type 'XXXClass'. COM components that enter the CLR and do not support IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the __ComObject type. Instances of this type cannot be cast to any other class; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

Would anyone have used F# with COM Apis that are usually used with C# and have some tip on these type conversions ?

Thanks a lot !


Solution

  • adding the STAThread attribute to the main function worked in :

    module Main =
    
      [<STAThread>] 
      do
        ...