1

I'm trying to use a C++ library (GAlib) in my C# project, in Visual Studio 2010.

I built the library to get the .lib and, thanks for your useful advice:

Using c++ library in c#

I'm able to call the functions but I not able to create classes inheriting from the ones present within the library.

I have followed this guide

http://blogs.msdn.com/b/vcblog/archive/2008/12/08/inheriting-from-a-native-c-class-in-c.aspx

I added

__declspec(dllexport) class GAEvalData {
public: 
. . . 
}

but using dumpbin /EXPORT ga.lib to achieve the EntryPoint, I have no info.

I report the dumpbin output:

Dump of file ga.lib
File Type: LIBRARY

And nothing else.

Also inverting

class __declspec(dllexport) GAEvalData {
public: 
. . . 
}

it seems no work.

Any suggestions?

Is this the best way to inherit classes from external libraries?

Thanks in advance,

Dave

Community
  • 1
  • 1
Dave
  • 11
  • 2
  • 3
    Can you compile the C++ library in C++/CLI? – CodesInChaos Feb 05 '12 at 13:11
  • 1
    You might also want to look into SWIG or CXXI. | [CXXI github](https://github.com/mono/cxxi) | [CXXI introduction post](http://tirania.org/blog/archive/2011/Dec-19.html) – CodesInChaos Feb 05 '12 at 13:15
  • You need to ignore what's in that blog post. The number of ways you'll blow your foot off are too numerous. The failure mode is very nasty, debugging a corrupted GC heap is zero fun. Writing a C++/CLI wrapper is by far the simplest way. http://stackoverflow.com/a/2691448/17034 – Hans Passant Feb 05 '12 at 17:13
  • Thanks Hans for reply, but I don't understand why I should write a C++/CLI wrapper since I use C#. I think I would rewrite the declaration of the class using [DllImport("cppexp.dll", EntryPoint = "??0CSimpleClass@@QAE@H@Z", ... ] for each Method of the Class, as suggested by blogs.msdn.com/b/vcblog/archive/2008/12/08/… but I need the Entry Point that dumpbin does not return. Hans, what I expected to do is exactly the one posted by "Micah 23 Jan 2009 11:07 PM" in the link http://blogs.msdn.com/b/vcblog/archive/2008/12/08/inheriting-from-a-native-c-class-in-c.aspx . Thanks in advance. – Dave Feb 05 '12 at 21:16

1 Answers1

1

Normally you should declare your class similar to:

#ifdef DBTOOL_EXPORTS
#define DBTOOL_API __declspec(dllexport)
#else
#define DBTOOL_API __declspec(dllimport)
#endif

class DBTOOL_API Class {}

Your C++ dll should define DBTOOL_EXPORTS. I think the idea is clear. Hopefully it will help

nogard
  • 9,432
  • 6
  • 33
  • 53
  • That's not relevant to C# code, it doesn't #include a header file. – Hans Passant Feb 05 '12 at 17:09
  • Thanks nogard for reply, but I think it is the same thing I've done.. I still cannot see Class object in my C# code, what I expected to do is exactly the one posted by "Micah 23 Jan 2009 11:07 PM" in the link http://blogs.msdn.com/b/vcblog/archive/2008/12/08/inheriting-from-a-native-c-class-in-c.aspx . Thanks in advance. – Dave Feb 05 '12 at 21:17