关键词搜索

源码搜索 ×
×

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

发布2020-07-29浏览6041次

详情内容

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

 

如下:

  1. cimport win32api
  2. cimport win32gui
  3. cdef public int getCursorPosX():
  4. x, y = win32api.GetCursorPos()
  5. return int(x)
  6. cdef public int getCursorPosY():
  7. x, y = win32api.GetCursorPos()
  8. return int(y)
  9. cdef public int test():
  10. x = 10;
  11. return int(x)
  12. cdef public int test2():
  13. x = 10
  14. win32api.GetCursorPos()
  15. return int(x)

如果这样编译:

cython CursorPy.pyx

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

目前只能将其去掉

  1. #cimport win32api
  2. #cimport win32gui
  3. cdef public int getCursorPosX():
  4. x, y = win32api.GetCursorPos()
  5. return int(x)
  6. cdef public int getCursorPosY():
  7. x, y = win32api.GetCursorPos()
  8. return int(y)
  9. cdef public int test():
  10. x = 10;
  11. return int(x)
  12. cdef public int test2():
  13. x = 10
  14. win32api.GetCursorPos()
  15. return int(x)

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

cython CursorPy.pyx

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

这里必须用x64的release.

包含项需要:

文件结构如下:

新建

GetCursorPostion.h

  1. #pragma once
  2. #include "stdafx.h"
  3. #include <Windows.h>
  4. #define ExportFunc _declspec(dllexport)
  5. extern "C" ExportFunc POINT getCursorPos();
  6. extern "C" ExportFunc int getTest();
  7. extern "C" ExportFunc int getTest2();
  8. GetCursorPostion.cpp
  9. // GetCursorPosition.cpp : 定义 DLL 应用程序的导出函数。
  10. //
  11. #include "stdafx.h"
  12. #include "CursorPy.h"
  13. #include "GetCursorPosition.h"
  14. POINT getCursorPos() {
  15. POINT result;
  16. result.x = getCursorPosX();
  17. result.y = getCursorPosY();
  18. return result;
  19. }
  20. int getTest() {
  21. int ret = test();
  22. return ret;
  23. }
  24. int getTest2() {
  25. int ret = test2();
  26. return ret;
  27. }

再dll启动时进行添加:

这里需要调用:

  1. switch (ul_reason_for_call)
  2. {
  3. case DLL_PROCESS_ATTACH:
  4. Py_Initialize();
  5. PyInit_CursorPy();
  6. case DLL_THREAD_ATTACH:
  7. case DLL_THREAD_DETACH:
  8. case DLL_PROCESS_DETACH:
  9. Py_Finalize();
  10. break;
  11. }

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

然后进行生成好文件:

下面是调用:

源码如下:

  1. #include <iostream>
  2. #include <Windows.h>
  3. using namespace std;
  4. typedef POINT(*CursorPos)();
  5. typedef int(*Test)();
  6. typedef int(*Test2)();
  7. int main() {
  8. HMODULE hMoudle = LoadLibrary("D:\\vsproject\\GetCursorPosition\\x64\\Release\\GetCursorPosition.dll");
  9. if (!hMoudle) {
  10. cout << "loadLibrary failed!" << endl;
  11. getchar();
  12. return 0;
  13. }
  14. CursorPos cursorPos;
  15. cursorPos = (CursorPos)GetProcAddress(hMoudle, "getCursorPos");
  16. Test test = (Test)GetProcAddress(hMoudle, "getTest");
  17. Test2 test2 = (Test2)GetProcAddress(hMoudle, "getTest2");
  18. while (1) {
  19. //POINT point = cursorPos();
  20. //cout << "x:" << point.x << " y:" << point.y << endl;
  21. cout << test() << endl;
  22. //cout << test2() << endl;
  23. Sleep(500);
  24. }
  25. return 0;
  26. }

源码打包下载地址:

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

 

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载