Combining C++ and C#

Is it a good idea to combine C++ and C# or does it pose any immediate issues?

I have an application that needs some parts to be C++, and some parts to be C# (for increased efficiency). What would be the best way to achieve using a native C++ dll in C#?

-------------Problems Reply------------

Yes using C# and C++ for your product is very common and a good idea.

Sometimes you can use managed C++, in which case you can use your managed C++ module just like any other .NET module.

Typically you'd do everything that you can in C#. For the parts you need to do in C++ you'd typically create a C++ DLL and then call into that DLL from C#. Marshalling of parameters is done automatically for you.

Here is an example of importing a C function inside a DLL into C#:

[DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);

The question is clearly how to integrate his own C++ code to his C# solution, not just what attribute to use in order to call an existing function from the win32 API. Even if the answer was already accepted, I think it's incomplete, and the following should apply.

Yes, it is common practice in the cases where the task can either run faster, use less resources, and also in some cases to access methods that are not available in the .net framework.

If your goal is to gain efficiency you need to code a native unmanaged C++ library, you will create a new unmanaged c++ project(that compiles as a dll library) in visual studio and reference this library from your C# project.

In your case it seems you might be writting an unmananged C++ library, and the following applies.

As for any immediate issues you were asking about, it would impact deployment and obfuscation.

  • Deployment: Keep in mind that the C# dlls you build will run on any cpu, both 32 and 64bit, but this new native and unmanaged C++ library will force your program to be either for 32 or 64 specific.

    This is something you will configure in your Visual Studio configuration manager, and will be taken care of at compile time, you will pick AnyCPU for C# assemblies and for your new unmanaged C++ library, which will be in it's own project, you will have to choose from win32 or x64.

    So you will now have 2 setups, it is recommended best practice to have separate setups, one for 32 and another one for 64. Or since 32bit support is dropping very fast, you could focus on 64bit only.

    Also, your library might end up referencing the VC++ redistibutable provided by visual studio, which you might have to include in your deployment, altho some version of it are included on many OS, I found it's rarely the same one I compiled with and it's best to deploy it with your application to be sure. If this pack is missing, the target machine will have a SideBySide exception in the eventviewer->application log.

    To catch and handle an exception thrown from unmanaged code, the only catch that works is the empty one, the one with no exception type in the parenthesis after the catch(). So you can wrap your calls to unmanaged code in this to handle all unmanaged exception thrown from inside the unmanaged code, if you put a .net type like catch(Exception), it will just jump over it. The only way to catch an unmanaged exception, inside managed code is in this format.


try
{
//call unmanaged code
}
catch
{
//handle unmanaged exception
}

  • Obfuscation: Any method calls done from C# that are now calling unmanaged code will now be excluded from renaming automatically. And on the flip side, if your unmanadged c++ library needs to call methods from your managed assemblies, those will need to be excluded from renaming, manually, in order to be visible to the C++ library calling them.

If what you need is only to call well known c++ libraries like the Windows ones, you will NOT need to create a new unmanged c++ project, only use the [DllImport()] attribute suggested in a previous answer. And in this case you could take a look at this reference http://www.pinvoke.net/

What about C++ .NET? You can write managed code as well as native c++ code.

Category:c# Views:0 Time:2010-03-28
Tags: c#

