legacy code visual basic to c# - c#

I'm building an c# app that is designed to replicate and enhance the functionality of an VB6 app developed many years ago.
My problem is that one module in the old app does some realy complicated things and trying to replicate this starting from specs would be a big pain for me. Even if this sounds like the best idea.
My question is : is there a way i could keep the old VB module and integrate it into the c# app, so that i wouldn't have to rewrite everything ?
Thanks!

Yes. You can turn the module into a COM object, and .Net will happily call into COM objects. Depending on what you have right now you may need to find a copy of Visual Studio 6 to make that happen (it's still available on MSDN if you have a subscription). To give you more exact instructions we'd need to see more of your code.

Visual Studio has a wizard that allows you to convert VB6 into VB.NET, which you could then reference in your C# project and use. However, this migration tool is no longer shipped with Visual Studio 2010 (it was shipped in older versions) Even still though, there generally is a lot of manual touchup work required, you are generally better off rewriting it by hand which could result in higher quality code, anyway. My recommendation to you is to rewrite the code, you can make touchups and make sure that it works correctly without having to go the COM/Interop route, but don't necessarily shun that option either. If rewriting all the code really is an issue, then interop is a solution.

You can use COM or export the module as a DLL. Here's some information for you regarding VB6 and COM.

In theory yes, turning it into a com module, and calling it from your c# code. Whether this would be easier or not, I am not sure.
The problem is that this issue will not go away, and if there are changes needed to the module, or the interface, then someone has to do it. So practical yes, but consider carefully what you are doing.

You can migrate the existing vb code to vb.net and compile it as a separate assembly and add it to the C# application.

Can't VS2010 (maybe 2008) support both VB and C# projects in the same solution? If I'm not mistaken, they can. So you could actually convert your VB6 code into VB.Net and have the project in your solution with the C# stuff.

Related

Easiest way to Implement Scripting to WPF C# Visual Studio 2015

I'd like some help in how to add scripting support to a WPF C# project I'm doing on Visual Studio 2015. One of the things I'd like to do is to be able to change User Control properties within that script. I've being trying Roslyn C#, and I read some stuff about IronPython and PowerShell Tools. But, all that information is not really helping.
So, do you have a simple answer? Like, the easiest way to execute scripts in Visual Studio 2015 C# WPF Application, that are able to change properties of User Controls within the project?
Any help would be appreciated.
Thanks,
Lucas.
T4 templates can help but they are less extensible than lets say Roslyn.
I found a great post on how to easily implement IronPython and how to access User Controls within the scripts in Python, so you can there change different Control properties, and even create them! Here's the link:http://www.codeproject.com/Articles/602112/Scripting-NET-Applications-with-IronPython
I think it should be quite simple to embed powershell or F# interactive in an application and there are lots of examples when we search the web for this. The question is only are we comfortable writing scripts in Powershell and F#?
In my application I provide a C# editor and compile scripts as static methods (which I uses as functions) using the standard .Net compiler API's which are installed with the framework (Microsoft.Csharp, System.CodeDom). This generates a temporary dll and perhaps can not be properly termed a script. The implementation is straight forward and more or less what you expect - I have to provide a way for the users to specify references and namespaces, then generate a source file, compile it to an assembly and then call members of the static class. This above technique works well because the application has extension points that are well adapted to user defined code with a certain well defined structure (you might have similar well defined needs and perhaps full scripting is not necessary).
On the other hand, I am considering providing a "super user" console where the application can be driven by script, and in this case, I will use either Powershell or F-sharp interactive (I'm leaning towards F#). I don't think the implementation is particularly complex, but these are things that can easily bring the application down or corrupt application data if it is not well controlled - and it is unlikely that my users will profit from scripting as it is a programmer environment.

C++/CLI Settings.settings designer like in C#

I am currently writing my GUI program in C++/CLI. (I know, not good. And I am not sure about it ever since I first thought about switching to C#. Another Question of mine).
Another question here that might convince me is, if there is anything compareable to the Settings-Designer in C# that I can use in C++/CLI. I got it working with my own implementation of the Settings-class but it is kind of error-prone. So, are there any designers or functions I can use in C++/CLI to make the app- and user-Settings management easier?
No. The missing feature in the C++ IDE is the code generator that auto-generates code from the setting designer. Microsoft just never invested the energy to create the VS add-ins required to support it, code generators like this were only implemented for the VB.NET and C# IDEs. Multiple reasons for that, a bit beyond the scope of this Q+A.
You can still use the settings designer, you just have a jump through a few hoops. Empowered by the excellent support for language interop in .NET, you can simply add a C# class library project to your solution. Then Project > Properties > Settings > click Create. On the toolbar, change the Access Modifiers combobox from Internal to Public so your C++/CLI code can use them. Add your settings as usual.
Add the project reference to your C++/CLI project and you can now use ClassLibrary1::Properties in your code just as you would in a C# app. Just a different namespace.
One more hoop, saving the tricky one for last, you have to copy the app.config file from your C# project to your C++/CLI's build directory. Use xcopy in a post-build event to do that, final name must be yourcppapp.exe.config. God help you if you have to merge other .config settings, that's very hard to automate. Fwiw, best to go with the flow, you are in C++ land now. It is supposed to be hard, otherwise anybody could do it :) Settings only work for small monolithic LOB apps anyway, not the typical C++ target.

Converting Visual Studio 6 Libraries To Visual Studio 10

I am just looking at the options for now but I have a large number of C libraries that were written in Visual Studio 6 as statically linked .lib. Newer development needs to be done in Visual Studio 2010 that will call these libraries and it has been decided that although there are possible solutions to calling these old libraries, that they should be updated to Visual Studio 2010. As part of that conversion, it is looking likely that we might as well convert them to an OO language. Whether that be C++, C++/CLR or C# is up for discussion.
New applications may be written in C# that will call these libraries.
What would be best to convert the libraries to? It also needs to be a fairly straightforward (i.e. not too time consuming) option. I think converting them to C# will be too much work. Unmanaged C++ would be the easiest but would C++/CLR be a good middle ground? I am thinking here that it is easier to call C++/CLR from C# than unmanaged C++ and therefore would be worth the little more work here
Rewriting from scratch is not an option. There are over 100 libraries
and a lot of code.
If rewritting them is not an option I don't understand this statement.
As part of that conversion, it is looking likely that we might as well
convert them to an OO language. Whether that be C++, C++/CLR or C# is
up for discussion.
You cannot "convert" C code to C# and/or C++/CLR code. The only correct term would be a total and complete rewrite. It sounds like your better of just loading the project in the newest visual studio and compiling it as is.
If it already works there is no reason to change the language the dll is written in.
By the sounds of it...You don't seem to be familar with any of the languages your suggesting considering you had a question about the term unmanaged C++.
We have already gone down the route of leaving these libraries as C
written in VS 6 and trying to call them from C# written in VS 2010. It
got a little messy and it was decided that this was not a good long
term direction.
This means your only real choice is to rewrite the libraries so they are easier to work with. Just switching to C++ will not solve the problems you ran into. You can use C style conventions within C++ without an issue.

Recommended migration strategy for C++ project in Visual Studio 6

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?
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):
Pick a functional module (something that is not GUI-related) in the old code-base.
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#?).
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).
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.
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).
Make the new application use the module picked in step 1.
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.
Faced with the same task, my strategy would be something like:
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.
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
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#.)
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.
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.
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! :)
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.
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.
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.
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.
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.
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.

