Linking problems with managed/unmanaged classes

A

akash

I'm having problems calling an unmanaged class from a managed wrapper.
I suspect I'm missing something obvious, as I'm unfamiliar with C++ and
classes are very simple. My unmanaged class is as follows:

// Unmanaged.h
#pragma once
namespace Unmanaged {
class __declspec(dllexport) UnmanagedClass
{
// TODO: Add your methods for this class here.
public:
double DoUnmanagedStuff();
};
}

// Unmanaged.cpp
#include "stdafx.h"
#include "Unmanaged.h"
class UnmanagedClass
{
public:
double DoUnmanagedStuff() {
return 1.234;
}
};


This class is called from a managed wrapper as follows:

//Managed.cpp
#include "stdafx.h"
#include "Managed.h"
#include "..\Unmanaged\Unmanaged.h"

using namespace Unmanaged;

public ref class ManagedClass {
private:
UnmanagedClass* UClass;

public:
ManagedClass() {
UClass = new UnmanagedClass();
}
~ManagedClass() {
delete UClass;
}
double DoStuff() {
return UClass->DoUnmanagedStuff();
}
};

I get the following error when I try to compile the code:

error LNK2028: unresolved token (0A00000C) "public: double __thiscall
Unmanaged::UnmanagedClass::DoUnmanagedStuff(void)"
(?DoUnmanagedStuff@UnmanagedClass@Unmanaged@@$$FQAENXZ) referenced in
function "public: double __clrcall ManagedClass::DoStuff(void)"
(?DoStuff@ManagedClass@@$$FQ$AAMNXZ)

Any ideas what is going on?
Thanks.
 
B

Ben Voigt

akash said:
I'm having problems calling an unmanaged class from a managed wrapper.
I suspect I'm missing something obvious, as I'm unfamiliar with C++ and
classes are very simple. My unmanaged class is as follows:

// Unmanaged.h
#pragma once
namespace Unmanaged {
class __declspec(dllexport) UnmanagedClass
{
// TODO: Add your methods for this class here.
public:
double DoUnmanagedStuff();
};
}

Defines "::Unmanaged::UnmanagedClass", including a forward declaration of
"double ::Unmanaged::UnmanagedClass::DoUnmanagedStuff(void)"
// Unmanaged.cpp
#include "stdafx.h"
#include "Unmanaged.h"
class UnmanagedClass
{
public:
double DoUnmanagedStuff() {
return 1.234;
}
};

Defines "::UnmanagedClass" with a definition of "double
::UnmanagedClass::DoUnmanagedStuff(void)". This class is different from
"::Unmanaged::UnmanagedClass".

Try:
// Unmanaged.cpp
#include "stdafx.h"
#include "Unmanaged.h"

double Unmanaged::UnmanagedClass::DoUnmanagedStuff(void)
{
return 1.234;
}
 
A

akash

Thanks for the suggestion. Unfortunately I now get two errors:

error LNK2028: unresolved token (0A00000C) "public: double __thiscall
Unmanaged::UnmanagedClass::DoUnmanagedStuff(void)"
(?DoUnmanagedStuff@UnmanagedClass@Unmanaged@@$$FQAENXZ) referenced in
function "public: double __clrcall ManagedClass::DoStuff(void)"
(?DoStuff@ManagedClass@@$$FQ$AAMNXZ)

error LNK2019: unresolved external symbol "public: double __thiscall
Unmanaged::UnmanagedClass::DoUnmanagedStuff(void)"
(?DoUnmanagedStuff@UnmanagedClass@Unmanaged@@$$FQAENXZ) referenced in
function "public: double __clrcall ManagedClass::DoStuff(void)"
(?DoStuff@ManagedClass@@$$FQ$AAMNXZ)

I must still be doing something stupid, but I can't figure out what!
 
B

Ben Voigt

I note you have declspec(dllexport). Are you actually importing a DLL here,
or are Unmanaged.cpp and Managed.cpp in the same project?
 
A

akash

I am using a DLL. When I put the two classes in the same project, it
all works. Clearly I'm still doing something stupid regarding DLL use,
but I'm not sure what.

For the moment I'll live with having everything in the same
project...unless you have any suggestions?
 
B

Ben Voigt

akash said:
I am using a DLL. When I put the two classes in the same project, it
all works. Clearly I'm still doing something stupid regarding DLL use,
but I'm not sure what.

Oh!

You need to use dllexport inside the DLL and dllimport outside. A #define
is good for this...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top