Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a COM component in C++ (6.0 - I know its old but I'm learning using it) using the IDispatch interface and am trying to consume it in C#.

My C# code is as follows:

static void Main(string[] args)
    {
        Type type = Type.GetTypeFromProgID("Bens.General.1");
            
        Object obj = Activator.CreateInstance(type);

        ITest tObj = (ITest)obj;
            
        Console.WriteLine("Value1: " + tObj.MyValue());

        Console.ReadLine();
    }


(I have also tried this using dynamic but no luck there either).

with the following ITest interface:

[Guid("4B8C6960-EB25-11e7-A3BD-001CF0FAD303")] // This matches my IID in Component QueryInterface

    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ITest
    {
        [DispId(1)]
        string MyValue();

        [DispId(2)]
        string Value2();
    }


The C++ QueryInterface is:

HRESULT __stdcall Cmpnt::QueryInterface(const IID& iid, void** ppv)
{
	char buffer[37];

	char* guid = guid_to_str(&iid, buffer);

	MessageBox(NULL, guid, guid, 0);

	if(iid == IID_IUnknown)
	{
		*ppv = static_cast<general*>(this);
	}
	else if(iid == IID_IDispatch)
	{
		*ppv = static_cast<general*>(this);
	}
	else if(iid == IID_general)
	{
		*ppv = static_cast<general*>(this);
	}
	else
	{
		*ppv = NULL;
		return E_NOINTERFACE;
	}
	
	reinterpret_cast<IUnknown*>(*ppv)->AddRef();

	return S_OK;
}


IID_general definitely matches the GUID in the ITest attribute.

What I have tried:

It works fine in VB6 and VBA (Office 2016) but in C# says that the Interface
{
4B8C6960-EB25-11e7-A3BD-001CF0FAD303
}
Isn't implemented but I know for a fact that in the C++ QueryInterface method it is (it works in VB/VBA so why not C#?)

The MessageBox in the C++ code shows the C# is trying at least 10 IID's including the IID_general which seems strange to me with no luck. VBA, Excel shows that it tries 6 IID's and succeeds after that.

UPDATE: I have updates my COM component to support early binding with type library, MIDL generated interfaces, etc... - again this works for VBA (Excel) but not in C#.
Posted
Updated 25-Mar-18 23:31pm
v4
Comments
F-ES Sitecore 26-Mar-18 4:14am    
It works in VB and VBA as they are COM based langues so have been written from the ground-up to support IDispatch (ie they support both early and late binding). With c# the entire framework paradigm has changed to be strongly-typed so there's no native support for things like IDispatch. I believe you can still access the methods using reflection\Invoke. I googled "call IDispatch c#" and found a few contenders, below is one but there are others you might want to look at too

http://www.productiverage.com/idispatch-iwastedtimeonthis-but-ilearntlots

1 solution

Using the old VC 6.0 isnt a good idea because it is nearly 20 years old and so misses the modern development. You better use an actual version of Visual Studio, because the old (VC 6) and new .net interfaces (C#) arent compatible.

Take a look at this A Beginner Tutorial for Writing Simple COM/ATL DLL to see the better support. And most of all it works on the net-platform and so is fully debugable in the different languages!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900