Managed and unmanaged C++: error LNK2020: unresolved token

  • Thread starter Thread starter Roland
  • Start date Start date
R

Roland

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 read the other topics on
the same problem here on the groups but haven't found the solution to
my problem yet.

I have written a managed C++ wrapper class called MDirectShowHandler.
This currently wraps only the constructor and destructor calls (see
code below) in the unmanaged class DirectShowHandler. When I try to
build this, the .obj files are created but the linker throws errors:

error LNK2020: unresolved token (06000001)
ManagedDirectShowHandler.MDirectShowHandler::.ctor
error LNK2020: unresolved token (06000002)
ManagedDirectShowHandler.MDirectShowHandler::Finalize
error LNK2020: unresolved token (06000002) delete
fatal error LNK1120: 3 unresolved externals

Here are the 4 files:

ManagedDirectShowHandler.h:
---------------------------
#pragma once
#include "DirectShowHandler.h"
#pragma managed
#using <mscorlib.dll>
using namespace System;

namespace ManagedDirectShowHandler
{
public __gc class MDirectShowHandler
{
public:
MDirectShowHandler();
~MDirectShowHandler();
private:
DirectShowHandler __nogc* m_pDirectShowHandler;
};
}

ManagedDirectShowHandler.cpp:
-----------------------------
#pragma once
#include "stdafx.h"

#include "ManagedDirectShowHandler.h"

public __gc class MDirectShowHandler
{
public:
MDirectShowHandler::MDirectShowHandler()
{
m_pDirectShowHandler = new DirectShowHandler();
}

MDirectShowHandler::~MDirectShowHandler()
{
m_pDirectShowHandler->~DirectShowHandler();
}
private:
DirectShowHandler __nogc* m_pDirectShowHandler;
};

DirectShowHandler.h:
--------------------
#pragma once
#pragma unmanaged

class DirectShowHandler
{
public:
DirectShowHandler(void);
~DirectShowHandler(void);
};

DirectShowHandler.cpp:
----------------------
#include "StdAfx.h"
#include ".\directshowhandler.h"
#include <iostream>
#using <mscorlib.dll>
using namespace std;

// Constructor for DirectShowHandler (this class is unmanaged)
DirectShowHandler::DirectShowHandler(void)
{
cout << "DirectShowHandler created" << endl;
}

DirectShowHandler::~DirectShowHandler(void)
{
cout << "DirectShowHandler destroyed" << endl;
}


The Visual Studio project is set to .NET Class Library, so that it
should create a DLL which I can then add to my C# project.

Any ideas why the errors mentioned above occur? This is quite
frustrating as there don't seem to be any good examples that take you
through it step by step.

Thanks,

Roland
 
Hello, I just sorta stumbled on a possible solution to your (and my) quandry. I myself have been trying to figure out how to create a C++ .net class for use in a vb.net project. My C knowledge is extreemly poor but up to the task of the C code I need to make, just not how to get that code to work with an external program. btw: I do have some java experience which makes some C structures to code easier to understand.

I was also having a problem getting a simple test to compile. I defined a function int Hello() in the .h file and then in the cpp file I had only one line of code: return 10;

This was just to test and see how to get communications up but I kept getting the same linking error as yourself. I tried a variety of things and then remembered something about objects in C++ using double : (ex ::) to reference sub parts of objects. Following the below mentioned setup I get something that compiles and works with my vb.net code. (Note that there was some trial and error before this point).

ProjectName::ClassName::MethodName(){
Code;
}

ProjectName is the name of the project you setup. Classname is what is defined in the .h file where the helper sais to put your methodnames at. MethodName is... well, your method name.

I hope this helps.
 
Check Your Namespace

I think you're forgetting...

using namespace ManagedDirectShowHandle;

the compiler can't find the constructors and destructors for the header you defined.

(I realize I'm late in helping you... but I ran into the same problem and then found the fix).

Jason
http://www.channelgee.com
 
Back
Top