Related post

  • How to combine two projects in Mercurial? 2008-08-15

    I have two separate mercurial repositories. At this point it makes sense that they "become one" because I want to work on the two projects simultaneously. I'd really like the two projects to each be a subdirectory in the new repository. How do I merg

  • How to combine several C/C++ libraries into one? 2008-08-16

    I'm tired of adding ten link libraries into my project, or requiring eight of them to use my own. I'd like to take existing libraries like libpng.a, libz.a, libjpeg.a, and combine them into one single .a library. Is that possible? How about combining

  • Find the best combination from a given set of multiple sets 2008-08-18

    Say you have a shipment. It needs to go from point A to point B, point B to point C and finally point C to point D. You need it to get there in five days for the least amount of money possible. There are three possible shippers for each leg, each wit

  • Web 2.0 Color Combinations 2008-08-26

    What are the most user-friendly color combinations for Web 2.0 websites, such as background, button colors, etc.? --------------Solutions------------- ColorSchemer will suggest good schemes for you. If you want to try something out on your own, try C

  • Split data access class into reader and writer or combine them? 2008-08-27

    This might be on the "discussy" side, but I would really like to hear your view on this. Previously I have often written data access classes that handled both reading and writing, which often led to poor naming, like FooIoHandler etc. The rule of thu

  • How do you combine multiple result sets in SSRS? 2008-08-29

    What's the best way to combine results sets from disparate data sources in SSRS? In my particular example, I need to write a report that pulls data from SQL Server and combines it with another set of data that comes from a DB2 database. In the end, I

  • Combining and Caching multiple JavaScript files in ASP.net 2008-09-06

    Either I had a bad dream recently or I am just too stupid to google, but I remember that someone somewhere wrote that ASP.net has a Function which allows "merging" multiple JavaScript files automatically and only delivering one file to the client, th

  • Can't Re-bind a socket to an existing IP/Port Combination 2008-09-07

    Greetings, I'm trying to find a way to 'unbind' a socket from a particular IP/Port combination. My pseudocode looks like this: ClassA a = new ClassA(); //(class A instantiates socket and binds it to 127.0.0.1:4567) //do something //...much later, a h

  • Why does Path.Combine not properly concatenate filenames that start with Path.DirectorySeparatorChar? 2008-09-09

    From the Immediate Window in Visual Studio: ? System.IO.Path.Combine(@"C:\test",@"test") "C:\\test\\test" ? System.IO.Path.Combine(@"C:\test",@"\test") "\\test" It seems that they should both be the same. The old FileSystemObject.BuildPath() didn't w

  • Is Asp.net and Windows Workflow good combination? 2008-09-15

    I am implementing a quite simple state-machine order processing application. It is a e-commerce application with a few twists. The users of the application will not be editing workflows by themselves. Microsoft claims that asp.net and Windows Workflo

  • Combined SVN FTP system? 2008-09-16

    Are there any systems out there where one can check in changes for a website and have that automatically update the website. The website effetively runs off the latest stable build the whole time without the need to ftp the files to the server. -----

  • Combining two SyndicationFeeds 2008-09-17

    What's a simple way to combine feed and feed2? I want the items from feed2 to be added to feed. Also I want to avoid duplicates as feed might already have items when a question is tagged with both WPF and Silverlight. Uri feedUri = new Uri("http://st

  • Is it possible to combine a series of PDFs into one using Ruby? 2008-09-17

    I have a series of PDFs named sequentially like so: 01_foo.pdf 02_bar.pdf 03_baz.pdf etc. Using Ruby, is it possible to combine these into one big PDF while keeping them in sequence? I don't mind installing any necessary gems to do the job. If this i

  • Combine rows / concatenate rows 2008-09-18

    I'm looking for an Access 2007 equivalent to SQL Server's COALESCE function. In SQL Server you could do something like: Person John Steve Richard SQL DECLARE @PersonList nvarchar(1024) SELECT @PersonList = COALESCE(@PersonList + ',','') + Person FROM

  • What is a y-combinator? 2008-09-18

    A y-combinator is a comp-sci concept from the "functional" side of things. Most programmers don't know much at all about them, if they've even heard about them. What is a y-combinator? How do they work? What are they good for? Are they useful in proc

  • How to combine requests for multiple javascript files into one http request? 2008-09-19

    This concept is a new one for me -- I first came across it at the YUI dependency configurator. Basically, instead of having multiple requests for many files, the files are chained into one http request to cut down on page load time. Anyone know how t

  • Algorithm Issue: letter combinations 2008-09-19

    I'm trying to write a piece of code that will do the following: Take the numbers 0 to 9 and assign one or more letters to this number. For example: 0 = N, 1 = L, 2 = T, 3 = D, 4 = R, 5 = V or F, 6 = B or P, 7 = Z, 8 = H or CH or J, 9 = G When I have

  • How does Jan Willem Klop's "(L L L-)" Y combinator work? 2008-09-21

    I understand what a Y Combinator is, but I don't understand this example of a "novel" combinator, from the Wikipedia page: Yk = (L L L L L L L L L L L L L L L L L L L L L L L L L L) Where: L = λabcdefghijklmnopqstuvwxyzr. (r (t h i s i s a f i x e d

  • Combining Scriptaculous and JQuery in a Rails application 2008-09-22

    I've got the following situation A rails application that makes use of rjs / Scriptaculous to offer AJAX functionality Lot of nice javascript written using JQuery (for a separate application) I want to combine the two and use my JQuery based function

  • Combination of more than one crypto algorithm 2008-09-23

    I'm considering the following: I have some data stream which I'd like to protect as secure as possible -- does it make any sense to apply let's say AES with some IV, then Blowfish with some IV and finally again AES with some IV? The encryption / decr

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.144 (s). 11 q(s)