1

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:

  1. 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).
    }
    
  2. Creating a COM class wrapper:

    [Guid("AD87369B-3BBA-4f1c-81C5-B92FCEA9A1F4"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    interface ICamera
    {
        //static HRESULT GetCameraInterface();
        bool StartPreview();
        bool StopPreview();
    }
    
    1. 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.

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes it's was I can read, but I'm developing to windows CE, and I think it's only registered there?? Please bear over I'm out on deep water here.
Do you have the Dll of the com object? If so, maybe you can run "regsvr32.exe YourComLibrary.dll" to register it.
As far as i know, you can't create a TLB from a header file that easily. Have you asked the provider of the COM Object for a TLB file? Maybe they can help.
1

you need to register the COM dll with the following line

regasm COMDll.DLL /tlb

then you can add it as a reference to your project.

Comments

1

I have completed some research and found that you can include a header file in an idl file in much the same way you do with C, below are the two links that show you how to include the header file in the idl and compile the idl to a tbl file which you can use in .Net:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa367049(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/aa367064(v=vs.85).aspx

I haven’t tried it and it looks like you may encounter some difficulty if the header file contains more than just the COM definitions, so good luck and please let me know how it went.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.