python调用dll

调用CALLBACK标记的dll方法要用dll = ctypes.windll.LoadLibrary( 'test.dll' )

没有CALLBACK的方法用dll = ctypes.cdll.LoadLibrary( 'test.dll' )

 

例子:

from ctypes import *
d2d = cdll.LoadLibrary("Direct2dDll.dll")
print(d2d.DCreateWindow)

CALLBACK = CFUNCTYPE(c_int,c_int)

def py_cmp_func(a):
    print(str(a))
    return 0;
    
callBackFunc = CALLBACK(py_cmp_func)

d2d.SetCallBack(callBackFunc)
d2d.DCreateWindow()
// Direct2dDll.cpp : 定义 DLL 应用程序的导出函数。
//

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

int (*CallBack)(int);

void SetCallBack(int (*fun)(int))
{
    (*fun)(0);
    CallBack = fun;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND    - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY    - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    (*CallBack)((int)message);
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: 在此添加任意绘图代码...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

HWND DCreateWindow()
{
    HINSTANCE instance = GetModuleHandle(0);

    TCHAR szWindowClass[] = L"pw";            // 主窗口类名

    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    printf("2");
    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = instance;
    wcex.hIcon            = 0;//LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = 0;//MAKEINTRESOURCE(IDC_WIN32PROJECT1);
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = 0;//LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    RegisterClassEx(&wcex);
    printf("3");
    HWND hWnd = CreateWindow(szWindowClass, L"title", WS_OVERLAPPEDWINDOW,
                  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, instance, NULL);
    printf("4");
    ShowWindow(hWnd, SW_MAXIMIZE);
    UpdateWindow(hWnd);

       
    MSG msg;
    // 主消息循环:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, 0, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return hWnd;
}

 

转载于:https://www.cnblogs.com/wangjixianyun/p/3416315.html

### 如何在Python调用DLL文件 #### 使用 `ctypes` 调用 DLL 文件 `ctypes` 是 Python 中的一个标准库模块,用于加载动态链接库 (DLL) 并调用其中的函数。以下是通过 `ctypes` 加载并调用 DLL 函数的方法。 1. **导入必要的模块** 需要从 `ctypes` 导入相关类来操作 DLL 和数据类型。 2. **指定 DLL 文件路径** 将目标 DLL 的绝对路径传递给 `CDLL` 或 `WinDLL` 类以加载该库。 3. **定义参数和返回值类型** 如果 DLL 函数有特定的数据类型需求,则需设置其输入参数和返回值类型以便正确交互。 4. **调用 DLL 函数** 可像调用普通 Python 函数一样调用已加载的 DLL 函数。 下面是一个完整的示例: ```python from ctypes import * # 定义 DLL 文件路径 dll_path = r"C:\path\to\your\dll.dll" # 加载 DLL 库 lib = CDLL(dll_path) # 假设 DLL 提供了一个名为 add 的函数,接受两个整数作为参数并返回它们的和 lib.add.argtypes = [c_int, c_int] # 设置参数类型为 int, int lib.add.restype = c_int # 设置返回值类型为 int # 调用 DLL 中的 add 函数 result = lib.add(2, 3) print(f"The result of adding 2 and 3 is {result}") # 输出结果应为 5 ``` 此代码片段展示了如何加载一个 DLL 文件并通过它调用一个简单的加法函数[^2]。 #### 处理 C++ 编写的 DLLDLL 是由 C++ 构建时,需要注意符号修饰问题。默认情况下,C++ 编译器会对函数名进行名称修饰(name mangling),这使得 Python 的 `ctypes` 无法直接找到对应的函数。为了避免这一问题,在编写导出函数时需要加上 `extern "C"` 关键字,从而禁用名称修饰[^3]。 例如,假设有一个 C++ 工程中的头文件如下所示: ```cpp #ifdef EXPORTS_DLL #define MY_API __declspec(dllexport) #else #define MY_API __declspec(dllimport) #endif // 确保函数可以被 Python 正确识别 extern "C" MY_API int multiply(int a, int b); ``` 此时可以在 Python 中按照相同方式加载并调用这个乘法函数: ```python from ctypes import * dll_path = r"C:\path\to\your\dll.dll" lib = CDLL(dll_path) lib.multiply.argtypes = [c_int, c_int] lib.multiply.restype = c_int result = lib.multiply(4, 5) print(f"The result of multiplying 4 and 5 is {result}") ``` #### 数据类型的映射 为了确保与 DLL 进行正确的通信,必须清楚地知道每个参数及其对应的结果应该是什么样的数据类型。下表列出了常见的 C 类型到 Python `ctypes` 对象之间的转换关系[^1]: | C Type | ctypes Object | |----------------|--------------------| | char | c_char | | unsigned char | c_ubyte | | short | c_short | | unsigned short | c_ushort | | int | c_int | | unsigned int | c_uint | | long | c_long | | float | c_float | | double | c_double | 对于更复杂的情况,比如字符串处理或者数组传递,可能还需要额外配置缓冲区或指针对象。例如创建字符缓冲区可以用 `create_string_buffer()` 方法实现[^6]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值