Solved

use a c++ library from c#

Posted on 2015-01-07
14
162 Views
Last Modified: 2015-01-11
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
0
Comment
Question by:thready
  • 7
  • 7
14 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 1

Author Comment

by:thready
Comment Utility
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
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 1

Author Closing Comment

by:thready
Comment Utility
LOL!!  COM, here I come again...  :-)
0
 
LVL 1

Author Comment

by:thready
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:thready
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 
LVL 1

Author Comment

by:thready
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Good luck then! *duck*
0
 
LVL 1

Author Comment

by:thready
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Well, I said it would save you from trouble ;o)

The marshalling stuff can really be a pain in the [censsored].
0
 
LVL 1

Author Comment

by:thready
Comment Utility
Here comes my next question in the c# section...  More points ?   :-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now