提交 | 用户 | 时间
|
f7932f
|
1 |
#pragma once |
D |
2 |
#include "pch.h" |
|
3 |
#include "stdafx.h" |
|
4 |
#include "Python.h" |
|
5 |
#include <string> |
|
6 |
#include <cstdarg> |
|
7 |
|
|
8 |
//#include "md5file.h" |
|
9 |
//#pragma warning (disable : 4996) |
|
10 |
//#include <iostream> |
|
11 |
//#include <fstream> |
|
12 |
//#include "shlobj.h" |
|
13 |
//#include "atlstr.h" |
|
14 |
//#include <windows.h> |
|
15 |
//#include "stdio.h" |
|
16 |
// |
|
17 |
//#include "md5global.h" |
|
18 |
//#include "md5.h" |
|
19 |
//#include <fstream> |
|
20 |
//#include <sstream> |
|
21 |
//#include <memory> |
|
22 |
//#include <iomanip> |
|
23 |
//#include <exception> |
|
24 |
|
|
25 |
using namespace std; |
|
26 |
|
|
27 |
#ifdef PYUTILS_EXPORTS |
|
28 |
#define PYUTILS_API __declspec(dllexport) |
|
29 |
#else |
|
30 |
#define PYUTILS_API __declspec(dllimport) |
|
31 |
#endif |
|
32 |
|
|
33 |
extern "C" PYUTILS_API PyObject * create_py_module(string module_name); |
|
34 |
|
|
35 |
|
|
36 |
extern "C" PYUTILS_API int py_initialize(); |
|
37 |
|
|
38 |
extern "C" PYUTILS_API int py_finalize(); |
|
39 |
|
|
40 |
|
|
41 |
extern "C" PYUTILS_API int py_isInitialized(); |
|
42 |
|
|
43 |
|
|
44 |
//template <typename T> T* create_sarr_ptr(int size) { |
|
45 |
// T* result = new T[size]; |
|
46 |
// return result; |
|
47 |
//} |
|
48 |
///* |
|
49 |
//@brief ������άָ�� |
|
50 |
//*/ |
|
51 |
//template <typename T> T** create_darr_ptr(int row_size, int col_size) { |
|
52 |
// T** darray = new T *[row_size]; |
|
53 |
// for (size_t i = 0; i < row_size; i++) { |
|
54 |
// darray[i] = new T[col_size]; |
|
55 |
// } |
|
56 |
// return darray; |
|
57 |
//} |
|
58 |
///* |
|
59 |
//������άָ������c#��cPP�IJ������� |
|
60 |
//*/ |
|
61 |
//template <typename T> T* create_darr2_ptr(int row_size, int col_size) { |
|
62 |
// T* darray2 = new T[row_size*col_size]; |
|
63 |
// return darray2; |
|
64 |
//} |
|
65 |
// |
|
66 |
///* |
|
67 |
//@brief ������ṹ�壬���ֳ�ʼ�����������η����ѿ����ڴ�ռ� |
|
68 |
//*/ |
|
69 |
//template <typename T> struct sarr { |
|
70 |
// T* head; |
|
71 |
// int size; |
|
72 |
// sarr(int size) { |
|
73 |
// this->size = size; |
|
74 |
// this->head = create_sarr_ptr<T>(size); |
|
75 |
// }; |
|
76 |
// sarr() { this->size = 1; this->head = create_sarr_ptr<T>(1); }; |
|
77 |
// sarr(const sarr<T>* data) { |
|
78 |
// T* new_ptr = create_sarr_ptr<T>(data->size); |
|
79 |
// for (int i = 0; i < data->size; i++) { |
|
80 |
// new_ptr[i] = data->head[i]; |
|
81 |
// } |
|
82 |
// this->head = new_ptr; |
|
83 |
// this->size = data->size; |
|
84 |
// }; |
|
85 |
// /* |
|
86 |
// ʹ�õȺŸ�ֵʱ���붼�ǽṹ��ָ�룬��Σ�*sarr1=sarr2�� |
|
87 |
// */ |
|
88 |
// void operator=(const sarr<T>* data) { |
|
89 |
// if (this != data) { |
|
90 |
// if (this->head != NULL) { |
|
91 |
// delete[] this->head; |
|
92 |
// this->head = NULL; |
|
93 |
// } |
|
94 |
// T* new_ptr = create_sarr_ptr<T>(data->size); |
|
95 |
// for (int i = 0; i < data->size; i++) { |
|
96 |
// new_ptr[i] = data->head[i]; |
|
97 |
// } |
|
98 |
// this->head = new_ptr; |
|
99 |
// this->size = data->size; |
|
100 |
// } |
|
101 |
// }; |
|
102 |
// ~sarr() { |
|
103 |
// delete[] head; |
|
104 |
// head = NULL; |
|
105 |
// } |
|
106 |
//}; |
|
107 |
///* |
|
108 |
//��ά����ṹ�壬����һά��ʽ�Ĵ洢��C#��>C++�� |
|
109 |
//*/ |
|
110 |
//template <typename T> struct darr2 { |
|
111 |
// T* head; |
|
112 |
// int row_size; |
|
113 |
// int col_size; |
|
114 |
// darr2(int row, int col) { |
|
115 |
// this->col_size = col; |
|
116 |
// this->row_size = row; |
|
117 |
// this->head = create_darr2_ptr<T>(row, col); |
|
118 |
// }; |
|
119 |
// darr2(const darr2<T>*data) { |
|
120 |
// |
|
121 |
// T* new_ptr = create_darr2_ptr<T>(data->row_size, data->col_size); |
|
122 |
// for (int i = 0; i < data->row_size*data->col_size; i++) |
|
123 |
// { |
|
124 |
// new_ptr[i] = data->head[i]; |
|
125 |
// } |
|
126 |
// this->head = new_ptr; |
|
127 |
// this->row_size = data->row_size; |
|
128 |
// this->col_size = data->col_size; |
|
129 |
// }; |
|
130 |
//}; |
|
131 |
// |
|
132 |
///* |
|
133 |
//@brief ��ά����ṹ�壬���ֳ�ʼ�����������η����ѿ����ڴ�ռ� |
|
134 |
//*/ |
|
135 |
//template <typename T> struct darr { |
|
136 |
// T** head; |
|
137 |
// int row_size; |
|
138 |
// int col_size; |
|
139 |
// darr(int row, int col) { |
|
140 |
// this->col_size = col; |
|
141 |
// this->row_size = row; |
|
142 |
// this->head = create_darr_ptr<T>(row, col); |
|
143 |
// }; |
|
144 |
// darr(const darr<T>* data) { |
|
145 |
// T** new_ptr = create_darr_ptr<T>(data->row_size, data->col_size); |
|
146 |
// for (int i = 0; i < data->row_size; i++) { |
|
147 |
// for (int j = 0; j < data->col_size; j++) { |
|
148 |
// new_ptr[i][j] = data->head[i][j]; |
|
149 |
// } |
|
150 |
// } |
|
151 |
// this->head = new_ptr; |
|
152 |
// this->row_size = data->row_size; |
|
153 |
// this->col_size = data->col_size; |
|
154 |
// }; |
|
155 |
// darr(const darr2<T>* data) { |
|
156 |
// int row = data->row_size; |
|
157 |
// int col = data->col_size; |
|
158 |
// T** new_ptr = create_darr_ptr<T>(row, col); |
|
159 |
// for (size_t i = 0; i < row; i++) |
|
160 |
// { |
|
161 |
// memcpy(new_ptr[i], data->head + i * col, col * sizeof(T)); |
|
162 |
// } |
|
163 |
// this->head = new_ptr; |
|
164 |
// this->row_size = data->row_size; |
|
165 |
// this->col_size = data->col_size; |
|
166 |
// } |
|
167 |
// void operator=(const darr<T>* data) { |
|
168 |
// if (this != data) { |
|
169 |
// |
|
170 |
// if (this->head != NULL) { |
|
171 |
// for (int i = 0; i < this->row_size; i++) { |
|
172 |
// delete[] this->head[i]; |
|
173 |
// } |
|
174 |
// this->head = NULL; |
|
175 |
// } |
|
176 |
// T** new_ptr = create_darr_ptr<T>(data->row_size, data->col_size); |
|
177 |
// for (int i = 0; i < data->row_size; i++) { |
|
178 |
// for (int j = 0; j < data->col_size; j++) { |
|
179 |
// new_ptr[i][j] = data->head[i][j]; |
|
180 |
// } |
|
181 |
// } |
|
182 |
// this->head = new_ptr; |
|
183 |
// this->row_size = data->row_size; |
|
184 |
// this->col_size = data->col_size; |
|
185 |
// } |
|
186 |
// }; |
|
187 |
// darr() { this->col_size = 1; this->row_size = 1; this->head = create_darr_ptr<T>(1, 1); }; |
|
188 |
// ~darr() { |
|
189 |
// for (int i = 0; i < row_size; i++) { |
|
190 |
// delete[] this->head[i]; |
|
191 |
// } |
|
192 |
// this->head = NULL; |
|
193 |
// } |
|
194 |
//}; |
|
195 |
|
|
196 |
/* |
|
197 |
����Ϊȫ���߳���ʵ���������� |
|
198 |
*/ |
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
/* |
|
203 |
����Ϊ����У���벿�� |
|
204 |
*/ |
|
205 |
//int write_string_to_file_append(const string & file_string, const string str) |
|
206 |
//{ |
|
207 |
// std::ofstream OsWrite(file_string, std::ofstream::app); |
|
208 |
// OsWrite << str; |
|
209 |
// OsWrite << std::endl; |
|
210 |
// OsWrite.close(); |
|
211 |
// return 0; |
|
212 |
//} |
|
213 |
//string getCode() { |
|
214 |
// HKEY tempKey; //��� |
|
215 |
// // DWORD dwLength = 256*sizeof(TCHAR); |
|
216 |
// DWORD dwType = REG_SZ; //���ݸ�ʽ |
|
217 |
// TCHAR lpstrReturn[256];//·������ |
|
218 |
// TCHAR lpstrReturn2[256];//·������ |
|
219 |
// DWORD dwLength = 256 * sizeof(TCHAR); //·�������С |
|
220 |
// DWORD dwLength2 = 256 * sizeof(TCHAR); //·�������С |
|
221 |
// //����ע��� |
|
222 |
// RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Cryptography", 0, KEY_READ, &tempKey); |
|
223 |
// RegQueryValueEx(tempKey, _T("MachineGuid"), NULL, &dwType, (LPBYTE)&lpstrReturn, &dwLength); |
|
224 |
// RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\HardWareConfig", 0, KEY_READ, &tempKey); |
|
225 |
// RegQueryValueEx(tempKey, _T("LastConfig"), NULL, &dwType, (LPBYTE)&lpstrReturn2, &dwLength2); |
|
226 |
// string machineGuid(lpstrReturn); |
|
227 |
// string lastConfig(lpstrReturn2); |
|
228 |
// //��ȡ |
|
229 |
// string code1(machineGuid.substr(0, 13)); |
|
230 |
// string code2(lastConfig.substr(14, 6)); |
|
231 |
// string code3(lastConfig.substr(20, 17)); |
|
232 |
// //string code4(machineGuid.substr(0, 4)); |
|
233 |
// string finalCode = code1 + code2 + code3; |
|
234 |
// //finalCode.push_back('}'); |
|
235 |
// //cout << finalCode << endl; |
|
236 |
// //fprintf('C:\\Users\\10951\\Desktop\\code.txt,&finalCode'); |
|
237 |
// return finalCode; |
|
238 |
//} |
|
239 |
//std::string getFileMD5(const std::string& filename) |
|
240 |
//{ |
|
241 |
// std::ifstream fin(filename.c_str(), std::ifstream::in | std::ifstream::binary); |
|
242 |
// if (fin) |
|
243 |
// { |
|
244 |
// MD5_CTX context; |
|
245 |
// MD5Init(&context); |
|
246 |
// |
|
247 |
// fin.seekg(0, fin.end); |
|
248 |
// const auto fileLength = fin.tellg(); |
|
249 |
// fin.seekg(0, fin.beg); |
|
250 |
// |
|
251 |
// const int bufferLen = 8192; |
|
252 |
// std::unique_ptr<unsigned char[]> buffer{ new unsigned char[bufferLen] {} }; |
|
253 |
// unsigned long long totalReadCount = 0; |
|
254 |
// decltype(fin.gcount()) readCount = 0; |
|
255 |
// // ��ȡ�ļ����ݣ�����MD5Update()����MD5ֵ |
|
256 |
// while (fin.read(reinterpret_cast<char*>(buffer.get()), bufferLen)) |
|
257 |
// { |
|
258 |
// readCount = fin.gcount(); |
|
259 |
// totalReadCount += readCount; |
|
260 |
// MD5Update(&context, buffer.get(), static_cast<unsigned int>(readCount)); |
|
261 |
// } |
|
262 |
// // �������һ�ζ��������� |
|
263 |
// readCount = fin.gcount(); |
|
264 |
// if (readCount > 0) |
|
265 |
// { |
|
266 |
// totalReadCount += readCount; |
|
267 |
// MD5Update(&context, buffer.get(), static_cast<unsigned int>(readCount)); |
|
268 |
// } |
|
269 |
// fin.close(); |
|
270 |
// |
|
271 |
// // ���������Լ�� |
|
272 |
// if (totalReadCount != fileLength) |
|
273 |
// { |
|
274 |
// std::ostringstream oss; |
|
275 |
// oss << "FATAL ERROR: read " << filename << " failed!" << std::ends; |
|
276 |
// throw std::runtime_error(oss.str()); |
|
277 |
// } |
|
278 |
// |
|
279 |
// unsigned char digest[16]; |
|
280 |
// MD5Final(digest, &context); |
|
281 |
// |
|
282 |
// // ��ȡMD5 |
|
283 |
// std::ostringstream oss; |
|
284 |
// for (int i = 0; i < 16; ++i) |
|
285 |
// { |
|
286 |
// oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(digest[i]); |
|
287 |
// } |
|
288 |
// oss << std::ends; |
|
289 |
// |
|
290 |
// return std::move(oss.str()); |
|
291 |
// } |
|
292 |
// else |
|
293 |
// { |
|
294 |
// std::ostringstream oss; |
|
295 |
// oss << "FATAL ERROR: " << filename << " can't be opened" << std::ends; |
|
296 |
// throw std::runtime_error(oss.str()); |
|
297 |
// } |
|
298 |
//} |
|
299 |
//inline int checkCode() { |
|
300 |
// TCHAR path[256]; |
|
301 |
// //��ȡ�û������ַ |
|
302 |
// SHGetSpecialFolderPath(0, _T(path), CSIDL_DESKTOPDIRECTORY, 0); |
|
303 |
// string folderPath(path); |
|
304 |
// string tempFolderPath1 = folderPath + "\\code_temp.txt"; |
|
305 |
// string code = getCode(); |
|
306 |
// write_string_to_file_append(tempFolderPath1, string(code)); |
|
307 |
// string mD5Code = getFileMD5(tempFolderPath1); |
|
308 |
// write_string_to_file_append(string(folderPath + "\\code_check.txt"), string(mD5Code));//ɾ����仰 |
|
309 |
// remove(data(tempFolderPath1)); |
|
310 |
// |
|
311 |
// //��txt��ȡ�ַ��� |
|
312 |
// string s; //ÿ�ζ�ȡһ��txt�ļ����ص��ַ��� |
|
313 |
// //��ȡһtxt�ļ� |
|
314 |
// ifstream infile; |
|
315 |
// infile.open(string(folderPath + "\\code_check.txt")); |
|
316 |
// while (getline(infile, s)) |
|
317 |
// { |
|
318 |
// //�� |
|
319 |
// //cout << s << endl; |
|
320 |
// break; |
|
321 |
// } |
|
322 |
// int resultEnv = s.compare(mD5Code); |
|
323 |
// cout << resultEnv << endl; |
|
324 |
// infile.close(); //�ر��ļ� |
|
325 |
// return resultEnv; |
|
326 |
//} |
|
327 |
|
|
328 |
|
|
329 |
/* |
|
330 |
@brief ���ݴ����cpp����ת��python���� |
|
331 |
@param data��struct sarr/darr |
|
332 |
*/ |
|
333 |
//inline PyObject* parseCppData(int data) { return Py_BuildValue("i", data); } |
|
334 |
//inline PyObject* parseCppData(float data) { return Py_BuildValue("f", data); } |
|
335 |
//inline PyObject* parseCppData(double data) { return Py_BuildValue("d", data); } |
|
336 |
//inline PyObject* parseCppData(string data) { return Py_BuildValue("s", data.c_str()); } |
|
337 |
//inline PyObject* parseCppData(char* data) { string s(data); return Py_BuildValue("s", s.c_str()); } |
|
338 |
//inline PyObject* parseCppData(const char* data) { return Py_BuildValue("s", data); } |
|
339 |
//template <typename T> PyObject* parseCppData(T* data, int size) { |
|
340 |
// PyObject* pylist = PyList_New(size); |
|
341 |
// PyObject* pylist_i = NULL; |
|
342 |
// for (int i = 0; i < size; i++) { |
|
343 |
// pylist_i = parseCppData(*(data + i)); |
|
344 |
// PyList_SetItem(pylist, i, pylist_i); |
|
345 |
// } |
|
346 |
// return pylist; |
|
347 |
//} |
|
348 |
//template <typename T> PyObject* parseCppData(T** data, int row, int col) { |
|
349 |
// PyObject* pylist = PyList_New(row); |
|
350 |
// PyObject* pylist_i = NULL; |
|
351 |
// for (int i = 0; i < row; i++) { |
|
352 |
// pylist_i = parseCppData(*(data + i), col); |
|
353 |
// PyList_SetItem(pylist, i, pylist_i); |
|
354 |
// } |
|
355 |
// return pylist; |
|
356 |
//} |
|
357 |
// |
|
358 |
//template <typename T> PyObject* parseCppData(sarr<T>* data) { |
|
359 |
// return parseCppData(data->head, data->size); |
|
360 |
//} |
|
361 |
//template <typename T> PyObject* parseCppData(darr<T>* data) { |
|
362 |
// return parseCppData(data->head, data->row_size, data->col_size); |
|
363 |
//} |
|
364 |
|
|
365 |
|
|
366 |
/* |
|
367 |
@brief ���ݴ����cpp����ת��python���� |
|
368 |
@param data��struct sarr/darr |
|
369 |
@param pydata: PyList |
|
370 |
*/ |
|
371 |
//inline void parsePyData(int& res, PyObject* pydata) { PyArg_Parse(pydata, "i", &res); } |
|
372 |
//inline void parsePyData(float& res, PyObject* pydata) { PyArg_Parse(pydata, "f", &res); } |
|
373 |
//inline void parsePyData(double& res, PyObject* pydata) { float f = 0.0; PyArg_Parse(pydata, "f", &f); res = (double)f; } |
|
374 |
//inline void parsePyData(string& res, PyObject* pydata) { char* s; PyArg_Parse(pydata, "s", &s); res = s; } |
|
375 |
// |
|
376 |
//template <typename T> void parsePyData(T* data, PyObject* pydata, int size) { |
|
377 |
// |
|
378 |
// for (int i = 0; i < size; i++) { |
|
379 |
// parsePyData(*(data + i), PyList_GetItem(pydata, i)); |
|
380 |
// } |
|
381 |
//} |
|
382 |
//template <typename T> void parsePyData(T** data, PyObject* pydata, int row_size, int col_size) { |
|
383 |
// for (int i = 0; i < row_size; i++) { |
|
384 |
// parsePyData(*(data + i), PyList_GetItem(pydata, i), col_size); |
|
385 |
// } |
|
386 |
//} |
|
387 |
//template <typename T> void parsePyData(sarr<T>* data, PyObject* pydata) { |
|
388 |
// parsePyData(data->head, pydata, data->size); |
|
389 |
//} |
|
390 |
//template <typename T> void parsePyData(darr<T>* data, PyObject* pydata) { |
|
391 |
// parsePyData(data->head, pydata, data->row_size, data->col_size); |
|
392 |
//} |
|
393 |
// |
|
394 |
// |
|
395 |
///* |
|
396 |
//@brief ���ݴ����cpp����ת��python���� |
|
397 |
//@param data��struct sarr/darr |
|
398 |
//@param pytuple: PyTuple |
|
399 |
//@param index: tuple�±� |
|
400 |
//*/ |
|
401 |
//template<typename T>void parsePyTuple(T& data, PyObject* pytuple, int index) { |
|
402 |
// parsePyData(data, PyTuple_GetItem(pytuple, index)); |
|
403 |
//} |
|
404 |
// |
|
405 |
//template<typename T>void parsePyTuple(sarr<T>* data, PyObject* pytuple, int index) { |
|
406 |
// parsePyData(data, PyTuple_GetItem(pytuple, index)); |
|
407 |
//} |
|
408 |
//template<typename T>void parsePyTuple(darr<T>* data, PyObject* pytuple, int index) { |
|
409 |
// parsePyData(data, PyTuple_GetItem(pytuple, index)); |
|
410 |
//} |
|
411 |
// |
|
412 |
///* |
|
413 |
//��ά����洢��ʽת�䣬darrת��darr2������jni����java���յĶ�λ����ת����C++���յ�darr2 |
|
414 |
//*/ |
|
415 |
//template <typename T> darr2<T>* convert_darr2_from_darr(darr<T>* data) { |
|
416 |
// int row = data->row_size; |
|
417 |
// int col = data->col_size; |
|
418 |
// T* new_ptr = create_darr2_ptr<T>(row, col); |
|
419 |
// darr2<T>* result = new darr2<T>(row, col); |
|
420 |
// for (size_t i = 0; i < row; i++) |
|
421 |
// { |
|
422 |
// memcpy(new_ptr + i * col, data->head[i], col * sizeof(double)); |
|
423 |
// } |
|
424 |
// result->head = new_ptr; |
|
425 |
// result->row_size = row; |
|
426 |
// result->col_size = col; |
|
427 |
// |
|
428 |
// return result; |
|
429 |
//} |
|
430 |
///* |
|
431 |
//int��darr2 |
|
432 |
//*/ |
|
433 |
//template <typename T> darr2<T>* convert_darr2_from_darr_int(darr<T>* data) { |
|
434 |
// int row = data->row_size; |
|
435 |
// int col = data->col_size; |
|
436 |
// T* new_ptr = create_darr2_ptr<T>(row, col); |
|
437 |
// darr2<T>* result = new darr2<T>(row, col); |
|
438 |
// for (size_t i = 0; i < row; i++) |
|
439 |
// { |
|
440 |
// memcpy(new_ptr + i * col, data->head[i], col * sizeof(int)); |
|
441 |
// } |
|
442 |
// result->head = new_ptr; |
|
443 |
// result->row_size = row; |
|
444 |
// result->col_size = col; |
|
445 |
// |
|
446 |
// return result; |
|
447 |
//} |
|
448 |
///* |
|
449 |
//sarrת��Ϊdarr2,����jni����java���յ�һά����ת����C++���յ�darr2 |
|
450 |
//*/ |
|
451 |
//template <typename T> darr2<T>* convert_darr2_from_sarr(sarr<T>* data) { |
|
452 |
// int row = data->size; |
|
453 |
// int col = 1; |
|
454 |
// T* new_ptr = create_darr2_ptr<T>(row, col); |
|
455 |
// darr2<T>* result = new darr2<T>(row, col); |
|
456 |
// for (size_t i = 0; i < row; i++) |
|
457 |
// { |
|
458 |
// memcpy(new_ptr + i * col, data->head + i*col, col * sizeof(double)); |
|
459 |
// } |
|
460 |
// result->head = new_ptr; |
|
461 |
// result->row_size = row; |
|
462 |
// result->col_size = col; |
|
463 |
// |
|
464 |
// return result; |
|
465 |
//} |