0

Hi, I am trying to use some C# code I have written in C++ - its worked fine in the past (without inheritance) so I'm totally stumped by this problem I'm getting.

I have made a really simple version, which demonstrates the problem, specifically when it goes into any COM method it gets an "Unhandled exception at 0xabababab in ConsoleTest.exe: 0xC0000005: Access violation."

Here is the c++ code:

#include <iostream>
#include <string>
#include <atlbase.h>
using namespace std;

#import <mscorlib.tlb> raw_interfaces_only
#import "C:\Users\tofuser\Documents\Visual Studio 2008\Projects\COMLibraryTest\bin\Debug\COMLibraryTest.tlb" no_namespace named_guids

void main()
{
	CoInitialize ( NULL );
	
	cout << "[a] or [b]?" << endl;

	CComPtr<ISuper> thing;

	char in;
	cin >> in;

	if (in == 'a')
	{
		HRESULT hr = CoCreateInstance(CLSID_SubA,
				NULL, CLSCTX_INPROC_SERVER,
				IID_ISubA, (void**)(&thing));

		if (FAILED(hr))
		{
			printf("Couldn't create the instance of SubA\n", hr);
		}
	}
	else if (in == 'b')
	{
		HRESULT hr = CoCreateInstance(CLSID_SubB,
				NULL, CLSCTX_INPROC_SERVER,
				IID_ISubB, (void**)(&thing));

		if (FAILED(hr))
		{
			printf("Couldn't create the instance of SubB\n", hr);
		}
	}
	
	thing->Something = 5;  //error here
	cout << "something: " << thing->Something  << endl;

	CoUninitialize();
}

the error is thrown when it goes into the Something method, specifically in the generated code here:

inline void ISuper::PutSomething ( long pRetVal ) {
    HRESULT _hr = put_Something(pRetVal); //here
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
}

I generate the tlb file from a C# project containing:

namespace COMLibraryTest
{
	[Guid("3b35f97c-e9f0-472a-95e7-cdf067c8f9d9")]
	public interface ISuper
	{
		int Something { get; set; }
	}

	[Guid("5b8d3df3-973f-4e4e-b6b7-4210a405af29")]
	public interface ISubA : ISuper { }

	[Guid("9007eb45-cbeb-4975-93d6-5c640d901502")]
	public class SubA : ISubA
	{
		int something;
		public int Something
		{
			get { return something; }
			set { something = value; }
		}
	}	

	[Guid("7779f353-1db8-4528-8120-5c83ac0453e5")]
	public interface ISubB : ISuper { }

	[Guid("254cc431-b803-41ef-a61c-4297d8819a3f")]
	public class SubB : ISubB
	{
		int something;
		public int Something
		{
			get { return something; }
			set { something = value; }
		}
	}
}

and generate the tlb using regasm (with no errors).

I've also ran the code without COM using C#, and it runs fine, with no exceptions:

namespace ConsoleApplication2
{
	class Program
	{
		static void Main(string[] args)
		{
			COMLibraryTest.ISuper thing = null;

			Console.WriteLine("[a] or [b]?");
			String input = Console.In.ReadLine();
			if (input.Equals("a"))
			{
				thing = new COMLibraryTest.SubA();
			}
			else if (input.Equals("b"))
			{
				thing = new COMLibraryTest.SubB();
			}

			thing.Something = 1;

			Console.WriteLine(thing.Something);
		}
	}
}

I'm unsure if the problem is in C# or C++, but I figured it might be C++ because I seem to be able to run it in C# ok (but I may be doing the COM stuff wrong on that side)

Thanks for any help.

Joe

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by hanvyj
0

Hmm, well I tried this:

CoInitialize ( NULL );
	
	ISubB *thing1;

	HRESULT hr = CoCreateInstance(CLSID_SubB,
				NULL, CLSCTX_INPROC_SERVER,
				IID_ISubB, (void**)(&thing1));

	if (FAILED(hr))
	{
		printf("Couldn't create the instance of SubB\n", hr);
	}
	
	thing1->Something = 5;

	cout << "something: " << thing1->Something  << endl;

which wont compile: "error C2039: 'Something' : is not a member of 'ISubB'" Which doesn't make much sense, ISubB extends ISuper! In fact, if I even try to put this in, it comes up with a warning!:

[Guid("7779f353-1db8-4528-8120-5c83ac0453e5")]
	public interface ISubB : ISuper
	{
		int Something { get; set; }
	}

warning CS0108: 'COMLibraryTest.ISubB.Something' hides inherited member 'COMLibraryTest.ISuper.Something'. Use the new keyword if hiding was intended.

and then works, do I really have to have a copy of every inherited method :/ it kind of seems to defeat the point?

Edited by hanvyj: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.