Files
PowerToys/PyWinAlfred/Main.cpp
qianlifeng 935d26f956 research
2014-01-09 22:16:19 +08:00

103 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <tchar.h>
#include "Python.h"
#include <thread>
#include <future>
int i = 0;
extern "C" __declspec(dllexport) void InitPythonEnv()
{
Py_Initialize();
PyEval_InitThreads();
// 启动子线程前执行为了释放PyEval_InitThreads获得的全局锁否则子线程可能无法获取到全局锁。
}
char* GetErrorMessage()
{
char *pStrErrorMessage = NULL;
if(PyErr_Occurred()){
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
pStrErrorMessage = PyString_AsString(pvalue);
}
return pStrErrorMessage;
}
char* Exec(char* directory, char* file, char* method, char* para)
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pClass, *pInstance;
char *error;
PyEval_ReleaseLock();
PyGILState_STATE gstate = PyGILState_Ensure();
// Build the name object
PyObject *path = PySys_GetObject("path");
PyList_Append(path, PyString_FromString(directory));
pName = PyString_FromString(file);
error = GetErrorMessage();
if(error != NULL){
char* err =new char[5000]();
sprintf(error, "%s:%s","PYTHONERROR",error);
return err;
}
pModule = PyImport_Import(pName);
error = GetErrorMessage();
if(error != NULL){
char* err =new char[5000]();
sprintf(err, "%s:%s","PYTHONERROR",error);
return err;
}
pDict = PyModule_GetDict(pModule);
error = GetErrorMessage();
if(error != NULL){
char* err =new char[5000]();
sprintf(err, "%s:%s","PYTHONERROR",error);
return err;
}
pClass = PyDict_GetItemString(pDict,"PyWinAlfred");
error = GetErrorMessage();
if(error != NULL){
char* err =new char[5000]();
sprintf(err, "%s:%s","PYTHONERROR",error);
return err;
}
pInstance = PyObject_CallObject(pClass, NULL);
error = GetErrorMessage();
if(error != NULL){
char* err =new char[5000]();
sprintf(err, "%s:%s","PYTHONERROR",error);
return err;
}
// Call a method of the class with two parameters
pValue = PyObject_CallMethod(pInstance,method, "(s)",para);
error = GetErrorMessage();
if(error != NULL){
char* err =new char[5000]();
sprintf(err, "%s:%s","PYTHONERROR",error);
return err;
}
char * str_ret = PyString_AsString(pValue);
//PyErr_Clear();
PyGILState_Release(gstate);
return str_ret;
}
extern "C" __declspec(dllexport) char* ExecPython(char* directory, char* file, char* method, char* para)
{
//PyEval_ReleaseLock();
char* s = Exec(directory,file,method,para);
return s;
//auto future = std::async(Exec,directory,file,method,para);
//return future.get();
}