|
Hi ya
I'm working on a native C++ Client/Server application, where the Client is implemented using MFC. The Client and Server shares a common DataModel-library.
I want to perform a incremental conversion of the MFC Client to a .NET implementation. The code-base is rather large (and obscure), so turning on /clr for the entire Client-project is not a good option.
Is it possible to create a new ManagedClient-library, which the original Client-project can link with. Then slowly move the views and controls from the original Client-Project to the ManagedClient-library while converting them to managed c++/c#. So the main Client-project will continue to be unmanaged but will link to a managed library.
At the same time the ManagedClient-library will probably need to access classes in the DataModel-library. Is it possible to create a ManagedDataModel-library which contains managed wrappers around the native DataModel-library.
Is there a better way for incrementally converting an existing MFC application ? And are there any obvious pitfalls I should be aware of ?
Best regards
-Rolf
|
|
|
|
|
Yes. You can maintain your old MFC client application while calling into new managed DLLs. In fact, this is an article I've wanted to write for awhile.
Any file which will be calling into a C# DLL will need to have the following options set in its Properties dialog:
* C/C++/General - Compile with common language runtime (/clr)
* C/C++/General - Debug Information Format - Program Database (/Zi)
* C/C++/Code Generation - Basic Runtime Checks - Default (others may work too)
* C/C++/Code Generation - Enable Minimal Rebuild - No
* C/C++/Code Generation - Enable C++ Exceptions - Yes With SEH Exceptions (/EHa)
* C/C++/Precompiled Headers - Create/Use Precompiled Header - Not Using Precompiled Headers
Add the following near the top of your file (after #include's)
* #pragma managed
* using namespace System::Windows::Forms;
* using namespace MyManagedDLL;
...and any other namespaces you may want to add. This just makes coding easier so you won't always need to prepend your classes with the fully qualified name.
Right click on your project and select References. Add the following references.
* mscorlib
* System
* System.Windows.Forms
* Your special C# DLL
Depending on your code, you may need to add more References. You will receive compilation errors if anything is missing.
The above will allow you to call a managed DLL from your client MFC application. There might be one thing I left off. I've had others do this same thing and I recall one time someone mentioning they had to add another reference I missed.
Going from managed to non-managed is a different story. One option is to use P/Invoke, but that is really best used for calling 'c' functions, not for C++. There is quite a lot written in regards to P/Invoke on this site.
http://www.codeproject.com/KB/miscctrl/HostMFC.aspx[^]
I haven't yet used it, but plan to look more into this for work when I have more time. This is a project explaining how to host your MFC application from within a .NET App Framework. It looks very interesting.
I have not really had any pitfalls, but there are some things to be aware of. You will need to write code to convert between your C++ data types/classes and the managed data class. You will need to write this code in C++ using CLI.
|
|
|
|
|
I was actually planning to link with a static library compiled with /CLR (Giving access to .NET), while the old MFC project remains untouched. Is this not possible ?
Found this article that seems to answer some of my technical questions Mixed (Native and Managed) Assemblies
modified on Tuesday, October 28, 2008 11:50 AM
|
|
|
|
|
I've got friend suggesting me to convert cons char* variable to char* by copying the data. I mean using: char *x = strdup(const char *y).
But I think I can easily typecasting it, like this: x = (char *) y.
Am I doing a good coding practice here because I prefer typecasting it I don't even get warning for doing this.
Any way are they the same? Which one should be a good programmer do?
THX!!!!
From Indonesia with love..!!
|
|
|
|
|
auralius wrote: Am I doing a good coding practice here
No. Casting away const is bad. Without the const it implies
the location is writable, which it is not, otherwise it wouldn't
be const
You'd only need a strdup() duplicate of the string if you're going to
alter its contents.
Static casting is rarely necessary so I always consider it bad practice.
Casts are not for fixing compiler errors and warnings!
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Casting is always "dangerous", so think what you do. It is really often the course of strange bugs and problems, which appear months or years after writing the crap. (believe me)
Types are your helpers, if it is a const think why!!! Why isnt char* x const?
x = (char *) y : the same string to which are pointing two pointers.
In your case with strdup you got another string !!!
(you must understand that, or stop coding )
PS: I compile my Release builds always with warning level 4
Greetings from Germany
|
|
|
|
|
yup....i think you booth are right..
i've got some old functions which return a const char* variable...
that's confusing me since i need also to modify the result...
thanks kastenk
thanks mark
From Indonesia with love..!!
|
|
|
|
|
hi
i want to create a property on my object and
type of it is system::io::ports::serialport , but i can't !
how can i create this property ?
|
|
|
|
|
iman_kh wrote: i want to create a property on my object
What is a property on an object?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
iman_kh wrote: i want to create a property on my object
do you mean property for your class? If yes see the below code
property System::IO::Ports::SerialPort^ SampleProperty
{
System::IO::Ports::SerialPort^ get(){
}
void set(System::IO::Ports::SerialPort^ port){
}
}; C++/CLI also has a simple way to declare the properties. You just need to write
property System::IO::Ports::SerialPort^ SampleProperty; . Compiler generates the backing fields for you.
|
|
|
|
|
hi,
yes
so thanks for your help,this was helpful
|
|
|
|
|
I am using Visual c++ NET 2005.
How can I get the system date ?
|
|
|
|
|
There is DateTime class in .Net Framework.
|
|
|
|
|
Use DateTime class to get the system date. The Now method returns the current system time.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
|
I want Copying the Data from an ASCII Text File on a PC Back to an Operating System Spooled File for Iseries don't use ftp.
thanks very much.
|
|
|
|
|
It is rude to cross post.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
I'm trying to write a function but I believe my parameters are wrong.
void printWinner(matchWon, gameWon, gameLost)
{
cout << endl << endl
<< "Player Matches Games Games" << endl;
<< " Won Won Lost " << endl;
<< "------ ------- ----- -----" << endl;
<< " A " << matchWonA << " " << gameWonA
<< " " << gameLostA << endl;
<< " B " << matchWonB << " " << gameWonB
<< " " << gameLostB << endl;
<< " C " << matchWonC << " " << gameWonC
<< " " << gameLostC << endl;
This being my code, I was wondering if I should give a function to each of my players (A, B, C).
So, void printAScore (matchWonA, gameWonA, gameLostA)
void printBScore (matchWonB, gameWonB, gameLostB)
and same thing with C.
What is the most efficient way of doing this?
|
|
|
|
|
kclkl18 wrote: What is the most efficient way of doing this?
You don't show valid C++ code, and you're wondering about efficiency?
Please post on the correct board:
Visual C++/MFC Board[^]
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
kclkl18 wrote:
What is the most efficient way of doing this?
Reading a good C++ tutorial.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Any ideas why the following code runs faster (19 vs 37 msec in the first test) with /clr flag on? No clr is used in the code at all!
#include "stdafx.h"
class myClass
{
};
int main()
{
clock_t start;
const int COUNT = 100000;
myClass* objects[COUNT];
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start));
start = clock();
for (int i = 0; i < COUNT; i++){
objects[i] = new myClass();
}
clock_t start2 = clock();
for (int i = 0; i < COUNT; i++){
delete objects[i];
}
printf("Native without delete: %f\n", (double)(start2 - start));
printf("Deletion: %f\n", (double)(clock() - start2));
printf("Total: %f\n", (double)(clock() - start));
}
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Because managed code ROCKS!
But seriously...
gajatko wrote: 19 vs 37 msec
It's unlikely your tick count has a resolution better that 19ms. That
looks suspiciously like the difference between a single tick.
Maybe try timing something that takes a few seconds or use
the performance counter for timing.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
With
const int COUNT = 10000000;
myClass** objects = new myClass*[COUNT];
(heap because of stackoverflow)
I got 3582 native and 1612 with /clr
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
gajatko wrote: I got 3582 native and 1612 with /clr
Which numbers are you reporting there?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start) / CLOCKS_PER_SEC * 1000);
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|