VC++6 MFC migration managed C++(windows form )

B

Bredal Jensen

I want to port my MFC (VC++6) application to manageg VC ++. 7. I want to do
this because some things are much better done with C#. So i could write a C#
class and use in
my VC++ code as Languague interoperability is one of the main features of
the .Net
framework. I know my application would have to obey to the CTS (common type
specification) but my main concerns are the MFC stuff!
I do not really know which options you have with windows forms. Can they do
what MFC
can do? How about functions pointers. How does managed C++ deal with this?
Maybe this is not the right place to post this, but any usefull comment will
be very
appreciated.

Thanks
 
N

Niki Estner

Bredal Jensen said:
I want to port my MFC (VC++6) application to manageg VC ++. 7. I want to
do
this because some things are much better done with C#. So i could write a
C#
class and use in
my VC++ code as Languague interoperability is one of the main features of
the .Net
framework.

First port it to unmanaged VC++ 7. MFC 6 and MFC 7 do have some differences.
After that, compiling the code as managed code is usually only a compiler
switch.
I know my application would have to obey to the CTS (common type
specification) but my main concerns are the MFC stuff!

You can still do anything in managed C++; You only have to obey CTS rules
for the types you want to expose to other languages (like C#)
I do not really know which options you have with windows forms. Can they
do
what MFC can do?

IMO it would be a good idea to convert your MFC app to WinForms, however
there's no real need to do that.
How about functions pointers. How does managed C++ deal with this?

It's managed, but it's still C++. You have const types, templates, function
pointers, void pointers...

If you can't compile your C++ code as managed code, you could still use COM
interop.

Hope this helps,

Niki
 
B

Bredal Jensen

Well thanks for your answer, i have already compiled with Vc++.7 .


So what is this compiler switch i have to add?
 
N

Niki Estner

Bredal Jensen said:
Well thanks for your answer, i have already compiled with Vc++.7 .


So what is this compiler switch i have to add?

Go to the properties of your project - general tab - switch "use managed
extensions" to "yes".
You may have to resolve a few conflicts with other project settings, but
afterwards you should be able to include managed code into your application.

Niki
 
B

Bredal Jensen

Many thanks , this sounds so great to me , i never though it would be so
simple.

Well , i did that and now it says :


"Command line error D2016 : '/RTC1' and '/clr' command-line options are
incompatible"
 
N

Niki Estner

Bredal Jensen said:
Many thanks , this sounds so great to me , i never though it would be so
simple.

Well , i did that and now it says :


"Command line error D2016 : '/RTC1' and '/clr' command-line options are
incompatible"

Yes, I had the same experience with converted VC6 projects. You can either
create a new dummy MFC project in VC7 and "copy" all compiler settings (new
projects are configured so they can be compiled with managed extensions), or
you can adjust the compiler switches one by one with each error;

Have a look at the MSDN article on the "/clr" compiler switch, it should
contain all the information you need.

Niki
 
B

Bredal Jensen

I'm having trouble finding where these "compiler settings" can be found.
I'm using visual studio ..Net
 
N

Niki Estner

Bredal Jensen said:
I'm having trouble finding where these "compiler settings" can be found.
I'm using visual studio ..Net

Really? It's quite easy to find information on them...

Example: /RTC1;
Open up MSDN; Go to the "Index" Tab; Enter "/RTC1"; Read the article,
especially this paragraph:
"To set this compiler option in the Visual Studio development environment:
1. Open the project's Property Pages dialog box. For details, see Setting
Visual C++ Project Properties.
2. Click the C/C++ folder.
3. Click the Code Generation property page.
4. Modify one or both of the following properties: Basic Runtime Checks or
Smaller Type Check."

Niki
 
B

Bredal Jensen

Now my program compiles as manages C++ and actually runs.
Is that all i had to do to run as managed C++?

Well i must say your informations are very accurate and i'm
really thankfull for all these help.

I still have questions though. I now decided to write one of my user
interface part as a C# "windows control" .

I need to do the equivalent of (Postmessage or SendMessage) plus send data
like with (wParam and lParam) . Is there a way of doing this?

Again thank you so much....
 
N

Niki Estner

Bredal Jensen said:
Now my program compiles as manages C++ and actually runs.
Is that all i had to do to run as managed C++?

It's not 100% managed code yet, but you can reference managed assemblies and
create managed types. The possibility to have managed and unmanaged code in
one assembly is one of the major advantages of managed C++. The price is
that you often have to use keywords like __gc or __value to tell the
compiler which way to go.
Well i must say your informations are very accurate and i'm
really thankfull for all these help.

I still have questions though. I now decided to write one of my user
interface part as a C# "windows control" .

I need to do the equivalent of (Postmessage or SendMessage) plus send data
like with (wParam and lParam) . Is there a way of doing this?

You can still P/Invoke SendMessage and PostMessage with the DllImport
attribute, and every control has a protected virtual method "WndProc".
However, it's usually a bad idea to use them if you can avoid it: First of
all, those "wParam/lParam" parameters tend to disturb the GC if they carry
pointers; They are ugly to debug, and error-prone.
Depending on what you want to do, I'd suggest having a deeper look at
events, delegates, interfaces (for loose-coupling), or the Control.Invoke
method (for inter-thread communication).

Niki
 
B

Bredal Jensen

What i want to do:

I was once C++ addict but since i tried C#, i just don't want to use
too much of C++ anymore.
I have this huge MFC application and it needs new features. I then want to
do all
new developement in C# and work my way through like this.

An alternative is a whole rewrite in C# , but it does not seem like our
budget
allow this for now.
So here is what want to do. Did that sound Crazy? ;)
 
