PowerToys/PyWinAlfred.Test/Source.cpp
2014-01-03 19:29:46 +08:00

124 lines
2.9 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 <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;
PyGILState_STATE gstate = PyGILState_Ensure();
// Initialise the Python interpreter
// 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();
printf("My thread is finishing... %s \n",para);
PyGILState_Release(gstate);
return str_ret;
}
int main(int argc, char *argv[])
{
char* directory = "d:\\github\\WinAlfred\\WinAlfred\\bin\\Debug\\Plugins\\p";
char* file = "main";
char* method = "query";
char* para1 = "p 1";
char* para2 = "p 2";
char* para3 = "p 3";
char* para4 = "p 4";
int i = 0;
// <20><>ʼ<EFBFBD><CABC>
Py_Initialize();
// <20><>ʼ<EFBFBD><CABC><EFBFBD>߳<EFBFBD>֧<EFBFBD><D6A7>
PyEval_InitThreads();
PyEval_ReleaseLock();
// <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>
//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();
getchar();
Py_Finalize();
return 0;
}