Intellisense in Visual Studio 2005 between C# and VB - can't navigate to definitions

I'm absolutely stunned by the fact that MS just couldn't get it right to navigate to the definition of a method, when you're combining C# and VB projects in one solution. If you're trying to navigate from VB to C#, it brings up the "Object Explorer", and if from C# to VB, it generates a metadata file.
Honestly, what is so complicated about jumping between different languages, especially if they're supposedly using the same CLR?
Does anyone know why this is, or if there's any workaround?
Did they get it right in VS 2008?
#Keith, I am afraid you may be right about your answer. I am truly stunned that Microsoft screwed this up so badly. Does anyone have any ideas for a workaround?
#Mladen Mihajlovic - that's exactly the situation I'm describing. Try it out yourself; project references don't make a shred of difference.
This is general to both languages.
F12 in VB.Net always takes you to the object browser
F12 in C# always takes you to a meta-data definition
This is a deliberate mechanism to try and match expected behaviour for upgrading users. The C# way gives you the right information, but the VB way is what users of VBA or VB6 will expect.
The behaviour is the same in VS2008.
These are the rules for external projects, both should take you to the code if it is in the same solution.
You're quite right - VB projects treat C# projects as external and vice versa - you can't navigate from code in one to the other. I've tested this in the latest VS2008 and it's still an issue.
It also fails to get complete meta-data. Add a method to your C# code and it won't appear in VB's intellisense until you compile the C# assembly.
This is similar to how components appear in the toolstrip, so I figure the normal navigate to code functionality is a feature of code with a common compilers, and everything else uses some kind of reflection.
As long as you're still building a PDB it should be able to find the files, I guess it doesn't because they need it to support release builds too. It couldn't find the line of code without the PDB lookups.
Make sure that your reference is to the VB project and not just a DLL file.
It's a known issue, the workaround are two: use ctrl+, or use some plugin that add this function, like resharper (that will add this function in the F12).

Categories

Resources