1

I need to call methods of the IWeatherStation interface which inherits IUnknown from C#. How do I go about it, if it is possible?

Thank you

Simon Mourier
  • 117,251
  • 17
  • 221
  • 269
Anupama
  • 79
  • 1
  • 1
  • 4
  • Yes. But that doc page is inadequate to do it correctly. You can't really assume the member order is correct and the critical [Guid] is missing. Ask the vendor for help, either the IDL or (surely) the .NET wrapper they already have available. – Hans Passant Sep 20 '18 at 13:14

1 Answers1

2

Sure. There is an example here: Example COM Class (C# Programming Guide).

The first thing you should do is locate the real (idl or c/c++) definition of the interface. For this SDK it's located for example in the <setup path>\Lockheed Martin\Prepar3D v4 SDK 4.3.29.25520\inc\PDK\IWeatherSystem.h header file. This is where you will get the methods and the interface id (a.k.a. 'IID').

So in your case, it would be something like:

[Guid("e4452b96-16ba-4e82-9342-aa37af1f5c26")] // IID
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IWeatherStationV430 // IUnknown base interface is implicit
{
    bool IsValid();
    float GetSurfaceWind();
    void SetSurfaceWind(float aVal);
    // etc...
}
Simon Mourier
  • 117,251
  • 17
  • 221
  • 269
  • Thank you very much. For methods like GetCloudAtIndex (__in UINT uIndex, P3D::CloudLayer &aLayer), will I have to create wrapper dlls and add them as reference? – Anupama Sep 20 '18 at 17:55
  • @Anupama - you don't need wrapper dll, you can declare all methods of the interface with C#. You'll have to declare the CloudLayer struct as a C# struct, etc. Note it maybe quite a lot of work... – Simon Mourier Sep 20 '18 at 19:00
  • Thank you very much. I tried but am facing issues. Should i use the concept of coclass? [link]https://msdn.microsoft.com/en-us/ie/aa645736%28v=vs.94%29?f=255&MSPPError=-2147217396#vcwlkcominteroppart1cclienttutorialanchor2 – Anupama Sep 24 '18 at 07:48
  • It depends on the library, but yes, if the .h contains CLSID definitions, it means there are coclasses. You can create/get a reference on an interface either with a coclass (a ComImport class in C#), or though a standard dll export (a DllImport in C#). You can look at example here also: https://stackoverflow.com/questions/14306048/controlling-volume-mixer/14322736#14322736 – Simon Mourier Sep 24 '18 at 08:05
  • I see. Thanks a lot. The header file has only InterfaceId defns for both interfaces and ServiceId defn for WeatherSystem. Can i still try the approach in the msdn link and your example? – Anupama Sep 24 '18 at 08:15