473,387 Members | 1,582 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,387 software developers and data experts.

LNK2020 using managed/unmanaged classes combination and workaround question

Greetings!

I found myself with a LNK2020 linking problem using VS.NET 2003 under a
situation that I was able to replicate in a small project.

I found one workaround for the problem too, but it's ugly and sure things
shouldn't be like that. I'd like to know if that's a conceptual mistake by
me, a compiler error, both, or none of them! ;-)

To replicate the problem:

- Create a new solution with 2 projects:
- The first project will be a DLL (New Project, Win32 Application,
Application Settings, mark 'DLL' and 'Export Symbols'). Name it anything,
say, TestDLL
- For the other project, create a "Windows Forms application"

In the DLL:
- Add to project, besides the default .cpp generated by VS, another one, say
ManagedClassX.cpp
- Define your class like this:

//ManagedClassX.h:
//--------------------------------
public __gc class ManagedClassX
{
public:
void SomeMethod();
};
//--------------------------End of h

//ManagedClassX.cpp:
//--------------------------------
#using <mscorlib.dll>

#include "ManagedClassX.h"

void ManagedClassX::SomeMethod()
{
};
//----------------------------End of cpp

As we can see, this is a managed class added to the DLL.

To the Windows Forms project,

- Add a button to the main form. Add to this button the code:

ManagedClassX* test = new ManagedClassX();
test->SomeMethod();

and make sure that the Form1.h contains

#include "ManagedClassX.h"

All set up, make sure you compile everything with /clr. Yes, I understand
this will create managed code for everything.

At my test machine here, the DLL compiles just fine. But the WinForms
application
that is going to use it no - it gives me an LNK2020 error:

------ Build started: Project: WinForms, Configuration: Release Win32 ------

Compiling...
Form1.cpp
Linking...
LINK : error LNK2020: unresolved token (06000002) ManagedClassX::SomeMethod
LINK : fatal error LNK1120: 1 unresolved externals

How can that be? The class was compiled, the .lib file from the DLL is
already added in my "Additional Dependencies" property for the project...

Everything shoud work fine! Methods exported from the Win32 dll work fine on
my Windows Forms Application. Sure, they are being exported by the
"__declspec(dllexport)" directive, but the managed class is declared
"Public", so the class and its methods should be visible... Shouldn't them?

The Workaround is to add the "ManagedClass.cpp" file inside my "Windows
Forms"
project, and having the project compile again. Then I don't get the
unresolved external and things work. But this has some drawbacks, like
having to compile files twice... Surely it's not the desired solution!

Any clues?

Thank you for any help,

Gustavo Fabro

gu***********************@hotmail.com
Nov 17 '05 #1
0 1355

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

Similar topics

1
by: mccoyn | last post by:
I'm porting an old project to use .NET. When I try to link in a static library (.lib) that has a single managed class in it. I get the following errors: LINK : error LNK2020: unresolved token...
47
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen,...
2
by: Jacob Cohen | last post by:
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...
1
by: Eric Twietmeyer | last post by:
Hello, I'm starting to investigate cs, managed c++ and interoperating with a very large unmanaged code base. We are going to use Windows Forms (written in cs) to replace our old fashioned GUI. ...
2
by: Paul Kenny | last post by:
Hi, I am trying to expose the functionality of an unmanaged C++ class to the other languages available in the .NET Framework. I have decided to do this by wrapping the unmanaged C++ class in a...
0
by: Stefan | last post by:
Hi, Could anyone share some light on this and how I can work around it. If I have 2 managed c++ dlls (A and B) and B tries to use a native class in A this fails to link. It is easily...
9
by: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for...
3
by: WithPit | last post by:
I am trying to create an managed wrapper but have some problems with it by using abstract classes. In my unmanaged library code i had the following three classes with the following hierarchy ...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.