-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLauncher.cpp
139 lines (121 loc) · 3.14 KB
/
Launcher.cpp
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
#include <iostream>
#include <windows.h>
#include <Python.h>
//g++ -o MusyncSaveDecode/Launcher Launcher.cpp -I"C:\Python3x\include" -L"C:\Python3x\libs" -lpython3x
int main() {
// 加载 python3.x.dll
HMODULE hModule = LoadLibrary("./python311.dll"); // 替换为实际的版本号
if (!hModule) {
fprintf(stderr, "Failed to load python3.x.dll\n");
return 1;
}
// 加载模块
Py_Initialize();
if (!Py_IsInitialized()) {
fprintf(stderr, "Failed to initialize Python interpreter\n");
return 1;
}
PyObject* pModule, * pFunc, * pArgs, * pValue;
// 获取函数对象
pFunc = PyObject_GetAttrString(pModule, "Launcher");
if (pFunc == NULL) {
PyErr_Print();
fprintf(stderr, "Failed to get function 'Launcher()'\n");
Py_DECREF(pModule);
Py_Finalize();
return 1;
}
//// 创建参数
//pArgs = PyTuple_Pack(2, PyLong_FromLong(5), PyLong_FromLong(3));
//if (pArgs == NULL) {
// PyErr_Print();
// fprintf(stderr, "Failed to create arguments\n");
// Py_DECREF(pFunc);
// Py_DECREF(pModule);
// Py_Finalize();
// return 1;
//}
// 调用函数
pValue = PyObject_CallObject(pFunc, nullptr/*pArgs*/);
//Py_DECREF(pArgs);
if (pValue == NULL) {
PyErr_Print();
fprintf(stderr, "Call failed\n");
Py_DECREF(pFunc);
Py_DECREF(pModule);
Py_Finalize();
return 1;
}
// 检查返回值是否为 None
if (pValue == Py_None) {
printf("Result of call: None\n");
}
else {
// 打印结果
printf("Result of call: %ld\n", PyLong_AsLong(pValue));
}
Py_DECREF(pValue);
Py_DECREF(pFunc);
Py_DECREF(pModule);
Py_Finalize();
system("pause");
return 0;
//PyObject* pModule = PyImport_ImportModule("Launcher");
//if (!pModule) {
// PyErr_Print();
// fprintf(stderr, "Failed to load module 'Launcher'\n");
// Py_Finalize();
// return 1;
//}
//PyObject* pClass = PyObject_GetAttrString(pModule, "MyClass");
//if (!pClass) {
// PyErr_Print();
// fprintf(stderr, "Failed to get class 'MyClass'\n");
// Py_DECREF(pModule);
// Py_Finalize();
// return 1;
//}
//PyObject* pArgs = PyTuple_Pack(1, PyLong_FromLong(10)); // 传递初始化参数
//PyObject* pInstance = PyObject_CallObject(pClass, pArgs);
//Py_DECREF(pArgs);
//if (!pInstance) {
// PyErr_Print();
// fprintf(stderr, "Failed to create instance of 'MyClass'\n");
// Py_DECREF(pClass);
// Py_DECREF(pModule);
// Py_Finalize();
// return 1;
//}
//PyObject* pMethod = PyObject_GetAttrString(pInstance, "add");
//if (!pMethod) {
// PyErr_Print();
// fprintf(stderr, "Failed to get method 'add'\n");
// Py_DECREF(pInstance);
// Py_DECREF(pClass);
// Py_DECREF(pModule);
// Py_Finalize();
// return 1;
//}
//PyObject* pMethodArgs = PyTuple_Pack(1, PyLong_FromLong(5)); // 传递方法参数
//PyObject* pValue = PyObject_CallObject(pMethod, pMethodArgs);
//Py_DECREF(pMethodArgs);
//if (!pValue) {
// PyErr_Print();
// fprintf(stderr, "Call to method 'add' failed\n");
// Py_DECREF(pMethod);
// Py_DECREF(pInstance);
// Py_DECREF(pClass);
// Py_DECREF(pModule);
// Py_Finalize();
// return 1;
//}
//// 打印结果
//printf("Result of call: %ld\n", PyLong_AsLong(pValue));
//Py_DECREF(pValue);
//Py_DECREF(pMethod);
//Py_DECREF(pInstance);
//Py_DECREF(pClass);
//Py_DECREF(pModule);
//Py_Finalize();
//return 0;
}