C++ wrap to C# efficiency

G

Guest

Hi,

I have some C++ static library which I would like to wrap and use in C#
applications. It appears that I have two options.

1. Wrap the static library as dynamic library (dll), use unmanaged C++. Then
invoke the method in C# by DllImport.

2. Wrap the static lib using managed C++, which will create a dll as .NET
assembly, so C# project can directly use it as reference and call methods
directly.

Not good as C++, if my understanding on approaches is correct, would the
first approach create faster execution for the same function since it is
mostly wrapped by "old faster C++" codes?


Thanks for help

Chris
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

Well, calling the library using managed C++ is going to be faster, as
you are not going through the P/Invoke layer, you will be making the call
natively (known as "it just works" or IJW). However, depending on how your
code is called, I would be inclined to say that this falls in the realm of
micro-optimization, and I would be more worried about getting the calls to
work first than optimizing on this level.

Now, if the calls to the functions in the library require some pretty
complex structure manipiulation, then I would say that this facilitates
creating the wrapper in C++, as it is better suited for this work. If you
are passing simple structure types, or even primitive types, then I would
say just do the wrapping into a DLL and make the calls through the P/Invoke
layer.
 
G

Guest

thanks

Nicholas Paldino said:
Chris,

Well, calling the library using managed C++ is going to be faster, as
you are not going through the P/Invoke layer, you will be making the call
natively (known as "it just works" or IJW). However, depending on how your
code is called, I would be inclined to say that this falls in the realm of
micro-optimization, and I would be more worried about getting the calls to
work first than optimizing on this level.

Now, if the calls to the functions in the library require some pretty
complex structure manipiulation, then I would say that this facilitates
creating the wrapper in C++, as it is better suited for this work. If you
are passing simple structure types, or even primitive types, then I would
say just do the wrapping into a DLL and make the calls through the P/Invoke
layer.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

chrisben said:
Hi,

I have some C++ static library which I would like to wrap and use in C#
applications. It appears that I have two options.

1. Wrap the static library as dynamic library (dll), use unmanaged C++.
Then
invoke the method in C# by DllImport.

2. Wrap the static lib using managed C++, which will create a dll as .NET
assembly, so C# project can directly use it as reference and call methods
directly.

Not good as C++, if my understanding on approaches is correct, would the
first approach create faster execution for the same function since it is
mostly wrapped by "old faster C++" codes?


Thanks for help

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top