halochief996
halochief996

Reputation: 113

Convert a .lib and .h file to a DLL that can be used in Python

So I'm trying to import some function contained in a .lib file into Python to build an SDK that will allow me to talk to some special hardware components. I read online that it is not really easy to import a .lib file into Python:

Static library (.lib) to Python project

So I'm trying to build a dll using the .lib and its corresponding .h file. I don't have access to the .lib source code. All I have access to is the .h file. I've looked online and found this:

Converting static link library to dynamic dll

Since I'm building the DLL for Python I can't use the .def file. I tried directly just importing the .h and .lib file into a project and creating a dll file but the functions were not implemented. So I tried making a separate .h file called wrapper that wraps around the functions in the .h file and calls them, but the functions are still not implemented and working. And honestly I highly doubt what I did is correct

Here is my code:

hardware.h - the header file that came with the .lib file (note only putting up one function)

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

_hardware.h - wrapper around the original header file

#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 - implementing the wrapper

#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);
    }
}

Anyway I feel like making a wrapper is pointless as I should be able to just use the original .h file and .lib file directly to call the functions. Unless making a wrapper is the only way I can make a dll without getting the lib file source code. Are there ways to make a dll without knowing the lib file source code? Is there a way to import the lib file directly into Python? Any help is appreciated.

Upvotes: 2

Views: 1799

Answers (1)

halochief996
halochief996

Reputation: 113

Thank you @qexyn for the help. So I took out the namespace and class from my wrapper (_hardware.h) and made the functions global. Then I added extern "C" to those global functions, so my code ended up looking like this:

_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]);

After that everything worked. I was able to get Hardware info in my python SDK. Make sure to add the extern "C" otherwise the names get mangled and it show up ugly if you look up your function in dependency walker. Thanks for the help

Upvotes: 1

Related Questions