473,508 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unamanged types in managed interface generates TypeLoadException

Hi!

This is a quite involved question concerning two dll's that are consumed from a console application.

The first dll is called base.dll and is compiled in mixed mode (managed and unmanaged C++). One native class and one managed interface is declared and defined in base dll. The native class is exported via declspec(dllexport) and the managed interface via the public keyword. The managed interface has one property which returns a pointer to the unmanaged class. It looks like this:

// base.dll
// DLL_ITEM is defined as dllexport when base.dll is compiled
// and dllimport when some other dll imports it.

__nogc class DLL_ITEM Native
{
};

public __gc __interface NativeWrapper
{
__property Native* get_Native();
};

Now, base.dll is compiled and another dll, consumer1.dll, imports it.

In consumer1.dll two new classes are declared, one that implements the NativeWrapper interface and one that inherits from the Native class. To inherit from the native class, one must include its header file where it is declared. This is what is found in the consumer1.dll:

__nogc class Native2 : public Native
{
};

__gc class NativeWrapper2 : public NativeWrapper
{
__property Native* get_Native()
{
return new Native2();
}
}

Finally these two dll's are consumed in console.exe. Both base.dll and consumer1.dll are imported and console.exe compiles alright. But when consumer1.dll is loaded I get the following runtime error:

--

An unhandled exception of type 'System.TypeLoadException'
occurred in Unknown Module.

Additional information: Method get_Native in type
consumer1.Class1 from assembly consumer1, Version=1.0.1586.21893, Culture=neutral, PublicKeyToken=null does not have an implementation.

--

The reson for this seems to be the Native* return type which seems to be somewhat different between base.dll and consumer1.dll. If one changes Native* to void* everything works fine, no exception is thrown. So the question is, how can I use the Native class in both base.dll and consumer1.dll without getting the exception shown above?

I'm using VS .NET 2003.

/Mats

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 17 '05 #1
2 3226
Ok, here is an answer to my own question.

First of all, the error message is wrong and should read: "... Method get_Native in type NativeWrapper2 from consumer1 ... "

Having corrected that now over to the reason for this error. The compiler will generate metadata for the class Native in base.dll to describe the type. The same goes for the assembly consumer1. The metadata will be the same but a type is not solely based on its name but also on its physical location. That means that there will be two different Native classes, Native[1] in base.dll and Native[2] in consumer1.dll. This is no problem for the VC compiler which actually thinks that you are implementing the interface NativeWrapper when you write your NativeWrapper2 class. But when the loader tries to find the overloads for the functions in NativeWrapper it doesn't find any get_Native that returns a type Native[1] but rather a type Native[2]. That's why I get the exception telling me I didn't implement get_Native.

The solution then? Well, i did like this:

public __gc __interface NativeWrapper
{
__property void* get_Native();
};

void* is the same in all assemblies so this will be easy to implement. The drawback is that the typesystem is disabled. I put together a bunch of helperfunctions to access the Native object so the risk of doing something unwanted is minimised. If someone finds out a typesafe way to do it, please let me know!
************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 17 '05 #2
I received the same error in very similar circumstances.
The failure happened when calling a delegate whose interface is
abstract and whose implementation overrides the interface method.

First thing to verify is that the override keyword is in the
implementation.
Second thing to verify is that the implementation has the same
signature
as the interface (delegate). This can be verified by running:

C:\Program Files\Microsoft Visual Studio .NET
2003\SDK\v1.1\Bin\ildasm.exe (a GUI app)

and opening the DLL containing the implementation and then the one
containing the interface.

The next thing to do is to see if the implementing assembly fails to
load:
http://msdn.microsoft.com/library/de...uslogvwexe.asp
give instructions concerning the Fusion Log Viewer.
Bear in mind that this article should have said to add the DWORD
registry variables it refers to as they are not present by default.

In my case, the assembly loaded with no errors. This turned out to be
misleading, for the type load failure was caused by the inability to
load
an assembly on which the current delegate depended.

In particular, the delegate was implemented in one C++ DLL but
returned an object pointer defined in another C++ DLL. It was the
latter DLL that failed to load and yet fuslogvw showed no errors.

My error was resolved by copying the DLL, on which the delegate
implementation depended, to the current directory. I didn't try
having the DLL in LIBPATH, but that may have been sufficient. If you
see success on one system and failure on another, LIBPATH may be an
issue. Since LIBPATH differs for users and system, a load may
succeed for a user but fail for a service. Either way, having the
DLLs in the current directory works.
*---------------------------------*
Posted at: http://www.GroupSrv.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1273
by: cmelnick | last post by:
I created an interface, IMyInterface and placed it in namespace MyPackage. Something like this: namespace MyPackage { interface IMyInterface { // Stuff } } This interface as well as some...
9
4617
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
13
4236
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public...
0
987
by: Yahoo | last post by:
I found this article on askjeeves.com and was looking for others who successfully got this to work. I am trying to debug though system.web but when i use a compiled system.web with debugging info...
0
773
by: Z-Mechanic | last post by:
Hello everybody! I have a question. Anybody knows why managed function calls faster in MC++ rather than unmanaged function (defined with #pragma umanaged)? The differences is about 4 times...
9
2046
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
2
3621
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
2
1283
by: gunters | last post by:
Can please someone enlight me, I don't understand this: It is possible in C# to "reach" inside a boxed value type through an interface like in the following code: interface IPerson { string...
0
1356
by: krishnasamy | last post by:
Hello, I am writing a DLL for Capturing the Image from Camera Device using Camera SDK DLL. All the calls made to functions in Camera SDK DLL are working properly as I am able to get a return...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.