调用python步骤参考:https://blog.csdn.net/nanguabing007/article/details/89394541
#include <conio.h>
#include <iostream>
#include <ctime>
#include <Python.h>
using namespace std;
int gettime()
{
Py_SetPythonHome(L"C:/vmware/python3.7.5");
Py_Initialize();//使用python之前,要调用Py_Initialize();这个函数进行初始化
if (!Py_IsInitialized())
{
printf("初始化失败!");
return 0;
}
else
{
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
PyObject* pModule = NULL;//声明变量
PyObject* pFunc = NULL;// 声明变量
pModule = PyImport_ImportModule("gettime");//这里是要调用的文件名gettime.py
if (pModule == NULL)
{
cout << "没找到该Python文件" << endl;
}
else
{
pFunc = PyObject_GetAttrString(pModule, "gettime");//这里是要调用的函数名
PyObject_CallObject(pFunc, NULL);//没有参数就用NULL
}
Py_Finalize();//调用Py_Finalize,这个根Py_Initialize相对应的。
}
return 0;
}
int main()
{
int ch;
while(1)
{
if (_kbhit())
{
ch = _getch();
cout << ch << endl;
int t;
t = gettime();
if(ch == 27)
{
break;
}
}
}
system("pause");
return 0;
}
获取时间的python代码:gettime.py
//但是这里不能封装为dll
from datetime import datetime
import time
def gettime():
time = datetime.now()
t = time.minute*60*(10**6) + time.second*(10**6) + time.microsecond
print(t)