use a c++ library from c#

Hi Experts,

I have a .lib file that I would like to use from a c# application.  What's the best way to do this?

Thanks,
Mike
LVL 1
threadyAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
You can use P/invoke, but that will be clumsy. E.g. like

class SampleClass [

public:
  SampleClass(const char* str) : name(str) {}

  int Add(int n1, int n2) { return n1 + n2;}

private:

  std;;string name;
};

extern "C"
__declspec(dllexport)
long CreateSampleClass(const char* param) {

  SampleClass* p = new SampleClass(param);

  return (long) p;
}

extern "C"
__declspec(dllexport)
int Call_Add_OnSampleClass(long obj, int param1, int param2) {

  SampleClass* p = (SampleClass*) obj;

  return p->Add(param1,param2);
}

Open in new window


Keeping in mind that P/invoke stinn involves using 'StringBuilder' and other means of custom marshalling, I personally would tend to COM/Interop

But anyway, see http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx ("Platform Invoke Tutorial"), http://msdn.microsoft.com/en-us/library/aa446536.aspx ("An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact Framework") as well as http://www.codeproject.com/Articles/12121/Essential-P-Invoke ("Essential P/Invoke")
0
 
jkrCommented:
There is either P/Invoke that works well with calling unmanaged functions, but since you wrote 'C++', I assume 'classes' where COM Interop will suit you better. Taker a look at the article at http://www.codeproject.com/Articles/5001/NET-COM-Interoperability (".NET - COM Interoperability") and if you can use the newer .NET version, look here: http://www.codeproject.com/Tips/143694/Get-rid-of-COM-Interop-DLL-by-using-the-new-C-dy ("Get rid of COM Interop DLL by using the new C# 4 dynamic keyword")

Caveat: For either approach, you will have to turn that static library into a DLL or wrap it with one.
0
 
threadyAuthor Commented:
Great answer.  But I'm not sure I want to use COM.  They need to change the name or something.  It just seems too old for some reason.  I know that's not warranted.

I've been trying to keep things extra clean- using boost and standard c++ only.  Then you had to go and mention COM.  Not very nice.

I do like P/Invoke...  But I do have classes...  Hmmmmmm.......
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
jkrCommented:
BTW:

>> I've been trying to keep things extra clean- using boost and standard c++ only.
>> Then you had to go and mention COM.

*pointing-finger*

You mentioned .NET first ;o)
0
 
threadyAuthor Commented:
LOL!!  COM, here I come again...  :-)
0
 
threadyAuthor Commented:
I'm working on C++/CLI now and I saw a great example on youtube where you just create a CLR class library wrapper.  I didn't see the need to use COM classes yet...  Or is this more hidden nowadays? (no _i.h & .c file)...

Also, the example I saw passed a pointer to an int array.  Converting a C# to a c++ standard string is another story.

My plan of attack... Please correct me if/where I get a bad idea?
0-  Create the CLR Class library project since it facilitates the bridge between C# and unmanaged C++ for us.
1-  The C# stuff I can keep encoded in UTF16 since it's all Windows UI.
2-  The C# String is passed to the wrapper as-is.
3-  The wrapper converts the UTF16 C# string to c++ UTF-8 std::string (** somehow ** don't know how yet)
4-  The c++ library is pure business logic, encoded in UTF-8.
5-  do the unsafe code block to call the business logic back...

This is cool stuff.  I hope I don't hit a wall...  Thanks again.
0
 
jkrCommented:
So far, that sounds OK to me. But why not strictly sticking with UTF16 throughout the whole project? This will save you from nasty surprises when some aspects change (don't ask how much encoding 'fun' we had when brokering connections between a legacy Windows app and iOS via a C# Windows service recently)
0
 
threadyAuthor Commented:
i like the idea of dropping wstring and T_ and L"" in code.  It is not cross platform.

You can encode UTF-8 in standard C++ in a std::string too.. and if I understood correctly, most if not all the strlen functions work properly...  Also, UTF-16 byte order can change- and it's not fixed width, so it's wider than need be....

What kinds of aspects can change?  If I make it a rule that my business logic remains always in UTF-8, and that I only convert to whatever is needed on the boundaries, then all is well....  No?
0
 
jkrCommented:
Well, if I *knew* what could change, things would be way too simple :-D

No, maybe I am just too overcautious and pesimistic, but that's for a reason. I've seen it all to often that customers all of a sudden add requirements that interfere with previous assumptions and therefore try to make no assumptions at all.
0
 
threadyAuthor Commented:
And now I think I've run into the need for COM....  I thought I could use my static library from within my CLR class library... But getting linker errors now... (LNK2038).  More reading to do... <sigh>
0
 
jkrCommented:
Good luck then! *duck*
0
 
threadyAuthor Commented:
Amazing.  The CLR class library does away with the visible need for COM.  You just use your .lib as an implementation within and expose what you want to c#...  You do the conversion to the different types within this library.  I'm up and running!  :-)
0
 
jkrCommented:
Well, I said it would save you from trouble ;o)

The marshalling stuff can really be a pain in the [censsored].
0
 
threadyAuthor Commented:
Here comes my next question in the c# section...  More points ?   :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.