1
votes

I use Visual Studio 2015 and I have a C# Application-Project that defines a COM-Interface and generates a .tlb file on compilation. Now I want to import that Csharp.tlb into an idl.

MyLibrary.idl:

import "oaidl.idl";
import "ocidl.idl";
import "Cplusplus.idl";

library MyLibrary
{
  importlib("stdole32.tlb");
  importlib("stdole2.tlb");
  importlib("Csharp.tlb");

  interface IMyCOM : IDispatch
  {
    [propget, id(1)]
    HRESULT CpluplusObject
    (
      [out,retval] ICplusplusObject** cplusplusObject
    );

    [propget, id(2)]
    HRESULT CsharpObject
    (
      [out, retval] ICsharpObject** csharpObject
    );
  }

  coclass MyCOM
  {
    [default] interface IMyCOM;
  };
}

During compilation I get an error

C3646 'csharpObject': unknown override specifier in MyLibrary.tlh

MyLibrary.tlh was auto generated by the compilation and looks as the following

MyLibrary.tlh:

#pragma once
#pragma pack(push, 8)

#include <comdef.h>

namespace MyLibrary {

  struct __declspec(uuid("8e664998-bc93-48e7-adcc-84fc8598cd5d"))
  /* dual interface */ ICplusplusObject;

  _COM_SMARTPTR_TYPEDEF(ICplusplusObject, __uuidof(ICplusplusObject));

  struct __declspec(uuid("388ebf11-05c8-4b86-b2bd-60f0ef38695e"))
  IMyLibrary : IDispatch
  {
    __declspec(property(get=GetCplusplusObject))
    ICplusplusObjectPtr cplusplusObject;
    __declspec(property(get=GetCsharpObject))
    ICsharpObjectPtr csharpObject;

    ICplusplusObjectPtr GetCplusplusObject ( );
    ICsharpObjectPtr GetCsharpObject ( );

    virtual HRESULT __stdcall get_CplusplusObject (
      /*[out,retval]*/ struct ICplusplusObject * * cplusplusObject ) = 0;
    virtual HRESULT __stdcall get_CsharpObject (
      /*[out,retval]*/ struct ICsharpObject * * csharpObject ) = 0;
  }

  __declspec(implementation_key(1)) ICplusplusObjectPtr IMyLibrary::GetcplusplusObject ( );
  __declspec(implementation_key(2)) ICsharpObjectPtr IMyLibrary::GetcsharpObject ( );
}

The error means that ICsharpObjectPtr or ICsharpObject respectively is not known which I understand so far. ICplusplusObjectPtr is known because import "ICplusplus.idl" added the definitions into the .tlh and importlib("ICsharp.tlb"); did not obviously.

For Test reasons I generated the ICsharp.idl out of the .tlb by using OLE/COM Object Viewer and made an import of that idl. The error was gone after that.

But why does the importlib of the .tlb not work directly? I do not want to generate an idl file every time out of the .tlb.

I think that there is an #include "ICsharp.tlh" missing or something to make the type known for the .tlh. But how to tell the idl or the compiler to properly reference the ICsharpObject?

Thank you so much in advance for your help.

1
You are defining methods in the middle of a library block. That doesn't make any sense. Methods, naturally, must be part of an interface.Igor Tandetnik
You want to use auto_search attribute with your #import directive. Or else explicitly #import Csharp.tlb first.Igor Tandetnik
I made a correction of MyLibrary.idl code. I just forgot to write down the interface as you noticed already. Thank you.MaLe
The #import statement one can use in a cpp file, right? But I want to import a .tlb into an .idl file not a .cpp file. I did this with an importlib ("Csharp.tlb") statement, which leads to this error. If I generated an Csharp.idl out of that Csharp.tlb file I was able to use the import "Csharp.idl" statement and it worked. But I do not want to have this .tld->.idl conversion.MaLe
Where did the .tlh file come from? Normally, it's the product of the #import statement in your C++ code. Are you saying you don't have such a statement? With all due respect, I find it difficult to believe.Igor Tandetnik

1 Answers

1
votes

This is an error due to wrong order of tlb-imports. The #import directive tries to generate the primary (.tlh) and secondary (.tli) header files on compilation. If a tlb uses a type of another typelib which was not imported first this error will occur. In this case the following solved the error

Importer.cpp

#import "CplusplusLibrary.tlb"
#import "CsharpLibrary.tlb"
#import "MyLibrary.tlb"

Thank you so much Igor.