Search code examples
c#visual-c++winforms-interop

Unmanaged code in .Net


We have a custom Listview Treegrid in c++ project built on the Codeproject TreeGrid example from here. The code has been modified quite extensively over time.

I have a requirement to port the c++ project to C# Winforms. I'm thinking of creating a c++ dll for the Listview grid and interoperate with it in Winforms.

Am I asking for for trouble using an unmanaged Listview control in Winforms? Should I just port/rewrite the listview in Winforms? (A task I do not look forward too)


Solution

  • If I were you, I would prefer rewriting the Listview in C# or looking for a free or paid Grid that was written in C# and is meant to be used in Winforms.

    Don't forget that you will have to support the code after you do this job, this probably includes fixing bugs and adding features. It's better to know exactly how your code works and how to debug it. You also gives you a chance to write a Listview that is better suited for your needs.

    This will be quite difficult if you'll use this C++ Listview and use it in Winforms. But if you decide using it, there are two options that I can think of:

    1. Creating a wrapper for the Listview in C++/CLI that will hold the object and show the data in C#, this means that you will have to write a lot of conversion code to move the data from the Listview to the C# objects, but it should work.

    2. Using DllImport and working with the ListView as a datasource, I'm not really sure how exactly you will be able to get all the data from it, but it prevents you from writing the ugly CLI code...

    So to wrap it up, I recommend using a single language for UI code, and my language of choice for UI is C#, not C++.

    Good luck...