Wednesday, March 23, 2011

How to use existing C++ code in .NET (C#)

Hi,

I would like to create a C# project and implement the existing native (C++) code. Does anyone know about any good tutorial about it?

Thanks!

From stackoverflow
  • You can use P/Invoke and you can call unmanaged methods. I'll post you some examples: This is a reference to MSDN official documentation. This is a community mantained website with most of the common Windows unmanaged libraries along with method signatures and examples

  • Why don't you try managed wrappers for your C++ code.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcmg_accessingcodefromframeworkobjects.asp

  • If your C++ code is managed (as you say), then I'd suggest that you leave it as C++ unless you have a very good reason to -- future C# (etc.) assemblies should be able to just reference it 'as is'.

    Stu Mackellar : Good spot - I missed that and read it as "existing *un*managed (C++) code". Given the context it could well be a typo.
    Rowland Shaw : I've found through experience it's always worth checking the obvious first -- of course, if it was a typo, then lots have given suitable answers
    niko : my mistake... it's native C++
  • Probably the easiest way to do this is to create a C++/CLI project, which will enable you to mix managed and unmanaged C++. You can then wrap your existing unmanaged C++ classes with a managed .Net wrapper that your C# code can call directly. MSDN magazine has a good introductory article on C++/CLI.

    Interop will only really be useful for calling C-style functions. You can't really use it to interact with pure C++ classes.

  • I learned this by doing it, so I don't have a good tutorial, but there are a couple things you need to be aware of - managed C++ and C++/CLI both integrate with unmanaged code with very few ugly seams. This is not the case with C#. In managed C++, it's quite possible to do something like this:

    dotNetForm->Text = S"My Project"; // regular managed code
    ::SetWindowText(dotNetForm->Handle, "Your Project"); // mixed managed/unmanaged
    

    whereas in C# you would have to P/Invoke into SetWindowText--not that you really would: it's just a simple example, but you need to be aware that the managed C++ compiler is a weird combination of doing a lot of interop for you transparently and doing nothing for you at the same time (ie, it will do no implicit marashalling).

  • I assume by implement the existing code you mean you want to call it from C#, with minimal changes to the C++ code.

    I'm a fan and recent purchaser of the book C++/CLI in Action which has a couple of useful sample chapters online.

    This intro on CodeProject is a good starting point.

    The author of C++/CLI in Action has a number of articles on CodeProject, scroll down to the C++/CLI section on his index.

    The Wikipedia article on P/Invoke has a number of reasons why you might not want to use that approach, with which I agree:

    • loss of typing support by the compiler
    • possible data type or alignment issues as you have to map types by hand
    • need to pin garbage-collected objects

    The best starting point on MSDN is the summary article.

0 comments:

Post a Comment