N

Niki Estner

Bredal Jensen said:
What i want to do:

I was once C++ addict but since i tried C#, i just don't want to use
too much of C++ anymore.
I have this huge MFC application and it needs new features. I then want to
do all
new developement in C# and work my way through like this.

An alternative is a whole rewrite in C# , but it does not seem like our
budget
allow this for now.
So here is what want to do. Did that sound Crazy? ;)

No: thanks to managed C++ you can port an application step-by-step without
pain.
But you should always keep the architecture of your application in mind: If
you have to add a new feature which you would have put into a separate DLL
anyway, fine write it in managed code. However, if you have to add a feature
to an existing class, you should either write it in C++, or port the whole
class (or maybe even bigger chunks of your application) to C#. Otherwise
your application will be a mess quite soon.
Also, you should identify parts of your application that would profit most
from .NET (especially the ones that are bound to change), and port them to
C# when you have the time (you'll be lucky you did when you don't have the
time any more).

I think that's a good way to go, and if you do it right, it should save you
lots of time.

Niki
 
B

Bredal Jensen

well , i'm mostly going to build "windows control library" and use "Add
reference" and instantiate the classes.
Actually i though i could also just instantiate a C# class not in a binary
form. shouldn't that in theory be possible?
 
N

Niki Estner

Bredal Jensen said:
well , i'm mostly going to build "windows control library" and use "Add
reference" and instantiate the classes.

use the "new" operator to instantiate classes. adding a reference only makes
the classes in an assembly visible, it doesn't instantiate them yet.
Actually i though i could also just instantiate a C# class not in a binary
form. shouldn't that in theory be possible?

The C# class would have to be compiled, so it has to be in a binary form;
You can compile it at runtime, but I'm not sure if that's what you mean.

Niki
 
B

Bredal Jensen

What i mean is :

making my C# class available to a c++ class with:


"using Mynaspace.Myclasss "

and Myclass mc = new Myclass(....
 
B

Bredal Jensen

An other issue.
Now that my code is Dot net "ready"
i though i could just use fx: using System.Net . in my C++ file.
But that does not seem to work. I recall you saying my code
was not 100% manged . is that why i can not do this?
 
N

Niki Estner

Bredal Jensen said:
An other issue.
Now that my code is Dot net "ready"
i though i could just use fx: using System.Net . in my C++ file.

Did you reference the right assemblies? I think this namespace is
implemented in System.dll - you'll have to add a reference to it.
But that does not seem to work. I recall you saying my code
was not 100% manged . is that why i can not do this?

You should be able to do this; However it is possible that you will not be
able to use managed code in some of your functions because they already
contain native code; simply create a new empty function then, put the
managed code in it , and call it from your unmanaged function.

Niki
 
B

Bredal Jensen

I think i have a problem my visusal studio installation. i need to do a
reinstall.
I can not even see the add reference option .

But you are right i have not added a reference to "System.dll ".

I 'll try again later when i ahve a reliable visual studion installation.
 
N

Niki Estner

Bredal Jensen said:
I think i have a problem my visusal studio installation. i need to do a
reinstall.
I can not even see the add reference option .

Really? Select your project in the solution explorer and choose "Add
Reference" from the project menu.

You can also do the same from the source code with the "#using" preprocessor
directive.

Niki
 
B

Bredal Jensen

I can not see it in the project menu .
The IDE "Itself" also prompted me to reinstall .

But before i do , could you show code snippet of how to use "#using " thing.


Assum i want to get the list of my local ip/ips . I know
this can be done with Ip helper but i guess it must be much
more straight forward using .Net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top