using nonmangaed classes in a CLR application

here goes my first post in CPP.com. im kinda new to windows programming(but not C++) and this was one solution i found to a problem i was having.

as part of a school project i am creating a text-based game, and im doing it as a CLR application. before i started build the interface i built 3 standard c++ classes for weapons and other game items. once i got the main form built and mostly implemented i compiled it and... "you've got fail!!!"

apparently i couldnt use my standard classes in the project because it is an unmanaged class and CLR was using managed code(grrr Microsoft). after doing some Google i found an initial solution:rebuild all 3 of my classes using managed code which i really didnt want to do cause total line count between them is 1500 lines

after more googling i found a solution that didnt involve me doing that.
i had to dynamically allocate memory for the class so CLR doesnt throw it out


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// main.cpp
#include "stdafx.h"
#include "frmMain.h"
#include "robot.h"
using namespace GUItest;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	//dynamically allcates room for unmaaged class
	 robot * rbt = new rbt;
	


        // Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	

        // initalize startup form with unmanaged class 

	Application::Run(gcnew frmMain( rbt));
	return 0;
}


once that was done i modified my constructor/destructor to prevent leaks. then i ran into another problem all of my event handlers were asking for a managed type for their properties that were being modified by the unmanaged classes. so back to google and some time later i found that the windows library had what looked to me like datatype conversion function which so far look like they convert(among other things) unmangaed types to managed types
they can be found in
System::Convert
and heres how i am using them in my event handlers
1
2
//convert an unmanaged string from <string> to a managed string
txtName->Text = System::Convert::ToString( rbt->get_Name());


at time of writing im still looking for errors that might appear from doing this since i dont fully understand what the system::convert functions are doing and how it will affect the use of my unmanaged classes
hope this helps some people
Topic archived. No new replies allowed.