Wrapping native-C++ DLL in Managed C++: LNK2020 problem

  • Thread starter Thread starter Jacob Cohen
  • Start date Start date
J

Jacob Cohen

Under VC7.1, I am trying to wrap a native-C++ DLL that contains C++
objects in a Managed-C++ class library for use in a C# project.

I created and compiled the native DLL under VC7.1 as a Win32 DLL C++
(unmanaged) project. I found the decorated names of the symbols I
wanted to export and created a .def file, so that it generated an
import library.

I took this import library and had my Managed-C++ class library link
against it. The managed code does the style of wrapping of classes
found in the MSDN section on migrating to managed C++. That is, each
class replicates the interface of the wrapped class and forwards the
calls through to the native-class pointer. This is where I do all my
marshaling and converting of types from their .NET representations to
the native representations.

The code all compiles fine, but when I go to link, I get a series of
LNK2020 errors, which are unresolved tokens in the metadata. I am not
sure how this differs from the usual LNK2001 unresolved external
symbol, but when I remove the DLL import library from the managed-c++
project, I then get LNK2001 errors instead. It seems like it is
linking against the native-C++ DLL's import library, but not finding
everything it needs.

What else do I need to provide to my Managed-C++ library to satisfy
these unresolved tokens in the metadata? The native-C++ DLL does not
have any assembly information or metadata, and I am not really sure
what exactly it is looking for. The errors are things like:

LINK : error LNK2020: unresolved token (0A0000AA)
CTS.MIBEventTable.bind
LINK : error LNK2020: unresolved token (0A0000AB)
CTS.MIBEventTable.__ctor

These methods are defined in the DLL (and exported in the .def). When
I remove the import library, these errors convert to being LNK2001.

Anyone have any ideas what I might be missing?

Thanks in advance.

Regards,
Jake.
 
Jacob,

No help here.

I've been getting the same problem at my end. Here is a
snipet of code what will get a link error:

B.h:

#pragma once

using namespace System;

#pragma unmanaged
__nogc class B
{
public:
B() {};
~B() {};
};
#pragma managed
public __gc class A
{
private:
B * pB;
public:
A() { pB = new B;}
~A() { pB->~B();}
};


B.cpp

#include "stdafx.h"

#include "B.h"


This compiles fine, but throws an link error:
B error LNK2020: unresolved token (0A000008) delete

Interestingly, I took a piece of downloaded sample of
some wrapper code from Wrox.com and was able to build
that using the same skeleton. There maybe something set
differently between this downloaded solution and one I
start from scratch.

Steve
 
Here is what we were doing wrong....

All the examples ASSUME that you are making a standalone
program, but we are trying to make a stand alone DLL in
which we are mixing IDL and c code. This is a mixed mode
DLL, and you have to make some changes to the way the
code is linked.

Try looking here:
ms-
help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vcmex/html/vcc
onConvertingManagedExtensionsForCProjectsFromPureIntermedi
ateLanguageToMixedMode.htm

This seemed to solve my problem!
 
Back
Top