Files
PowerToys/PyWinAlfred/Main.cpp

69 lines
1.6 KiB
C++
Raw Normal View History

2013-12-23 19:21:51 +08:00
#include <tchar.h>
#include "Python.h"
extern "C" __declspec(dllexport) void InitPythonEnv()
{
}
2013-12-26 20:18:08 +08:00
extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* method, char* para)
2013-12-23 19:21:51 +08:00
{
try{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance;
2013-12-23 19:21:51 +08:00
// Initialise the Python interpreter
2013-12-23 19:21:51 +08:00
Py_Initialize();
// Create GIL/enable threads
2013-12-26 20:18:08 +08:00
//PyEval_InitThreads();
2013-12-26 20:18:08 +08:00
//PyGILState_STATE gstate = PyGILState_Ensure();
// // Get the default thread state
// PyThreadState* state = PyThreadState_Get();
// // Once in each thread
//PyThreadState* stateForNewThread = PyThreadState_New(state->interp);
//PyEval_RestoreThread(stateForNewThread);
2013-12-23 19:21:51 +08:00
// Build the name object
PyObject *sys = PyImport_ImportModule("sys");
PyObject *path = PyObject_GetAttrString(sys, "path");
PyList_Append(path, PyString_FromString(directory));
pName = PyString_FromString(file);
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pClass = PyDict_GetItemString(pDict,"PyWinAlfred");
if (PyCallable_Check(pClass))
{
pInstance = PyObject_CallObject(pClass, NULL);
}
else
{
PyErr_Print();
2013-12-23 23:53:38 +08:00
return "failed";
2013-12-23 19:21:51 +08:00
}
// Call a method of the class with two parameters
2013-12-26 20:18:08 +08:00
pValue = PyObject_CallMethod(pInstance,method, "(s)",para);
char * str_ret = PyString_AsString(pValue);
2013-12-26 20:18:08 +08:00
//PyGILState_Release(gstate);
//PyEval_SaveThread();
2013-12-25 00:21:54 +08:00
2013-12-23 19:21:51 +08:00
// Finish the Python Interpreter
//Py_Finalize();
2013-12-23 23:53:38 +08:00
return str_ret;
2013-12-23 19:21:51 +08:00
}
catch(int& value){
PyErr_Print();
}
}