Solved

use a c++ library from c#

Posted on 2015-01-07
14
174 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
ID: 40537071
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
ID: 40537110
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
ID: 40538375
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 86

Expert Comment

by:jkr
ID: 40538386
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
ID: 40540537
LOL!!  COM, here I come again...  :-)
0
 
LVL 1

Author Comment

by:thready
ID: 40542010
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
ID: 40542043
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
 
LVL 1

Author Comment

by:thready
ID: 40542048
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
ID: 40542081
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
ID: 40542094
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
ID: 40542198
Good luck then! *duck*
0
 
LVL 1

Author Comment

by:thready
ID: 40542361
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
ID: 40542555
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
ID: 40543176
Here comes my next question in the c# section...  More points ?   :-)
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 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