-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
183 lines (160 loc) · 5.05 KB
/
main.cpp
File metadata and controls
183 lines (160 loc) · 5.05 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <windows.h>
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <cstdlib>
#include <string>
#include <iostream>
#pragma comment(lib, "mono-2.0.lib")
extern "C" {
__declspec(dllexport)
void SayMyaw() {
std::cout << "Myaw!! (C++)" << std::endl;
}
}
int main(int argc, char* argv[])
{
#pragma region Load and compile the script
std::string scriptPath(R"(Dog.cs Cat.cs)");
std::string command = "\"" MONO_HOME "/bin/mcs\" " + scriptPath + " -target:library";
//Compile the script
system(command.c_str());
#pragma endregion
//#pragma region Init mono runtime
mono_set_dirs(MONO_HOME "/lib",
MONO_HOME "/etc");
//Init a domain
MonoDomain *domain;
domain = mono_jit_init("MonoScriptTry");
if (!domain)
{
std::cout << "mono_jit_init failed" << std::endl;
// system("pause");
return 1;
}
//Open a assembly in the domain
MonoAssembly *assembly;
char* assemblyPath = "Dog.dll";
assembly = mono_domain_assembly_open(domain, assemblyPath);
if (!assembly)
{
std::cout << "mono_domain_assembly_open failed" << std::endl;
// system("pause");
return 1;
}
//Get a image from the assembly
MonoImage* image;
image = mono_assembly_get_image(assembly);
if (!image)
{
std::cout << "mono_assembly_get_image failed" << std::endl;
// system("pause");
return 1;
}
#pragma endregion
#pragma region Run a static method
{
//Build a method description object
MonoMethodDesc* TypeMethodDesc;
char* TypeMethodDescStr = "Dog:Type()";
TypeMethodDesc = mono_method_desc_new(TypeMethodDescStr, NULL);
if (!TypeMethodDesc)
{
std::cout << "mono_method_desc_new failed" << std::endl;
// system("pause");
return 1;
}
//Search the method in the image
MonoMethod* method;
method = mono_method_desc_search_in_image(TypeMethodDesc, image);
if (!method)
{
std::cout << "mono_method_desc_search_in_image failed" << std::endl;
// system("pause");
return 1;
}
//run the method
std::cout << "Running the static method: " << TypeMethodDescStr << std::endl;
mono_runtime_invoke(method, nullptr, nullptr, nullptr);
}
#pragma endregion
#pragma region Run a normal method
{
//Get the class
MonoClass* dogclass;
dogclass = mono_class_from_name(image, "", "Dog");
if (!dogclass)
{
std::cout << "mono_class_from_name failed" << std::endl;
// system("pause");
return 1;
}
//Create a instance of the class
MonoObject* dogA;
dogA = mono_object_new(domain, dogclass);
if (!dogclass)
{
std::cout << "mono_object_new failed" << std::endl;
// system("pause");
return 1;
}
//Call its default constructor
mono_runtime_object_init(dogA);
//Build a method description object
MonoObject* result;
MonoMethodDesc* BarkMethodDesc;
char* BarkMethodDescStr = "Dog:Bark(int)";
BarkMethodDesc = mono_method_desc_new(BarkMethodDescStr, NULL);
if (!BarkMethodDesc)
{
std::cout << "mono_method_desc_new failed" << std::endl;
// system("pause");
return 1;
}
//Search the method in the image
MonoMethod* method;
method = mono_method_desc_search_in_image(BarkMethodDesc, image);
if (!method)
{
std::cout << "mono_method_desc_search_in_image failed" << std::endl;
// system("pause");
return 1;
}
//Set the arguments for the method
void* args[1];
int barkTimes = 3;
args[0] = &barkTimes;
//Run the method
std::cout << "Running the method: " << BarkMethodDescStr << std::endl;
mono_runtime_invoke(method, dogA, args, nullptr);
}
#pragma endregion
#pragma region Run a static method delegated back to native
{
//Build a method description object
MonoMethodDesc* TypeMethodDesc;
char* TypeMethodDescStr = "Cat:SaySomething()";
TypeMethodDesc = mono_method_desc_new(TypeMethodDescStr, NULL);
if (!TypeMethodDesc)
{
std::cout << "mono_method_desc_new failed" << std::endl;
// system("pause");
return 1;
}
//Search the method in the image
MonoMethod* method;
method = mono_method_desc_search_in_image(TypeMethodDesc, image);
if (!method)
{
std::cout << "mono_method_desc_search_in_image failed" << std::endl;
// system("pause");
return 1;
}
//run the method
std::cout << "Running the static method: " << TypeMethodDescStr << std::endl;
mono_runtime_invoke(method, nullptr, nullptr, nullptr);
}
#pragma endregion
// system("pause");
return 0;
}