Recommended migration strategy for C++ project in Visual Studio 6
Asked Answered
R

10

10

For a large application written in C++ using Visual Studio 6, what is the best way to move into the modern era?

I'd like to take an incremental approach where we slowly move portions of the code and write new features into C# for example and compile that into a library or dll that can be referenced from the legacy application.

Is this possible and what is the best way to do it?

Edit: At this point we are limited to the Express editions which I believe don't allow use of the MFC libraries which are heavily used in our current app. It's also quite a large app with a lot of hardware dependencies so I don't think a wholesale migration is in the cards.

Edit2: We've looked into writing COM-wrapped components in C# but having no COM experience this is scary and complicated. Is it possible to generate a C# dll with a straight-C interface with all the managed goodness hidden inside? Or is COM a necessary evil?

Rosebud answered 2/6, 2010 at 14:34 Comment(3)
If you're going from C++ to C#, I'd say you're rewriting it, not migrating it. C# and C++ are different languages, and do things in different ways. Also, why are you limited to Express editions? If it's a budgetary issue, has management considered what it will cost to rewrite a large app with hardware dependencies?Footnote
I definitely DON'T want to rewrite the existing, working code, but would like to start adding new modules in C# if we can find a way for them to work together and do it incrementally. Maybe eventually rewrite the UI stuff if it's feasible. Also yes, the budget is very limited right now, especially since we're still feeling out the options.Rosebud
To call C# components fro native C++, you need to expose C# libraries as COM libraries, and use COM client code in C++. This is possible, some COM experience is desired. But moving to 2010 does not necessary requires using C#. You need to understand where C# is better than native C++. For example, database access in C++ is really nightmare, and it is very easy in C#. Try to be selective, and choose the better language for every specific task.Chondroma
C
6

Faced with the same task, my strategy would be something like:

  1. Identify what we hope to gain by moving to 2010 development - it could be

    • improved quality assurance: unit testing, mocking are part of modern development tools
    • slicker UI: WPF provides a modern look and feel.
    • productivity: in some areas, .NET development is more productive than C++ development
    • support: new tools are supported with improvements and bugfixes.
  2. Identify which parts of the system will not gain from being moved to C#:

    • hardware access, low-level algorithmic code
    • pretty much most bespoke non-UI working code - no point throwing it out if it already works
  3. Identify which parts of the system need to be migrated to c#. For these parts, ensure that the current implementation in C++ is decoupled and modular so that those parts can be swapped out. If the app is a monolith, then considerable work will be needed refactoring the app so that it can be broken up and select pieces reimplemented in c#. (It is possible to refactor nothing, instead just focus on implementing new application functionality in c#.)

  4. Now that you've identified which parts will remain in C++ and which parts will be implemented in c#, (or just stipulate that new features are in c#) then focus turns to how to integrate c# and c++ into a single solution

    • use COM wrappers - if your existing C++ project makes good use of OO, this is often not as difficult as it may seem. With MSVC 6 you can use the ATL classes to expose your classes as COM components.
    • Integrate directly the native and c# code. Integrating "legacy" compiled code requires an intermediate DLL - see here for details.

Mixing the MFC UI and c# UI is probably not achieveable, and not adviseable either as it would produce a UI mix of two distinct styles (1990s grey and 2010 vibe). It is simpler to focus on achieving incremental migration, such as implementing new application code in c# and calling that from the native C++ code. This keeps the amount of migrated c# code small to begin with. As you get more into the 2010 development, you can then take the larger chunks that cannot be migrated incrementally, such as the UI.

Clod answered 15/6, 2010 at 1:35 Comment(6)
Hmm, not really a reason to move to 2010. The same can be had by moving to Qt and keeping C++. That might be a lot easierTwine
@Stephan - not really "a reason" to move? - I gave more than one reason - which are meant as examples to prompt thinking about why this task is necessary, which I think is prudent before embarking on a any large effort - the OP doesn't say, but may have his own reasons for migrating. Even so, please explain why you don't think they are reasons to move to 2010.Clod
In the sense of: can be had without moving to 2010Twine
Can you explain how migrating to Qt/C++ is less painful - isn't it simply substituting one set of new techologies for another?Clod
Thanks to everyone for the helpful suggestions. We will probably do something like: convert the project to 2010 and keep most of the code in C++ for now. For the transition we are going to use the plethora of new compiler errors to suggest fixes we can make in our legacy project over the near term and then re-convert. At some point we might rework the UI but for now will remain 90s gray. We will try to start implementing new features using a more managed approach, either C# or C++/CLI. mdma: thanks for the thoughtful suggestions and the helpful codeproject link!Rosebud
This approach isn't as incremental as I was originally hoping -- at some point we'll have to make the big leap and so in the transition there will probably be a lot of back and forth and some duplicate effort, but it seems the most realistic approach to do this while continuing to support the code.Rosebud
W
12

