-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
149 lines (114 loc) · 2.72 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
#include <string>
#include <vector>
#include <list>
#include <windows.h>
#include <shellapi.h>
#if defined(_DEBUG)
#undef _DEBUG
#include "python.h"
#define _DEBUG
#else
#include "python.h"
#endif
#define PYTHON_INSTALL_PATH L"c:\\Python38-64"
//--------------------------------------
int AppMain()
{
std::wstring exe_dir;
// Get exe's directory
{
wchar_t exe_path_buf[MAX_PATH + 1];
GetModuleFileName(NULL, exe_path_buf, MAX_PATH);
exe_dir = exe_path_buf;
size_t last_backslash_pos = exe_dir.find_last_of(L"\\/");
if (last_backslash_pos >= 0)
{
exe_dir = exe_dir.substr(0, last_backslash_pos);
}
else
{
exe_dir = L"";
}
}
// Setup environment variable "PATH"
{
std::wstring env_path;
wchar_t tmp_buf[1];
DWORD ret = GetEnvironmentVariable(L"PATH", tmp_buf, 0);
if (ret > 0)
{
DWORD len = ret;
wchar_t * buf = (wchar_t*)malloc((len + 1) * sizeof(wchar_t));
GetEnvironmentVariable(L"PATH", buf, (len + 1) * sizeof(wchar_t));
env_path = buf;
free(buf);
}
env_path = exe_dir + L"/lib;" + env_path;
SetEnvironmentVariable(L"PATH", env_path.c_str());
}
// Python home
{
#if defined(_DEBUG)
Py_SetPythonHome(PYTHON_INSTALL_PATH);
#else
Py_SetPythonHome(const_cast<wchar_t*>(exe_dir.c_str()));
#endif //_DEBUG
}
// Python module search path
{
std::wstring python_path;
python_path += exe_dir + L"/extension;";
#if defined(_DEBUG)
python_path += exe_dir + L";";
python_path += exe_dir + L"/..;";
python_path += std::wstring(PYTHON_INSTALL_PATH) + L"\\Lib;";
python_path += std::wstring(PYTHON_INSTALL_PATH) + L"\\Lib\\site-packages;";
python_path += std::wstring(PYTHON_INSTALL_PATH) + L"\\DLLs;";
#else
python_path += exe_dir + L"/library.zip;";
python_path += exe_dir + L"/lib;";
#endif
Py_SetPath(python_path.c_str());
}
// Initialization
Py_Initialize();
// Setup sys.argv
{
wchar_t * cmdline = GetCommandLine();
int argc;
wchar_t ** argv = CommandLineToArgvW(cmdline, &argc);
PySys_SetArgv(argc, argv);
LocalFree(argv);
}
// enable DPI handling
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
// Execute python side main script
{
PyObject * module = PyImport_ImportModule("cfiler_main");
if (module == NULL)
{
PyErr_Print();
}
Py_XDECREF(module);
module = NULL;
}
// Termination
Py_Finalize();
return 0;
}
#if ! defined(_DEBUG)
int WINAPI WinMain(
HINSTANCE hInstance, /* handle to current instance */
HINSTANCE hPrevInstance, /* handle to previous instance */
LPSTR lpCmdLine, /* pointer to command line */
int nCmdShow /* show state of window */
)
{
return AppMain();
}
#else
int main(int argc, const char * argv[])
{
return AppMain();
}
#endif