I have a header-file, the COM interface. I have created a small win32 program which works, but my main program is written in C#.
So I would like to import this COM object in my main program, but how do I do this, when the only thing I got is the header-file?
All places I’ve looked I need a tlb-file..?
I’m new to COM objects so just ask if you need some extra info, or have another workaround 🙂
[UPDATE]
First thanks for all the responses!
I’ve tried some different things, but haven’t solved my issue yet. In my research, I’ve found an article describing COM Interop http://msdn.microsoft.com/en-us/library/aa645736(v=vs.71).aspx#vcwlkcominteroppart1cclienttutorialanchor2
This haven’t helped me out. But I’ve found that I should be able to get moving if I can complete the following:
-
Declaring a COM coclass:
[ComImport, Guid("7C075F7F-FD71-40a2-AC63-0D0C4DB39ECA")] class CCamera { // Cannot have any members here // NOTE that the C# compiler will add a default constructor // for you (no parameters). } -
Creating a COM class wrapper:
[Guid("AD87369B-3BBA-4f1c-81C5-B92FCEA9A1F4"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] interface ICamera { //static HRESULT GetCameraInterface(); bool StartPreview(); bool StopPreview(); }- Using Casts Instead of QueryInterface:
try
{
CCamera cam = new CCamera();
ICamera test = (ICamera)cam;
//test.StartPreview();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
I get an invalid cast exception, so is this because I miss implementing some methods in the interface?
And how do I implement the following method from the c++ interface:
static HRESULT GetCameraInterface(void __RPC_FAR *__RPC_FAR *ppvObject);
[SOLUTION]
OK I got a solution, but I never solved to wrap the interface.
Instead I created a C++ dll project and exposed the methods I needed. Then in my C# project, could I use these methods with DllImport. If anybody need more explanation on how I archived this, send me a message.
If it is registered as a COM-Object, then you can import it via the Add Reference Dialog. Right click on the project –> Add Reference. Then select the Tab “COM” and you can select your COM Interface. Required Interop classes for .NET will be generated automatically.