473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

unresolved metadata token when wrapping native DLL

I am trying to wrap a native-C++ DLL in managed C++, to
use in a .NET project.

The native code is compiled into a DLL, and I have created
a .def file that exports the mangled names of the symbols
I am going to use from the wrapper library.

The wrapper library has the code written that uses the
native classes, and wraps them to present an equivalent of
their interface for .NET.

When I compile the managed-C++ wrapper library, everything
compiles fine, but upon linking I get a series of LNK2020
errors about unresolved tokens.

The help for this error suggests it is unable to resolve a
reference to the function that is in the metadata.

My wrapper project is set up to link against the import
library for the native DLL. When I do not link against
this library, I get the usual LNK2001 unresolved external
symbol. When I link against the import library, these go
away, but the LNK2020 appears.

Is it looking for metadata information in my native DLL,
or does it just mean that there is a reference in the
managed library's metadata that couldn't be resolved? In
either case, how do I go about resolving this?

Thanks in advance.

Regards,
David.
Jul 19 '05 #1
4 4083
Hi David,

Please refer to
http://www.c-sharpcorner.com/Code/20...keServices.asp for how to
create a wrapper class to a typical win32 DLL.

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

Jul 19 '05 #2
Hi David,

I am not sure what you mean by "The DLL I am trying to wrap contains C++
objects.", do you want to wrap C++ class or you instantiate C++ classes in
the unmanaged DLL?

Would you please post some code snippet of both unmanaged DLL and its
corresponding wrapper? Please also tell me the detailed error messages of
LNK2020 and LNK2001.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
Jul 19 '05 #3
The DLL contains the implementation of several C++
classes. These classes are what I am trying to wrap in my
managed-C++ DLL.

Here is an example of a class I am trying to wrap.

Unmanaged header:
#ifndef DEVICE_STATE_H
#define DEVICE_STATE_H
#include <SmartPtr.h>
#include <callback.h>

namespace Native {

class DeviceState
{
public:
DeviceState();
DeviceState(const DeviceState &);
~DeviceState();
DeviceState &operator=(const DeviceState &);

void set_bit(unsigned int bitpos, bool bval);
bool get_bit(unsigned int bitpos);

operator unsigned long() const;
SmartPtr<callback<bool> > create_callback(unsigned
long bitpos);

private:
class DeviceStateImpl;
DeviceStateImpl *impl;
};

}

#endif // DEVICE_STATE_H
Unmanaged implementation code (is compiled into the native
DLL):

#include <DeviceState.h>
#include <climits>
#include <algorithm>
#pragma warning(disable: 4127)
#include <bitset>

.... snip: some file-static utility functions ...

using Native::DeviceState;
DeviceState::DeviceState()
: impl(new DeviceStateImpl)
{
impl->ref_count = 1;
impl->Sync = new CriticalSection;
}

DeviceState::DeviceState(const DeviceState &other)
: impl(other.impl)
{
++impl->ref_count;
}

DeviceState &DeviceState::operator=(const DeviceState &rhs)
{
DeviceState temp(rhs);
std::swap(impl, temp.impl);
return *this;
}

DeviceState::~DeviceState()
{
if (0 == --impl->ref_count)
{
delete impl;
impl = 0;
}
}

void DeviceState::set_bit(unsigned int bitpos, bool bval)
{
impl->BitMask.set(bitpos, bval);
}

.... snip: more member function implementations ...
Managed code: This is in the header file:

private __gc class DeviceStateImpl;
public __gc class DeviceState
{
public:
DeviceState();
~DeviceState();

void SetBit(unsigned int bitpos, bool val);
bool GetBit(unsigned int bitpos);

__property void set_Bits(unsigned int bitpos, bool
val);
__property bool get_Bits(unsigned int bitpos);

private:
DeviceStateImpl *impl;
};
And this is the managed-C++ implementation of that class:

#include "stdafx.h"
#include "SNMPAgentLib.h" // managed header
#include "DeviceState.h" // native-code header

private __gc class DeviceStateImpl
{
public:
Native::DeviceState __nogc *state;
};

