none
Can I use C# to define the user interface and place the "meat" in native C++ DLLs? RRS feed

  • Question

  • Hi,

    I want to use C# to define the user interface of a desktop application while all the real work is delegated to a native C++ DLL. Is this possible?

    Regards,

    Juan Dent


    Juan Dent

    Saturday, August 19, 2017 9:18 PM

Answers

  • This is a different question; you are certainly adding to the initial question.

    Initially you said you wanted to use C# for the UI and that is good; .Net does not work well for UI stuff. The main problem is that the .Net UI design depends on partial classes and that is very different from the way that C++ does things. It is primarily the designer part of .Net. So for C++ most all of the code generated by the designer goes into header (".h") files and very little goes into the implementation (".cpp") files.

    The database does make things complicated. Probably the best solution is to use a multitiered architecture as in Chapter 5: Layered Application Guidelines with a Presentation Layer (UI), a Business Layer and a Data Layer (database). You can use C# for the UI and therefore use the designer that Visual Studio provides. You can use C++ for the business layer and use the Entity Framework in the data layer. That is a lot to learn but once you have done it, it should be easier to do many more applications.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Juan Dent Wednesday, August 23, 2017 12:42 AM
    Monday, August 21, 2017 5:45 AM

All replies

  • Hi,

    I want to use C# to define the user interface of a desktop application while all the real work is delegated to a native C++ DLL. Is this possible?

    Regards,

    Juan Dent


    Juan Dent

    • Merged by Stanly Fan Monday, August 21, 2017 6:38 AM the same thread
    Saturday, August 19, 2017 9:19 PM
  • Yes.  You can use P/Invoke, a managed C++ DLL to interface between C# and the native C++ DLL, or even expose the "meat" to C# through COM.  That's the simple answer.  The choice of method to use would be dependent on the particulars of the situation.
    • Proposed as answer by Baron Bi Monday, August 21, 2017 3:00 AM
    Saturday, August 19, 2017 10:29 PM
  • This is still one of the main reasons why managed C++ and C++/CLI was introduced.

    The /clr command line option for cl.exe, the C++ compiler, defaults to mixed mode assemblies. So even if Microsoft's support for it has dropped off recently, it is still probably the best way to create a mixed mode DLL for access in a managed application.

    Platform invoke can be fairly easy too, but this stops once you want more than C functions exported from the DLL. C# obviously wouldn't be able to understand C++ classes, so you would have to have an extra layer between. This is one of the reasons C++/CLI can be easier. There is also the whole thing about calling multiple native functions via P/Invoke from the same managed function that can cause performance issues because of the managed to native and native to managed transitions. So if you want to go the P/Invoke route, if you want to call multiple native functions from the same managed function, it would be better to write one native function that does all this work and call that from the managed function instead.

    Finally COM, unless you already have a COM component, the extra hassle to make it one isn't worth it if you can just use C++/CLI or P/Invoke to access it.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Proposed as answer by Baron Bi Monday, August 21, 2017 3:00 AM
    Sunday, August 20, 2017 12:09 AM
  • For the benefit of others, this is the same question as Can I use C# to define the user interface and place the "meat" in native C++ DLLs? so further responses should be posted there.


    Sam Hobbs
    SimpleSamples.Info

    Sunday, August 20, 2017 6:27 PM
  • C++ programmers often have a choice of either doing things the C way, such as C-style strings, or the C++ way, as in std::string. For .Net we also have the option to use C++/CLR, which is a .Net version of C++.

    I suggest using a Class Library (managed) style DLL; create the project as in the following:

    That is essentially the same as the "/clr command line option" that Darran Rowe describes.

    The disadvantage of that is that you need to learn C++/CLR a little bit, which is a little different from C++. For example, we use "gcnew" to allocate .Net objects and "new" for C++ objects. The advantage is that your C# program will look the same as if the DLL was a C# Class Library. You can still use C++ to do most of what you do iin C++. You will find many previous answers to questions about things like conversion of strings to/from std:string and things like that.

    See my Managed C++ Wrapper For Unmanaged Code for more about using a Class Library project.



    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by Simple Samples Monday, August 21, 2017 4:57 AM
    Sunday, August 20, 2017 6:53 PM
  • Hi,

    In keeping with the initial theme of this thread of questions, I have added a managed DataGrid to a Windows Form in C++/CLI. Now I want to connect this to a DataSet (or DataSource) created in native C++. What interface do I need to implement? Do I need to create a COM or .NET component?

    Regards,

    Juan Dent

    P.S.: My source will be either a SQL database or another collection of rows (tuples)


    Juan Dent

    Monday, August 21, 2017 1:42 AM
  • This is a different question; you are certainly adding to the initial question.

    Initially you said you wanted to use C# for the UI and that is good; .Net does not work well for UI stuff. The main problem is that the .Net UI design depends on partial classes and that is very different from the way that C++ does things. It is primarily the designer part of .Net. So for C++ most all of the code generated by the designer goes into header (".h") files and very little goes into the implementation (".cpp") files.

    The database does make things complicated. Probably the best solution is to use a multitiered architecture as in Chapter 5: Layered Application Guidelines with a Presentation Layer (UI), a Business Layer and a Data Layer (database). You can use C# for the UI and therefore use the designer that Visual Studio provides. You can use C++ for the business layer and use the Entity Framework in the data layer. That is a lot to learn but once you have done it, it should be easier to do many more applications.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by Juan Dent Wednesday, August 23, 2017 12:42 AM
    Monday, August 21, 2017 5:45 AM
  • While it is definitely possible as others have pointed out, there is a performance penalty in switching between managed and native execution. So do this with care. All operations possible with C# should be done there itself instead of doing it in C++. In short, you should have a damn good reason for doing what you're trying to do.

    «_Superman_»
    Microsoft MVP (Visual C++)(October 2009 - September 2013)

    Polymorphism in C

    Monday, August 21, 2017 6:03 AM
  • there is a performance penalty in switching between managed and native execution.
    That does not exclude C++ entirely from use with managed applications. Also, programmer productivity is also a concern. Programmers cost more than computer hardware. Programmers often misinterpret productivity as being laziness.


    Sam Hobbs
    SimpleSamples.Info

    Monday, August 21, 2017 6:45 AM