I'd like to take an incremental approach where we slowly move portions of the code

That's the only realistic way to do it.

First, what kind of version control do you use? (If you use branching version control that allows you to make experiments and see what works, while minimizing the risk of compromising your code; others are OK also, but you'll have to be really careful depending on what you are using).

Edit: I just saw you are using SVN. It may be worthwile to move to mercurial or git if you have the liberty to do that (the change provides a quantum leap in what you can do with the code-base).

and write new features into C# for example and compile that into a library or dll that can be referenced from the legacy application.

That's ... not necessarily a good idea. C# code can expose COM interfaces that are accessible in C++. Writing client code in C++ for modules written in C# can be fun, but you may find it taxing (in terms of effort to benefits ratio); It is also slow and error-prone (compared to writing C# client code for modules written in C++).

Better consider creating an application framework in C# and using modules (already) written in C++ for the core functionality.

Is this possible and what is the best way to do it?

Yes, it's possible.

How many people are involved in the project?

If there are many, the best way would be to have a few (two? four?) work on the new application framework and have the rest continue as usual.

If there are few, you can consider having either a person in charge of this, or more people working part-time on it.

The percentage of people/effort assigned on each (old code maintenance and new code development) should depend on the size of the team and your priorities (Is the transition a low priority issue? Is it necessary to be finished by a given date?)

The best way to do this would be to start adapting modules of the code to be usable in multiple scenarios (with both the old code and the new one) and continue development in parallel (again, this would be greatly eased by using a branching distributed version control system).

Here's how I would go about it (iterative development, with small steps and lots of validity checks in between):

  1. Pick a functional module (something that is not GUI-related) in the old code-base.

  2. Remove MFC code (and other libraries not available in VS2010 Express - like ATL) references from the module picked in step 1.

    Do not attempt to rewrite MFC/ATL functionality with custom code, unless for small changes (that is, it is not feasible to decide to create your own GUI framework, but it is OK to decide to write your own COM interface pointer wrapper similar to ATL's CComPtr).

    If the code is heavily dependent on a library, better separate it as much as possible, then mark it down to be rewritten at a future point using new technologies. Either way, for a library heavily-dependent on MFC you're better off rewriting the code using something else (C#?).

  3. reduce coupling with the chosen module as much as possible (make sure the code is in a separate library, decide clearly what functionality the module exposes to client code) and access the delimited functionality only through the decided exposed interface (in the old code).

  4. Make sure the old code base still works with the modified module (test - eventually automate the testing for this module) - this is critical if you need to still stay in the market until you can ship the new version.

  5. While maintaining the current application, start a new project (C# based?) that implements the GUI and other parts you need to modernize (like the parts heavily-dependent on MFC). This should be a thin-layer application, preferably agnostic of the business logic (which should remain in the legacy code as much as possible).

    Depending on what the old code does and the interfaces you define, it may make sense to use C++/CLI instead of C# for parts of the code (it can work with native C++ pointers and managed code, allowing you to make an easy transition when comunicating between managed .NET code and C++ native code).

  6. Make the new application use the module picked in step 1.

  7. Pick a new module, go back to step 2.

Advantages:

  • refactoring will be performed (necessary for the separation of modules)

  • at the end you should have a battery of tests for your functional modules (if you do not already).

  • you still have something to ship in between.

A few notes:

  • If you do not use a distributed branching version control system, you're better off working on one module at a time. If you use branching/distributed source control, you can distribute different modules to different team members, and centralize the changes every time something new has been ported.

  • It is very important that each step is clearly delimited (so that you can roll back your changes to the last stable version, try new things and so on). This is another issue that is difficult with SVN and easy with Mercurial / Git.

  • Before starting, change the names of all your project files to have a .2005.vcproj extension, and do the same for the solution file. When creating the new project file, do the same with .2010.vcxproj for the project files and solution (you should still do this if you convert the solutions/projects). The idea is that you should have both in parallel and open whichever you want at any point. You shouldn't have to make a source-tree update to a different label/tag/date in source control just to switch IDEs.

Edit2: We've looked into writing COM-wrapped components in C# but having no COM experience this is scary and complicated.

You can still do it, by writing wrapper code (a small templated smart pointer class for COM interfaces wouldn't go amiss for example - similar to CComPtr in ATL). If you isolated the COM code behind some wrappers you could write client code (agnostic of COM) with (almost) no problems.

Is it possible to generate a C# dll with a straight-C interface with all the managed goodness hidden inside? Or is COM a necessary evil?

Not that I know of. I think COM will be a necessary evil if you plan to use server code written in C# and client code in C++.

It is possible the other way around.

Worshipful answered 15/6, 2010 at 13:38 Comment(0)
C
6

Faced with the same task, my strategy would be something like:

  1. Identify what we hope to gain by moving to 2010 development - it could be

    • improved quality assurance: unit testing, mocking are part of modern development tools
    • slicker UI: WPF provides a modern look and feel.
    • productivity: in some areas, .NET development is more productive than C++ development
    • support: new tools are supported with improvements and bugfixes.
  2. Identify which parts of the system will not gain from being moved to C#:

    • hardware access, low-level algorithmic code
    • pretty much most bespoke non-UI working code - no point throwing it out if it already works
  3. Identify which parts of the system need to be migrated to c#. For these parts, ensure that the current implementation in C++ is decoupled and modular so that those parts can be swapped out. If the app is a monolith, then considerable work will be needed refactoring the app so that it can be broken up and select pieces reimplemented in c#. (It is possible to refactor nothing, instead just focus on implementing new application functionality in c#.)

  4. Now that you've identified which parts will remain in C++ and which parts will be implemented in c#, (or just stipulate that new features are in c#) then focus turns to how to integrate c# and c++ into a single solution

    • use COM wrappers - if your existing C++ project makes good use of OO, this is often not as difficult as it may seem. With MSVC 6 you can use the ATL classes to expose your classes as COM components.
    • Integrate directly the native and c# code. Integrating "legacy" compiled code requires an intermediate DLL - see here for details.

Mixing the MFC UI and c# UI is probably not achieveable, and not adviseable either as it would produce a UI mix of two distinct styles (1990s grey and 2010 vibe). It is simpler to focus on achieving incremental migration, such as implementing new application code in c# and calling that from the native C++ code. This keeps the amount of migrated c# code small to begin with. As you get more into the 2010 development, you can then take the larger chunks that cannot be migrated incrementally, such as the UI.

Clod answered 15/6, 2010 at 1:35 Comment(6)
Hmm, not really a reason to move to 2010. The same can be had by moving to Qt and keeping C++. That might be a lot easierTwine
@Stephan - not really "a reason" to move? - I gave more than one reason - which are meant as examples to prompt thinking about why this task is necessary, which I think is prudent before embarking on a any large effort - the OP doesn't say, but may have his own reasons for migrating. Even so, please explain why you don't think they are reasons to move to 2010.Clod
In the sense of: can be had without moving to 2010Twine
Can you explain how migrating to Qt/C++ is less painful - isn't it simply substituting one set of new techologies for another?Clod
Thanks to everyone for the helpful suggestions. We will probably do something like: convert the project to 2010 and keep most of the code in C++ for now. For the transition we are going to use the plethora of new compiler errors to suggest fixes we can make in our legacy project over the near term and then re-convert. At some point we might rework the UI but for now will remain 90s gray. We will try to start implementing new features using a more managed approach, either C# or C++/CLI. mdma: thanks for the thoughtful suggestions and the helpful codeproject link!Rosebud
This approach isn't as incremental as I was originally hoping -- at some point we'll have to make the big leap and so in the transition there will probably be a lot of back and forth and some duplicate effort, but it seems the most realistic approach to do this while continuing to support the code.Rosebud
N
4

First, your definition of modern era is controversial. There's no reason to assume C# is better in any sense than C++. A lot has been said on whether C# helps you better avoid memory management errors, but this is hardly so with modern facilities in C++, and, it's very easy to do mess with C# in terms of resource acquisition timing, that may be dependent on what other programs are doing.

Noeminoesis answered 11/6, 2010 at 19:44 Comment(0)
T
3

If you move straight from 6 to 2010 you may end up with some messed up project settings. If this isn't a fairly large project, and it's one of few that you need to convert, then that should be fine. Just open it in 2010, and follow the conversion wizard. Make sure to back up your project first, and verify your project settings when you're done.

In my opinion though the best way is to convert it step by step through each iteration of Visual Studio. I had to modernize 1400 projects from 2003 to 2010, and the best way that I found was to convert everything to 2005, then to 2008, and then finally to 2010. This caused the least amount of issues to arise for me.

If you only have 6 and the newest Visual Studio you may end up just having to try and go straight to the new one using the wizard. Expect some manual cleanup before everything builds correctly for you again.

Also, one more time, BACK IT UP FIRST! :)

Transcaucasia answered 2/6, 2010 at 14:39 Comment(5)
Hopefully it's backed up in some sort of version control system.Magnesia
This is definitely something to try but don't know that a wholesale migration is possible for us right now (see Edit above). Right now we're playing with options on a separate SVN branch so no worries about losing the old project :)Rosebud
Is this a commercial project, or an independent one?Transcaucasia
commercial, but a small company with only 2 developersRosebud
It should be, but you never know. If everything was the way it should be the stack wouldn't be overflowing! :)Transcaucasia
C
3

High-level C++ code calling low-level C# code doesn't look like a good idea. The areas where .NET languages are better, are user interface, database access, networking, XML files handling. Low-level stuff like calculations, hardware access etc. is better to keep as native C++ code.

Moving to .NET, in most cases it is better to rewrite UI completely, using WPF or Windows Forms technologies. Low-level stuff remains native, and different interoperability technologies are used to connect C# and native code: PInvoke, C++/CLI wrappers or COM interoperability. After some time, you may decide to rewrite low-level native components in C#, only if it is really necessary.

About compiling native C++ code in VS2010 - I don't see any problems. Just fix all compilation errors - new compilers have more strict type checking and syntax restrictions, and catch much more bugs at compilation time.

Chondroma answered 2/6, 2010 at 14:59 Comment(0)
L
3

Not sure why so many folks are advocating for COM. If you haven't already got a lot of COM in there, learning how to do it on the C++ side is going to hurt, and then you're using the slowest possible interop from the managed side. Not my first choice.

Ideally you have refactored your UI from your business logic. You can then build a new UI (WPF, WinForms, ASP.NET, web services that support some other client, whatever) and call into your business logic through P/Invoke or by writing a C++/CLI wrapper. @mdma has good advice for you assuming that the refactoring is possible.

However if you were paying me to come in and help you my very first question would be why do you want to do this? Some clients say they don't want to pay C++ devs any more, so they want all the C++ code gone. This is a scary objective because we all hate to touch code that works. Some clients want to expose their logic to ASP.NET or Reporting Services or something, so for them we concentrate on the refactoring. And some say "it looks so 1999" and for them I show them what MFC looks like now. Colours, skinning/theming including office and win7 looks, ribbon, floating/docking panes and windows, Windows 7 taskbar integration ... if you just want to look different, take a look at MFC in VS 2010 and you might not have to adjust any code at all.

Finally to make non-Express versions of VS 2010 affordable look into the Microsoft Partner Program. If you have sold your software to at least 3 customers who still speak to you, and can get through the Windows 7 logo self test (I have got VB 6 apps through that in a day or two) then you can have 5-10 copies of everything (Windows, Office, VS) for $1900 or so a year, depending on where you live.

Latticed answered 17/6, 2010 at 11:54 Comment(0)
S
2

To start I'd try and keep as much code as possible to avoid a rewrite. I'd also remove all unused code before starting the conversion.

Since VC++ 6.0 Microsoft changed the MFC libraries and the C++ Standard Library.

I recommend to start building your DLLs with no dependencies, then looking at your third party libraries, and then rebuild one dependent DLL/EXE at a time.

Introduce unit tests to make sure the behaviour of code does not change.

If you have a mixed build, using different versions of VC++, you need to guard against passing resources (file handles) between DLLs that use different versions of the VC runtime.

Shingles answered 2/6, 2010 at 15:4 Comment(0)
B
2

If at all financially possible I would strongly consider just paying the money for the version of Visual Studio that you need because you could very well lose more money on the time you spend. I do not know enough about the express editions to give a good answer on them but when integrating some code from a subcontractor that was written in C++ I used C++ / CLI. You will probably be able to reuse most of your codebase and will be familiar with the language but you will also have access to managed code and libraries. Also if you want to start writing new code in C# you can do that. The biggest problem I had with it was that in VS 2010 there is no intellisense in C++ / CLI.

Bondswoman answered 17/6, 2010 at 11:37 Comment(0)
D
1

Visual Studio 6 is legendary for being buggy and slow. Moving into the modern era would best be done by getting a new compiler. What is probably the easiest thing to do is write the legacy app into a DLL, then write your exe into C# and use P/Invoke. Then you never have to touch the old code again- you can just write more and more in C# and use less and less of the old DLL.

If your old code is very heavily OO, you can use C++/CLI to write wrapper classes that allow .NET to call methods on C++ objects, and collect them too if you use a reference counted smart pointer.

Dafodil answered 2/6, 2010 at 14:44 Comment(0)
N
0

You can use C# to write your new components with a COM or COM+ (System.EnterpriseServices) wrapper, which will be callable from your existing C++ code.

Nickeliferous answered 2/6, 2010 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.