DeviceState::DeviceState()
: impl(new DeviceStateImpl)
{
impl->state = new Native::DeviceState;
}

DeviceState::~DeviceState()
{
delete impl->state;
}

void DeviceState::SetBit(unsigned int bitpos, bool val)
{
impl->state->set_bit(bitpos, val);
}

bool DeviceState::GetBit(unsigned int bitpos)
{
return impl->state->get_bit(bitpos);
}

void DeviceState::set_Bits(unsigned int bitpos, bool val)
{
SetBit(bitpos, val);
}

bool DeviceState::get_Bits(unsigned int bitpos)
{
return GetBit(bitpos);
}
The code compiles fine, but when linking yields messages
like this:

LINK : error LNK2020: unresolved token (0A0000A2)
Native.DeviceState.get_bit
LINK : error LNK2020: unresolved token (0A0000A3)
Native.DeviceState.set_bit
LINK : error LNK2020: unresolved token (0A0000A4)
Native.DeviceState.__dtor
LINK : error LNK2020: unresolved token (0A0000A5)
Native.DeviceState.__ctor

However, I can link against the same native-DLL's import
library from a non-managed C++ project and I get no link
errors, everything works fine.

I am wondering why it needs the metadata information for
the native code portion, and if there's anything I can do
to either fix my code or my project settings to satisfy
this.

Thanks.

Regards,
David.
-----Original Message-----
Hi David,

I am not sure what you mean by "The DLL I am trying to wrap contains C++ objects.", do you want to wrap C++ class or you instantiate C++ classes in the unmanaged DLL?

Would you please post some code snippet of both unmanaged DLL and its corresponding wrapper? Please also tell me the detailed error messages of LNK2020 and LNK2001.

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

.

Jul 19 '05 #4
Hello David,

To resolve the problem, you have to convert the managed DLL to mixed mode.
Please refer to the following MSDN document:

Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode
http://msdn.microsoft.com/library/de...us/vcmex/html/
vcconconvertingmanagedextensionsforcprojectsfrompu reintermediatelanguagetomi
xedmode.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
Jul 19 '05 #5

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

Similar topics

9
by: David Kantowitz | last post by:
I am trying to wrap a native-C++ DLL in managed C++, to use in a .NET project. The native code is compiled into a DLL, and I have created a .def file that exports the mangled names of the...
1
by: 4Space | last post by:
Hi, I've just been refactoring a managed C++ class library. The managed classes use a couple of our native dlls, and the linkage on that front appears to be OK. I do however get this error at...
3
by: Kevin Burton | last post by:
I am trying to use managed C++ but I am getting the following link errors: Metadata file 'D:\Projects\Visa\AddressVerification\AddressVerificat...
1
by: Tony Baker | last post by:
Hi, If I 1) create a brand new Visual C++ Project -> Class Library (.Net) 2) in the stdafx.h file add #include <atlbase.h> I get the following errors: ------ Build started: Project: testATL,...
1
by: Joel Rondeau | last post by:
I am building a mixed-mode DLL with VS.NET 2003 and have run across some link errors. (I have looked at the MS Help articles about LNK2020 errors with mixed-mode C++ and that has not helped)...
10
by: Ian Lazarus | last post by:
Hello. How do "unresolved token" link errors occur. How do I fix them? Linking... LINK : error LNK2020: unresolved token (0A000015) ??_7type_info@@6B@ LINK : error LNK2020: unresolved token...
2
by: DEFENDER | last post by:
Hi .I have problem. I have project width 2 libs When i link my project i have errors some like this : msvcrt.lib(MSVCR71.dll) : error LNK2005: _memmove already defined i LIBCMT.lib(memmove.obj)...
0
by: Roland | last post by:
Hi, ultimately I want to call some unmanaged C++ class that contains some DirectShow code of mine from my C# classes, but for the time being I'd be happy if I could get this to build! I have...
2
by: jazihak | last post by:
Can any one tell me how to resolve this issue; I have the following function //hello.cpp #include "stdafx.h" #include <stdlib.h> #include <vcclr.h> #include <string> namespace Wrapper
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.