Files
PowerToys/PyWinAlfred.Test/Source.cpp

125 lines
2.9 KiB
C++
Raw Normal View History

2013-12-29 21:50:56 +08:00
#include <tchar.h>
#include <stdio.h>
#include "Python.h"
#include <thread>
#include <future>
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;
2014-01-09 22:16:19 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>ǰִ<C7B0>У<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ͷ<EFBFBD>PyEval_InitThreads<64><73><EFBFBD>õ<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳̿<DFB3><CCBF><EFBFBD><EFBFBD>޷<EFBFBD><DEB7><EFBFBD>ȡ<EFBFBD><C8A1>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
PyEval_ReleaseLock();
2014-01-03 19:29:46 +08:00
PyGILState_STATE gstate = PyGILState_Ensure();
2013-12-29 21:50:56 +08:00
2014-01-03 19:29:46 +08:00
// Initialise the Python interpreter
2013-12-29 21:50:56 +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);
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);
//PyEval_SaveThread();
// Finish the Python Interpreter
//PyErr_Clear();
2014-01-03 19:29:46 +08:00
printf("My thread is finishing... %s \n",para);
2013-12-29 21:50:56 +08:00
PyGILState_Release(gstate);
return str_ret;
}
int main(int argc, char *argv[])
{
2014-01-09 22:16:19 +08:00
char* directory = "d:\\github\\WinAlfred\\Plugins\\WinAlfred.Plugin.DouBan\\";
2013-12-29 21:50:56 +08:00
char* file = "main";
char* method = "query";
2014-01-09 22:16:19 +08:00
char* para1 = "movie 1";
char* para2 = "movie 2";
char* para3 = "movie 3";
char* para4 = "movie 4";
2014-01-03 19:29:46 +08:00
int i = 0;
// <20><>ʼ<EFBFBD><CABC>
2013-12-29 21:50:56 +08:00
Py_Initialize();
2014-01-03 19:29:46 +08:00
// <20><>ʼ<EFBFBD><CABC><EFBFBD>߳<EFBFBD>֧<EFBFBD><D6A7>
2013-12-29 21:50:56 +08:00
PyEval_InitThreads();
2014-01-09 22:16:19 +08:00
2014-01-03 19:29:46 +08:00
//std::async(Exec,directory,file,method,para);
std::async(Exec,directory,file,method,para1);
std::async(Exec,directory,file,method,para2);
std::async(Exec,directory,file,method,para3);
std::async(Exec,directory,file,method,para4);
// <20><>֤<EFBFBD><D6A4><EFBFBD>̵߳<DFB3><CCB5>ö<EFBFBD><C3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//PyGILState_Ensure();
2013-12-29 21:50:56 +08:00
getchar();
2014-01-03 19:29:46 +08:00
Py_Finalize();
2013-12-29 21:50:56 +08:00
return 0;
}