【问题标题】:Convert a .lib and .h file to a DLL that can be used in Python将 .lib 和 .h 文件转换为可在 Python 中使用的 DLL
【发布时间】:2016-10-27 21:46:47
【问题描述】:

所以我试图将 .lib 文件中包含的一些函数导入 Python 以构建一个 SDK,该 SDK 将允许我与一些特殊的硬件组件通信。我在网上看到,将 .lib 文件导入 Python 并不容易:

Static library (.lib) to Python project

所以我正在尝试使用 .lib 及其对应的 .h 文件构建一个 dll。我无权访问 .lib 源代码。我只能访问 .h 文件。我上网查了一下,发现是这样的:

Converting static link library to dynamic dll

由于我正在为 Python 构建 DLL,因此无法使用 .def 文件。我尝试直接将 .h 和 .lib 文件导入项目并创建 dll 文件,但未实现这些功能。因此,我尝试制作一个名为 wrapper 的单独 .h 文件,该文件将 .h 文件中的函数包装起来并调用它们,但这些函数仍未实现和工作。老实说,我非常怀疑我所做的是否正确

这是我的代码:

hardware.h - .lib 文件附带的头文件(注意只放一个函数)

extern "C" int WINAPI GetDigitalOutputInfo(unsigned int deviceNumbers[16], 
                                            unsigned int numBits[16], 
                                            unsigned int numLines[16]);

_hardware.h - 原始头文件的包装

#pragma once

#include <Windows.h>

#ifdef Hardware_EXPORTS
#define Hardware_API __declspec(dllexport) 
#else
#define Hardware_API __declspec(dllimport) 
#endif

namespace Hardware
{
    class Functions
    {
        public:
            static Hardware_API int NewGetDigitalOutputInfo(unsigned int deviceNumbers[16], unsigned int numBits[16], unsigned int numLines[16]);
    };

}

Hardware.cpp - 实现包装器

#include "stdafx.h"
#include "hardware.h"
#include "_hardware.h"
#pragma comment(lib, "..\\lib\\PlexDO.lib")
#pragma comment(lib, "legacy_stdio_definitions.lib")

namespace Hardware
{
    int Functions::NewGetDigitalOutputInfo(unsigned int deviceNumbers[16], unsigned int numBits[16], unsigned int numLines[16]) {
        return GetDigitalOutputInfo(deviceNumbers, numBits, numLines);
    }
}

无论如何,我觉得制作包装器毫无意义,因为我应该能够直接使用原始 .h 文件和 .lib 文件来调用函数。除非制作包装器是我在不获取 lib 文件源代码的情况下制作 dll 的唯一方法。有没有办法在不知道lib文件源代码的情况下制作dll?有没有办法将lib文件直接导入Python?任何帮助表示赞赏。

【问题讨论】:

  • 我很确定您将需要使用包装器方法。然而,为了让 Python 调用你的包装 DLL,函数应该是 C 而不是 C++。删除所有命名空间和类语法,只需 extern "C" 您的全局函数(就像在与 lib 匹配的标头中一样)。保留 Hardware_API 宏,并确保在构建 DLL 时定义 Hardware_EXPORTS。
  • @qexyn 谢谢!所以我删除了命名空间和类语法,但是当我将 extern "C" 添加到我的全局函数中时,如下所示:'PlexDO_API extern "C" int DOGetDigitalOutputInfo(unsigned int deviceNumbers[16], unsigned int numBits[16], unsigned int numLines[ 16]);'这些函数没有构建,我无法通过 Python 或 C++ 访问它们。如果我没有 extern "C",我仍然可以将 dll 与我构建的任何 C++ 应用程序一起使用,但 Python 无法访问它们。我知道为了让 Python 使用 DLL,extern "C" 是必需的。我写错了吗?

标签: python c++ dll .lib


【解决方案1】:

感谢@qexyn 的帮助。所以我从我的包装器(_hardware.h)中取出了命名空间和类,并使函数成为全局的。然后我将 extern "C" 添加到这些全局函数中,所以我的代码最终看起来像这样:

_hardware.h

#pragma once

#include <Windows.h>

#ifdef Hardware_EXPORTS
#define Hardware_API __declspec(dllexport) 
#else
#define Hardware_API __declspec(dllimport) 
#endif

extern "C" Hardware_API int NewGetDigitalOutputInfo(unsigned int deviceNumbers[16], unsigned int numBits[16], unsigned int numLines[16]);

之后一切正常。我能够在我的 python SDK 中获取硬件信息。确保添加外部“C”,否则名称会被破坏,并且如果您在依赖项walker 中查找您的函数,它会显得很丑陋。感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 2023-03-20
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多