Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


COM Interop

Posted on 2005-03-02
Medium Priority
Last Modified: 2013-11-25
I'm trying to create a .net object that will provide control over a tv tuner. The problem is that when ever I try to declare a COM interface as a data member of a managed object the COM interface is also turned into a managed pointer even if the _nogc tag is used.

Is it possible to have a non managed COM interface as a memeber of a managed class and if so what might I be doing wrong?

If this can't be done are there any work arounds?


This is what I've got so far.

public __gc class TvTuner
      // Constructor(s)
      // Destructor
      // Properties
      //TODO__property void set_Channel(int channel);
      //TODO__property int get_Channel();
      __property void set_PreviewWindow(IntPtr hwnd) {m_Hwnd = hwnd;}
      //TODO__property IntPtr get_PreviewWindow();
      // Methods
      static CapDeviceProp* GetCaptureDeviceProps()[];
      void SetCaptureDevice(CapDeviceProp *device);
      void Start();
      HRESULT InitVideoWindow();
      HRESULT InitCaptureGraph(IMoniker *pMoniker);
      // Variables
      ICaptureGraphBuilder2      __nogc*m_Build;
      IntPtr                  m_Hwnd;
                IAMTVTuner            __nogc*m_Tuner;
Question by:cdiamond66
  • 2

Author Comment

ID: 13533124
I guess I either asked a really difficult question or a really stupid one, but this is driving me nuts so I raised the points to try and get some one to respond.

Any way I though about trying to store the COM pointers in IntPtr structures but this doesnt seem to work either. Below is basically how I tried to do this.

// store COM pointer
IntPtr tmp = pGraph;

// retrieve COM pointer
IGraphBuilder *pGraph = reinterpret_cast<IGraphBuilder*>(tmp.ToPointer());
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 13596847
I'm not sure I understand your question fully.  Are you just trying to build a .NET Assembly that will work as a COM object?

All you need to do is declare the classes, functions, properties, etc. as public and build the thing and it *should* work.  My understanding is .NET as COM is really only fully supported in Internet Explorer.  I've been working on an ActiveX Control replacement and it's pretty nice to just build and use.

.NET creats the CCW for you when you build it this way.

How are  you instantiating the object?  You may have to register it with regasm <DLL PATH and NAME> /reg

That may be your problem.  Note that if it's a COM object you're trying to instantiate and you need a CLSID, then that's the way to at least test it.  Like I said, I'n not sure I fully understand all you're doing.

I don't think you want do to the IntPtr stuff.  If you want, you can do this: (is this C#)?


then for create an interface:

public Interface ITVTuner
     void setChannel();
     void getChannel();

public class TVTuner: ITVTuner
   public void setChannel(etc.) {etc}
   public void getChannel(etc.) {etc}


Author Comment

ID: 13821566
I came up with a solution myself. The solution was pretty simple. The problem was that becuase the pointer was declared inside a garbage collected class which made the pointer garbage collected. This made the following code fail.

comInterface->QueryInterface(IID_IBaseFilter, (void**)&gcObject);

The solution was to use a temporary non garbage collected pointer.

IBaseFilter *tmp = NULL;
comInterface->QueryInterface(IID_IBaseFilter, (void**)&tmp);
gcObject = tmp;

Accepted Solution

PAQ_Man earned 0 total points
ID: 13955104
PAQed with points refunded (300)

Community Support Moderator

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question