Trying to use unmanaged with C#

S

Steven Edison

Hi,

My company has a large library of unmanaged C++ COM objects we've been
using for years, it's massive, re-writing would take months to years.

One of these objects has a method that looks something like this:

HRESULT EnumItems(IUnkown* pUnk, VARIANT Param);

The IUnknown is a callback, where you would have done something like this:

class CCallback : public ICallbackInterface
{
//Implement dummy Release and AddRefs, supply a good
QueryInterface()

HRESULT MyCallback(LONG ValueNeeded1, BSTR ValueNeeded2, VARIANT
Param)
{
//do something with supplied information
return S_OK;
}
//...
};

CCallback* pCb = new CCallback;
pObj->EnumItems((IUnknown*)pCb, this);

internally it Queries for ICallbackInterface and feeds data to the callback.

Now I need to use this in my C# ASP.NET site. So I made something like
this:

//ref COM obj and convert to managed dll.
using System.Runtime.InteropServices;
using MyObjLib;

[ComVisible(true)] //added with no difference after getting error
public class CallbackClass : CallbackInterface
{
public void MyCallback(int ValueNeeded1, string ValueNeeded2, object
Param)
{
//deal with it
}
};

CallbackClass cc = new CallbackClass;
pObj.EnumItems(cc, this);

and I get this:

**Exception Details: System.InvalidOperationException: This type has a
ComVisible(false) parent in its hierarchy, therefore QueryInterface calls
for IDispatch or class interfaces are disallowed.

pointing to my Enum function. What am I doing wrong? and how can I use
this interface in C# correctly?

Thanks for any help!

Steven
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top