views:

328

answers:

2

I have a .exe file I've been given which has COM classes inside it -- these are from C++, not .net ComVisible types

I'd like to use those classes inside my .net project, but I can't add a reference to the exe; when I try I get

DIALOG BOX: 

---------------------------
Microsoft Visual Studio
---------------------------
A reference to 'C:\Program Files\blah\blah.exe' 
could not be added. Please make sure that the 
file is accessible, and that it is a valid 
assembly or COM component.
---------------------------
OK   
---------------------------

tlbimp also fails;

TLBIMP OUTPUT: 

> tlbimp blah.exe

Microsoft (R) .NET Framework Type Library 
to Assembly Converter 3.5.30729.1

Copyright (C) Microsoft Corporation.  All rights reserved.

TlbIml : error TI0000 : The input file 
'c:\program files\blah\blah.exe' is not
 a valid type library

But it really looks as if there are COM classes available; in my HKEY_CLASSES_ROOT hive I can see entries for the different COM clasess;

REGISTRY ENTRY:

\HKEY_CLASSES_ROOT
    \CLSID
        \{456B14EA-4CCC-11D4-BB91-0020AFC894E9}
            @="COM.Classname"
            \InprocHandler32
                @="ole32.dll"
            \LocalServer32
                "LocalServer32"=hex(7) ...
                @="C:\\PROGRA~2\\blah\blah.exe"
            \ProgID
                @="COM.Classname"

Anyone got a clue about how, and even if, I can use these COM classes from within a .net project?

A: 

Read these articles - ActiveX EXE Wrappers article and Calling COM Components from .NET Clients

adatapost
unfortunately these suggest the methods I've already tried and documented in the question.
Steve Cooper
+2  A: 

There are three methods of distributing a type library: either separately in a .tlb file, or as an embedded resource inside a .dll or .exe file. However, it is not mandatory to distribute a type library file with a COM component.

Usually the there would be a TypeLib registry key underneath the HKCR\CLSID{CLSID}, which would have a default value containing the typelib id. (oleview.exe is very useful for tracking down these registry entries). If your registry does not contain a TypeLib key, then is is likely that your component has not been distributed with a type library.

tlbimp.exe can import any embedded type library resources inside .dll or .exe files, so the error it is reporting above indicates to me that there is no embeded type library in your given executable. You can confirm this by viewing the exe using a resource view. I have forgotten the name of the Windows SDK resource viewer, but you can find a free one here: MiTec Exe Explorer

If the exe does not have a resource of TYPELIB, then you cannot create a .net interop library. Your only option is then to contact the maker of the component and ask for a type lib. If this is not possible, you can use late binding to call the component.

Mark Glasgow