1

We have created a simple C# class library like this (ClassLibrary1):

[ComVisible(true)]
public struct MyTestStruct
{
    public int z;
    public int h;
}

[ComVisible(true)]
public interface IMyTestInterface
{
    void MyTestFunction();
}

[ComVisible(true)]
public class CMyTestCom : IMyTestInterface
{
      public void MyTestFunction()
      {
      }
}

Export a tlb from it and use:

importlib("ClassLibrary1.tlb"); in an idl file.

The problem is that the intarface type (IMyTestInterface) can be used in the idl, but the struct is not accessable and we do not know why.

So this idl can be compiled:

importlib("stdole2.tlb");
importlib("ClassLibrary1.tlb");

[
    object,
    uuid(DB25FC11-C288-4B31-BE11-5BBA6B273D9B),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IMyNativeTestObj : IDispatch
{
    [id(1)] HRESULT MyFunction([in] IMyTestInterface  *Parameter);  
};

And this is not:

importlib("stdole2.tlb"); importlib("ClassLibrary1.tlb");

[
    object,
    uuid(DB25FC11-C288-4B31-BE11-5BBA6B273D9B),
    dual,
    nonextensible,
    pointer_default(unique)
]
interface IMyNativeTestObj : IDispatch
{
    [id(1)] HRESULT MyFunction([in] MyTestStruct  *Parameter);  
};

we have tried the tagMyTestStruct with the same result. If we open the tlb with the OleView "MyTestStruct" is in it.

  • 1
    `struct`s are an interop problem. If you want to use the tooling to auto-generate TLBs and header files, use interfaces. – IInspectable Oct 25 '16 at 12:17
  • The silly way that the C language handles structure tags applies to IDL as well. You have to use `struct MyTestStruct*`. Do be careful with structs, you formally have to use IRecordInfo to access the members. You'll get away with two simple `int` members since about every language picks the same layout for that struct, that might not be the case if the field types are more convoluted. Corrupting the GC heap is no joy. – Hans Passant Oct 25 '16 at 13:03
  • Ok so technikally it is not possible and not suggested anyway. Then we move the declaration to the C++ side into the IDL file or just change the struct to an interface. Thanks. – Viktor Kamarás Oct 26 '16 at 05:46

0 Answers0