472,789 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 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 4028
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
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

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.