Python笔记-使用cython生成dll,C++进行调用

这里就是把python改成cython语法,然后使用cython跑下,生成.h和.cpp然后通过python下的lib,以及so文件,以及include生成对应的dll,然后用c++调用即可:

 

如下:

cimport win32api
cimport win32gui

cdef public int getCursorPosX():
    x, y = win32api.GetCursorPos()
    return int(x)
	
cdef public int getCursorPosY():
    x, y = win32api.GetCursorPos()
    return int(y)
	
cdef public int test():
	x = 10;
	return int(x)
 
 
cdef public int test2():
	x = 10
	win32api.GetCursorPos()
	return int(x)

如果这样编译:

cython CursorPy.pyx

提示pxd是不存中的,目前再cpython中存在的pxd有:

目前只能将其去掉

#cimport win32api
#cimport win32gui

cdef public int getCursorPosX():
    x, y = win32api.GetCursorPos()
    return int(x)
	
cdef public int getCursorPosY():
    x, y = win32api.GetCursorPos()
    return int(y)
	
cdef public int test():
	x = 10;
	return int(x)
 
 
cdef public int test2():
	x = 10
	win32api.GetCursorPos()
	return int(x)

使用下面的命令生成.h和.cpp

cython CursorPy.pyx

下面演示下生成dll,vs2015!!创建dll

这里必须用x64的release.

包含项需要:

文件结构如下:

新建

GetCursorPostion.h

#pragma once


#include "stdafx.h"
#include <Windows.h>

#define ExportFunc _declspec(dllexport)

extern "C" ExportFunc POINT getCursorPos();
extern "C" ExportFunc int getTest();
extern "C" ExportFunc int getTest2();

GetCursorPostion.cpp
// GetCursorPosition.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "CursorPy.h"
#include "GetCursorPosition.h"

 POINT getCursorPos() {

	POINT result;
	result.x = getCursorPosX();
	result.y = getCursorPosY();
	return result;
}

 int getTest() {

	 int ret = test();
	 return ret;
 }

 int getTest2() {

	 int ret = test2();
	 return ret;
 }

再dll启动时进行添加:

这里需要调用:

	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		Py_Initialize();
		PyInit_CursorPy();
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		Py_Finalize();
		break;
	}

其中PyInit_CursorPy()可以在CursorPy.h中找到

然后进行生成好文件:

下面是调用:

源码如下:

#include <iostream>
#include <Windows.h>

using namespace std;

typedef POINT(*CursorPos)();
typedef int(*Test)();
typedef int(*Test2)();

int main() {

	HMODULE hMoudle = LoadLibrary("D:\\vsproject\\GetCursorPosition\\x64\\Release\\GetCursorPosition.dll");
	if (!hMoudle) {

		cout << "loadLibrary failed!" << endl;
		getchar();
		return 0;
	}

	CursorPos cursorPos;
	cursorPos = (CursorPos)GetProcAddress(hMoudle, "getCursorPos");
	Test test = (Test)GetProcAddress(hMoudle, "getTest");
	Test2 test2 = (Test2)GetProcAddress(hMoudle, "getTest2");

	while (1) {

		//POINT point = cursorPos();
		//cout << "x:" << point.x << " y:" << point.y << endl;
		cout << test() << endl;
		//cout << test2() << endl;

		Sleep(500);
	}

	return 0;
}

源码打包下载地址:

https://github.com/fengfanchen/CAndCPP/tree/master/pythonDll

 

 

 

相关推荐
第 1 章 软件包的安装和介绍....................1 1.1 Python 简介......................................1 1.2 安装软件包......................................2 1.2.1 Python(x,y)..................................... 2 1.2.2 Enthought Python Distribution (EPD)............................................. 3 1.3 方便的开发工具..............................3 1.3.1 IPython........................................... 4 1.3.2 Spyder............................................ 8 1.3.3 Wing IDE 101..............................12 1.4 函数库介绍....................................13 1.4.1 数值计算库.................................13 1.4.2 符号计算库.................................14 1.4.3 界面设计.....................................14 1.4.4 绘图与可视化 .............................14 1.4.5 图像处理和计算机视觉..............15 第 2 章 NumPy——快速处理数据.......... 16 2.1 ndarray 对象...................................16 2.1.1 创建数组.....................................16 2.1.2 存取元素.....................................21 2.1.3 多维数组.....................................24 2.1.4 结构数组.....................................29 2.1.5 内存结构.....................................32 2.2 ufunc 运算......................................35 2.2.1 四则运算.....................................37 2.2.2 比较和布尔运算 .........................39 2.2.3 自定义 ufunc 函数 ......................40 2.2.4 广播.............................................42 2.2.5 ufunc 函数的方法.......................46 2.3 多维数组的下标存取....................48 2.3.1 下标对象.....................................48 2.3.2 整数数组作为下标.....................49 2.3.3 一个复杂的例子.........................51 2.3.4 布尔数组作为下标.....................53 2.4 庞大的函数库 ............................... 54 2.4.1 求和、平均值、方差.................54 2.4.2 最值和排序.................................55 2.4.3 多项式函数.................................57 2.4.4 分段函数.....................................60 2.4.5 统计函数.....................................62 2.5 线性代数 ....................................... 65 2.5.1 各种乘积运算.............................65 2.5.2 解线性方程组........................
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页