Click to See Complete Forum and Search --> : Using *.lib file in managed code


cabasm
April 17th, 2008, 08:49 AM
I want to use a LIB file (*.lib) from managed code (C# code). To accomplish this I created a C++ managed class library project and wrote a class like below:

// MyClass.h
#pragma comment(lib, "MyLib.lib")

#pragma unmanaged

// This class is defined in MyLib.lib
class __declspec(dllimport) MyClass
{
public:
wchar_t* SomeMethod( );

virtual void Run( ) = 0; // pure virtual method makes this class abstract
};


#pragma managed

public class MyClassWrapper : public MyClass
{
public:
virtual void Run( )
{
// do some actions
}
};

namespace MyLibNs
{
// ref attribute makes the Run method visible to C# code (VS2008)
// in previous versions of VS2008, __gc is used instead of ref
public ref class Wrapper
{
MyClassWrapper* _pWrapper;

public:
Wrapper( ) { _pWrapper = new MyClassWrapper; }

void Run { _pWrapper->Run( ); }
};
}

Building the solution results in a linker error like below:

error LNK2001: unresolved external symbol "public: void __thiscall MyClass::Run(void)" (?Run@MyClass@@$$FQAEPB_WXZ)
fatal error LNK1120: 1 unresolved externals

How can I get rid of this? Is there a better solution to use a LIB file from within C#?

Thanks!