I prefer C# over (Managed)C++.
However there are some case (particularly those involving lots of interop)
where it makes perfect sense to write a ManagedC++ DLL (to be consumed by
C#).
The new managed C++ coming with v2.0 has a new syntax, much less ambiguous,
much cleaner.
But it's still C++ (you've got to maintain 2 file per class (.h, .cpp),
cross reference are a tedious to declare,
method::name::are::so::long::to::declare, etc..)
Usually, I program in C# and, eventually, I write "quick" managed C++
wrapper around complex API.
However I am n't sure it's really necessary in your case.
What's obvious to me is that you know naught of Interop and are struggling.
Of course you need to redefine all function/structure/other in C#, in a way
you've got to "rewrite the header" in C#.
Let's make a quick arbitrary rule of the thumb. If this library's header is
less than..... 200 line of code you be better of going the C# way, if it's
more than that maybe you should try Managed C++ instead?
function pointer in a structure are a bit tricky... but you could use IntPtr
from a delegate (but should ensure your managed delegate is not collected!)
a structure is often defined this way:
[StructLayout(LayoutKind.Explicit)]
public struct MyStruct
{
public IntPtr functionPointer;
public int anInt;
}
Sreenath said:
Thank you Lloyd,
I tried the interop but the problem with interop is structure parameters.
External unmanaged C++ library has functions with various structures,
pointer
variables as parameters to the function calls. These are all from the
third
party lib. So, before using them, I think I need to define them. Am I
right?
If I use DLLImport and use the extern then I am having problem with
structure definitions because I didnot define them.
I haven't tried the managed C++ in .net, how is that? Is that going to be
little difficult than writing old classical VC++, in that case, is it
better
to write in C++ instead of C#?
thanks,
Sreenath
Lloyd Dupont said:
There is 2 solution:
- code in managed C++, you keep coding as you used to, plus you've got a
few
new keywords to call into managed API.
the down side is the classical verbosity of C++ code.
- use Interop (look for interop in the SDK's documentation), that let you
call directly C function in a DLL from C#.
In effect you have to declare the function in C# (kind of rewritting the
header in .NET).
It's often, but not always, as simple as that:
class NativeCall
{
[DllImport("MyDll")]
public static extern int AnIntFunction(int anIntParam);
}
Sreenath said:
Hello,
I might be posting in a wrong place but in that case I thought this
will
guide me to the right place.
I have a third party .dll, .lib and .h files, I need to use those do
develop
a .net service app to communicate to the third party software at the
other
end. The .h files gives information about structures and functions,
classes
the third party uses.
How to use those files in .net as references/include to create a .net
service. In C++ world, we include .h files as part of project and add
the
.lib files. But how can we do in .net?
any kind of help is appreicated.
Thanks,
S