none
C# and C++/CLI dilemma RRS feed

  • Question

  • Hello,

    I'm in the process of creating a File Manager, it's a a side project of mine and I'm having hard times of choosing between C# and C++/CLI.

    Now, the reason I want to create it in C++ is avoiding the interoperability to unmanaged code.

    Now my C++ isn't great at all and I'll probably have a lot of catching up to do, the question is whether I can write everything in C# and use C++/CLI for the parts that requires me to use unmanaged libraries ? will I get any performance benefits in doing so ? is it even a good idea ?

    I fear for the amount of wrappers I'll have to create if I'll ever do that in C#, what's your opinion and thoughts ?

    However, like I already stated my C++ skills are falling behind, where do you think I should start as a .Net-centric developer to get up to par with C++/CLI ? what is required of me ?

    Should I even think about C++/CLI ? or I better off doing that in traditional C++ ?

    I already posted few questions in the C++ forum and I got some nice leading links, although I hope that someone here can write and share is own experience.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Thursday, March 17, 2011 7:18 AM
    Moderator

Answers

All replies

  • C++ is good for cases where you don't want your users to be required to install .NET framework.

    C# and especially WPF is 10 times easier and faster for creating graphical user interfaces. Also, since it is managed code, you don't have to worry about memory leaks and that means faster coding. I haven't seen no performance issues with C# and I see no advantage in writing mixed code.

    If you are ok with your clients installing .NET framework, C# is the way to go, definitely.


    Tan Silliksaar milemarx.com
    Thursday, March 17, 2011 7:27 AM
  • I know what are the benefits of C# and overall .Net development I've been doing that for the past 10 years and in enterprise projects what I want to know is the benefits of using C++/CLI in comparison to C#, this goes far beyond installing the .Net framework which is not really an issue nowadays.

    You see no advantage of mixed code after I said that my main concern is interoperability ? can you elaborate more on why you think a mixed code has no advantages ? because I see the exact opposite.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Thursday, March 17, 2011 8:26 AM
    Moderator
  • Well, people write mixed code because they feel more comfortable with c++ and that's ok. If you feel more comfortable with c++ then that's an advantage, of course.

    Some years ago, I had the same thing, but not any more because I feel absolutely comfy with c# now. After you make c# your own, there is no need for any mixed code, really. And if you are concerned about performance, then this is something that can be learned (as the techniques are different).

    The thing is, if there is an advantage, it must be personal.


    Tan Silliksaar milemarx.com
    Thursday, March 17, 2011 8:47 AM
  • @Tan Silliksaar, people write mixed code mainly because there's a need to do so, not because they feel comfortable with it.

    It might be an advantage but not the kind of advantage I'm looking for.

    I'll just give a certain scenario to make things easier.

    Say I have a compression library written in C++ and big parts of the application is written in C#, obviously if I used C++ this was a non-issue, but given that I choose to use C# how much I gain by using C++/CLI for interoperability than doing this through P/Invoke ?

    At first I thought like creating everything in C++/CLI, I read few articles and it seems that there are various issues with it, so I might end up creating some of the component in traditional C++ and most of the UI in C#.

     


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Thursday, March 17, 2011 2:07 PM
    Moderator
  • Can anyone share his own experience, at least ?

    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Friday, March 18, 2011 9:27 AM
    Moderator
  • Here are some articles I found for anyone who wants to study the issues I dealt with.

    MSDN: Native and .NET Interoperability

    .NET to C++ Bridge

    Interop Without PInvoke - Consuming Native Libraries in C#.

    How to Marshal a C++ Class


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Friday, March 18, 2011 4:11 PM
    Moderator
  • Would you consider an MFC approach?

    I've had a similar problem, choosing between writing an application in either MFC C++ or C# to make use of a customers own USB protocol with their own APIs. In the end, I went with both - the MFC application used on production lines where there is no time available to install the dot net framework and a C# WinForms applications for hardware developers with more diagnostic functions involved.

    I know this post probably isn't too helpful but I found the C++ code much easier to work with due to the native API calls. I also found it a good learning experience using C++ and the whole MFC framework - something which I can now apply better to future projects.

    Make no mistake though, I still prefer to use C# wherever I can.

    K

    Friday, March 18, 2011 4:25 PM
  • Well, at first I actually wanted to write everything in C++ so instinctively I thought about MFC.

    I read quite a bit about it and from what I gathered there are mixed opinions about MFC, mostly because in various places I've seen people commented on the lack of documentation, is it that bad ?

    Some says MFC is not as good as QT and due to my lack of knowledge I just declined both and picked the thing I know best C# :p

    Also, one of the reasons I ditched MFC is because I want to spend more time learning C++ rather than delving into MFC, although I have pretty good understanding when it comes to Win32 so this might help my learning curve (hopefully).

    Your post is quite useful! thank you.

    P.s For what it's worth I'm posting this in the C# forum because my current plan is to create the UI in C# and as you can read the thread my main concern was and is interoperability, secondly I wanted to understand how C# developers handled some of my issues and expected people to share their experience.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.

    Friday, March 18, 2011 7:51 PM
    Moderator
  • The documentation for MFC is there, it just takes a bit of digging around via Google to actually find a good example to go with the MSDN help.

    I still don't really understand why there is no comprehensive documentation and intellisense for MFC/C++ like there is for dot net, I guess that is the art of native programming (and yes, "programming" compared to dot net "developing").

    Personally, if I were going to involve CLI then I'd just use C#.

    Good luck!

     

     

    Friday, March 18, 2011 8:26 PM
  • I see, I'll have a look.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Saturday, March 19, 2011 5:01 AM
    Moderator
  • Hi Eyal,

    Have you solved this issue? Any update here?


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 23, 2011 9:04 AM
  • If you are trying to use C++ classes from a library then the easiest way will be to use C++/CLI, but if you are just going to be using CDECL functions exported from a DLL, then I'd definitely use C# with PInvoke.
    Wednesday, March 23, 2011 9:53 AM
  • Eyal,

    As soon as you use one single line of C# it makes no sense anymore (beside of existing code or very unsafe code) to use C++. 

    It is up to you if you use C++, Java, C# or VB mostly is that all personal preference from the one who uses it. 

    In the case of C languages like C++ and Java and C# it is even slight because those are all mathematical (Algol) based program languages.

    However, for me is important that I get with .Net an seem to be endless repository of classes which I simply can use.

     


    Success
    Cor
    Wednesday, March 23, 2011 9:59 AM
  • If you are trying to use C++ classes from a library then the easiest way will be to use C++/CLI, but if you are just going to be using CDECL functions exported from a DLL, then I'd definitely use C# with PInvoke.

    So basically you're telling me that I better off going with P/Invoke (for libs that have their functions exported) than creating wrappers for them in C++/CLI ? or just stating that it's easier not necessarily better ? can you tell me the differences from performance standpoint ?

    @Cor, I know it is up to me when it comes to the language I use, I'm not speaking about that, my concerns are existing libraries that I'm may end up using to perform various things in my File Manager, some of them are performance intensive such as compression libraries that every CPU cycle can be significant and make an impact.

    I hate repeating myself but I've said so multiple times and I think it should be clear now that my main concern is interoperability.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Wednesday, March 23, 2011 1:51 PM
    Moderator
  • Hi Eyal,

    Have you solved this issue? Any update here?


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Well, I'm still having doubts of using P/Invoke once I'll get into testing in the upcoming weeks I can mark the closest reply that answered my expectations, so please, can you leave this thread open for a while  ? it's appreciated.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.

    Wednesday, March 23, 2011 2:01 PM
    Moderator
  • Some functions signatures have marshaling issues which call for the use of a C++/CLI wrapper. That would be my criterium to decide to use a wrapper. I never felt the need to test performances.
    Wednesday, March 23, 2011 4:12 PM
  • @Louis, can you please elaborate a bit so I can understand what kind of functions exactly requires it ? links would be just fine.

    Thank you and everyone else.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.

    Wednesday, March 23, 2011 9:10 PM
    Moderator
  • Any function returning a non-blittable struct is incompatible with p/invoke. That was the problem here: http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/e79c0a76-52bc-48ec-9dce-3b8163dd3876. In that case, with a short array, the workaround is not a big deal, but it could become painful with a more complex structure.

    Passing an array of structures is not impossible, but you need to do quite some work if you use only C#. See here: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/a83a5786-b515-4892-a369-7cd1867b64ff

    Thursday, March 24, 2011 12:29 AM