gcnew class objects?

Nov 4, 2013 at 12:15pm
Hello,

I am relatively new to object in C++. Specially, the ones using gcnew.
I am using cpp to call some dll from C#.
My code works perfectly, but I didn't notice that I was using static 'objects' in some GUI I created from C#.
Now, I need to use independent object for each instance of my objects.
The first function I need to call is DownloadStringCPP, so I need to create the instance of Calendar there.
Please find the code:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// CppStdcallInerfaceWrapper.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#include <string>

#ifdef _MANAGED
#pragma managed(push, off)
#endif
 
#ifdef _MANAGED
#pragma managed(pop)
#endif
 
#using "Calendar.dll"
using namespace Calendar_Assembly;
using namespace System::Runtime::InteropServices;

//interior_ptr< Calendar > var;
/*
ref class Calendar_ref
{
public:
	Calendar_Assembly::Calendar ccc;
	
};*/

//Calendar_Assembly::Calendar ccc%;

/*
void Calendar::Calendar() {
	// Defined in .NET Assembly "System".
	Calendar ^ ccc = gcnew Calendar::Calendar();
}

ref struct Calendar {
	System::String^ sender;
	System::String^ receiver;
	System::String^ data;
};
*/


//Calendar^ cal = nullptr; // gcnew Calendar();
/*ref class ManagedGlobals {
public:
	Calendar^ cal = nullptr;

	//static Calendar_Assembly::Calendar^ cal = gcnew Calendar();
};*/

//ref class Calendar{
	//Calendar_Assembly:Calendar^ cal=gcnew Calendar();
	//System::String^ receiver;
	//System::String^ data;

	/*public ref class MyClass {
		public:
			
			Calendar calendar ;
	};
	*/


__declspec(dllexport) char* __stdcall getMatrixCPP()
{
	System::String ^ijth;
	ijth = cal->getMatrix();
	char *mat= new char[sizeof(ijth) + 1];
	mat = (char*)(void*)Marshal::StringToHGlobalAnsi(ijth);
	return mat;
}

__declspec(dllexport) char* __stdcall get_ord_execCPP()
{
	System::String ^str;
	str = cal->get_ord_exec();
	char *ord_exe= new char[sizeof(str) + 1];
	ord_exe = (char*)(void*)Marshal::StringToHGlobalAnsi(str);	
	return ord_exe;
}

__declspec(dllexport) int __stdcall getRowsCPP()    
{
	int r = cal->getRows();
	return r;
}

__declspec(dllexport) int __stdcall CreateGUICPP()
{
	int ret = cal->CreateGUI();
	return ret;
}

__declspec(dllexport) int __stdcall get_CDATACPP()
{
	int ret = cal->get_CDATA();
	return ret;
}


//Calendar^ cal=nullptr;
/*
__declspec(dllexport) void* __stdcall CalendarCPP() {
	Calendar* cal= gcnew Calendar_Assembly::Calendar();
	return cal;
}*/


public ref class MyClass {
public:

	Calendar calendar;
};



__declspec(dllexport) int __stdcall DownloadStringCPP()    
{
//	MyManagedTestClass^ clazz = gcnew MyManagedTestClass();
//	clazz->cal->DownloadString();

//	Calendar_ref^ a = gcnew Calendar_ref;
	//	a->ccc= gcnew Calendar_Assembly::Calendar();
	
	//MyClass^ Myclass = gcnew MyClass;
	//MyClass::Calendar_OCO calendar;
	Calendar^ calendar = gcnew Calendar_Assembly::Calendar();
	//var =calendar;

	//Calednarccc=  a;

	int ret = calendar->DownloadString();
	return ret;
}

__declspec(dllexport) char* __stdcall returnValsCPP()
{
	System::String ^rws_cols;
	rws_cols = cal->returnVals();
	if (rws_cols != "")
	{
		char *ret = new char[sizeof(rws_cols)+1];
		ret = (char*)(void*)Marshal::StringToHGlobalAnsi(rws_cols);
		return ret;
	}
	else
		return "";

}





It's a C++ wrapper.
Help.
Last edited on Nov 4, 2013 at 12:18pm
Nov 4, 2013 at 3:25pm
Is you question whether using gcnew with classes of not?

Well the so called ref classes can (must?) be created with gcnew. Read this:

http://msdn.microsoft.com/en-us/library/windows/apps/hh699870.aspx

http://msdn.microsoft.com/de-de/library/te3ecsc8%28v=vs.90%29.aspx

you can allso use ref new:
msdn wrote:
The handle-to-object operator ^ is known as a "hat" and is fundamentally a C++ smart pointer. The memory it points to is automatically destroyed when the last hat goes out of scope or is explicitly set to nullptr.
Topic archived. No new replies allowed.