Typecasting between ref class and interface class

E

ewpatton

Hi,

I have written a DLL in C++.NET that contains a class inherited from a
single abstraction:

//------ IThing.h ------
#ifndef __ITHING_H__
#define __ITHING_H__

namespace DLLObject
{
public interface class IThing {
System::String ^DoTest();
};
}

#endif

//------ Thing.h ------
#ifndef __THING_H__
#define __THING_H__

#include "IThing.h"

namespace DLLObject
{
public ref class Thing : public IThing {
virtual System::String ^DoTest();
};
}

#endif

//------ Thing.cpp ------

#include "Thing.h"

namespace DLLObject
{
using namespace System;
String ^Thing::DoTest() { return "Test"; }
}

Now I want to be able to access this class dynamically at runtime
(since it's assumed other people will make their own IThings.) since I
don't want to have to recompile every time I add a new custom object.
I found a sample online and attempted to mimic it in my own program:

Form1()
{
Reflection::Assembly ^myasm = Reflection::Assembly::LoadFrom("..\
\Debug\\DLLObject.dll");
Object ^obj;
IThing ^thing;
obj = myasm->CreateInstance("DLLObject.Thing", true);
thing = safe_cast<IThing ^>(obj);
MessageBox::Show(thing->GetName());
}

However, when I attempt to run this program an exception is thrown
stating that .NET can't cast an object of type DLLObject.Thing to
DLLObject.IThing. Can anyone give me any pointers on what I'm doing
wrong?

I can supply the sample solution if necessary.

Evan
 
M

Mattias Sjögren

Form1()
{
Reflection::Assembly ^myasm = Reflection::Assembly::LoadFrom("..\
\Debug\\DLLObject.dll");
Object ^obj;
IThing ^thing;
obj = myasm->CreateInstance("DLLObject.Thing", true);
thing = safe_cast<IThing ^>(obj);
MessageBox::Show(thing->GetName());
}

Where does your definition of IThing come from in this project? Do you
reference DLLObject.dll or do you #include Thing.h?

If you do the latter, you'll redefine the interface in the client
project and it will not be the same as the interface in the DLL (since
..NET type identity is scoped by the assembly the type is in).

You have to make sure all assemblies share a single common definition
of the interface.


Mattias
 
E

ewpatton

Where does your definition of IThing come from in this project? Do you
reference DLLObject.dll or do you #include Thing.h?

If you do the latter, you'll redefine the interface in the client
project and it will not be the same as the interface in the DLL (since
.NET type identity is scoped by the assembly the type is in).

You have to make sure all assemblies share a single common definition
of the interface.

Mattias

I've been including "IThing.h" since all files come from a shared
folder. How can I make both projects aware of a single interface?
Should I create an interface in its own DLL and use that as a
reference?

Evan
 

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