www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - cast fails for classes from windows dll

reply Andre <andre s-e-a-p.de> writes:
Hi,

I am not sure, whether this is a current limitation of the 
windows dll functionality of D
or I am doing s.th. which will not work.

I have developed in D a windows DLL which creates class instances 
by passing the name (using object.factory method).

In another D application I am using this DLL. My issue is, that 
the cast fails, although
typeid(bar).name shows the correct name .


module main;

// these classes are in a seperate module
// used for the dll & for this application
export class Foo {}
export class Bar : Foo {}
class Baz : Bar {}

void main()
{
	// this method calls the dll and returns Foo
	Foo c = dllCreateClass("main.Baz");
	
	// no failure
	assert( typeid(c).name == "main.Baz");
	
	// ----> fails
	if (auto myBar = cast(Bar) c){}
}

Kind regards
André
Jan 12 2016
parent Benjamin Thaut <code benjamin-thaut.de> writes:
On Tuesday, 12 January 2016 at 19:00:26 UTC, Andre wrote:
 Hi,

 I am not sure, whether this is a current limitation of the 
 windows dll functionality of D
 or I am doing s.th. which will not work.

 I have developed in D a windows DLL which creates class 
 instances by passing the name (using object.factory method).

 In another D application I am using this DLL. My issue is, that 
 the cast fails, although
 typeid(bar).name shows the correct name .


 module main;

 // these classes are in a seperate module
 // used for the dll & for this application
 export class Foo {}
 export class Bar : Foo {}
 class Baz : Bar {}

 void main()
 {
 	// this method calls the dll and returns Foo
 	Foo c = dllCreateClass("main.Baz");
 	
 	// no failure
 	assert( typeid(c).name == "main.Baz");
 	
 	// ----> fails
 	if (auto myBar = cast(Bar) c){}
 }

 Kind regards
 André
Thats a limitation of the current dll functionality. The type info of the class gets duplciated into both your executable and the dll and thus the cast fails. Until D properly supports Dlls on windows this is going to stay this way. Currently only a C like interface across dll boundaries is possible.
Jan 13 2016