dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Wrapping C++ COM in C#: How to handle events

  1. #1
    Join Date
    Feb 2007
    Posts
    5

    Wrapping C++ COM in C#: How to handle events

    I have a COM object written in what I believe is C++. I do not have the source code, only the .dll and an SDK manual that gives prototypes for all of the functions. The .dll provides an interface with an external piece of hardware. As I understand it, the .dll is proprietary, so there is no chance of getting the original source to modify.

    I'm wrapping the .dll in C# to interface with a program called LabVIEW. LabVIEW doesn't have native pointer support, and from what I understand it doesn't handle certain kinds of structs either. In the original .dll, arguably the most important component is an event that fires with a pointer to a struct of data.

    I've successfully wrapped most of the methods; simple things like connecting to the device, starting it, stopping it, etc. What I don't understand how to wrap are events. Short of rewriting the entire event, which seems impossible due to the proprietary nature of their code/device, is there any way of essentially mimicking the event that is already in place so I can restructure the data accordingly? This is my first C# project, in fact my first project involving COM and events as well.

    Thank you very much for your time and assistance!

  2. #2

    Re: Wrapping C++ COM in C#: How to handle events

    I would look into using C++/CLI instead of C# to create the wrapper class, the jargon can be a little confusing but for problems like this it is a god send because you can basically move back and forth between regular C++ and "Managed Code" with ease. For example your event problems can be solved by creating a Managed C++ class that essentially wraps your dll and captures the events reformats the data however you need it and then fires a managed event with managed data that everyone else can live with.

  3. #3
    Join Date
    Feb 2007
    Posts
    5

    Re: Wrapping C++ COM in C#: How to handle events

    Quote Originally Posted by matthewsanford@gmail.com View Post
    For example your event problems can be solved by creating a Managed C++ class that essentially wraps your dll and captures the events reformats the data however you need it and then fires a managed event with managed data that everyone else can live with.
    Does that mean that C# can't access or otherwise wrap the events of another dll? I'd really rather finish writing this in C# if possible.

    I still cannot find a way to hook in to the dll's events with C#. I also can't figure out how to naturally use the dll's properties; so far, I've just been simulating them.

    To be more specific, I'm using [DllImport...] to wrap the functions, but I haven't been able to find the equivalent for events or properties.
    Last edited by icbat; July 8th, 2010 at 11:58 AM.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Wrapping C++ COM in C#: How to handle events

    I have a COM object written in what I believe is C++.
    If this is an IDispatch based COM object (using BSTRs, SAFEARRAYs etc) then you can just add the dll as a reference to the C# project and an interop dll will automatically be created for you.

    Then you can call the COM objects in the dll directly from C#.

    Google "C# COM interop" if you need more information.

    Yours,

    Darwen

    P.S. On further reading you say you're using DllImport - but you don't use DllImport with COM objects !! I'm rather confused now as to whether your dll is a COM dll or not. Do you know what a COM dll is ?
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)