0

I want to call an COM object which have been created in c++.

I got an OLD file which contains an interface:

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

[oleautomation,uuid(D4F82FE1-E58E-11d1-9D47-006008098294)]
interface INotifier : IUnknown
{
//...
}

The component is created like this in the C++ app:

CreateInstance( __uuidof(CObjectServer),    
    __uuidof(INotifier), 
    (void**)&m_pINotifier,
    "CObjectServer INotifier");

So I tried to generate a TLB by using the MIDL compiler:

MIDL /tlb notifier.tlb /h notifierTest.h notifier.odl

It runs and no error messages are shown, but it doesn't generate a TLB file.

  1. How can I find out why it fails?
  2. Is there any other way to be able to create and invoke that component from C#?

Edit

There was a odl file ObjectServer.odl which contained the interface ODL inside a library clause. So I run MIDL on that ODL and got a TLB file.

however, when I try to reference that TLB in my C# project I get:

enter image description here

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Shouldn't there be a `library` block in the .idl? – sharptooth Jun 03 '15 at 10:58
  • possible duplicate of [How do I invoke the MIDL compiler to generate a .TLB file (type library) from an .IDL file?](http://stackoverflow.com/questions/30269119/how-do-i-invoke-the-midl-compiler-to-generate-a-tlb-file-type-library-from-an) – Roman R. Jun 03 '15 at 11:01
  • You can add `library` just for the purpose of building type library, and leave original ODL file intact (e.g. revert changes). You will still be able to reference the binary from C# code using standalone TLB file you obtained during this intermediate step. – Roman R. Jun 03 '15 at 11:52
  • Ok. I've added the library instruction and a TLB is generated. But when I try to load it in my C# project I get `A reference to ObjectServer.tlb could not be added. ... and it's a valid assembly or COM component`. – jgauffin Jun 03 '15 at 11:57
  • Perhaps your TLB is still empty (for whatever reason) or otherwise has issues. You can open it with COM/OLE Viewer (\bin\x86\oleview.exe in SDK Windows directory) and review the contents to make sure you have there all you expect to be there. Referencing in C# is straightforward - Add Reference, Browse - you will see *.tlb included in file type combo box. – Roman R. Jun 03 '15 at 12:36
  • 1
    The IDE holds a type library to a higher standard and refuses to import one that has issues. You'll need to run Tlbimp.exe from the Visual Studio Command Prompt, it will spit out some warning or error messages that tells you what is wrong. – Hans Passant Jun 03 '15 at 12:44
  • @HansPassant: Gr8. that works. Add an answer and I'll accept it. – jgauffin Jun 03 '15 at 12:50

0 Answers0