1

I'm using a 3rd-Party Vendor who is providing a Windows Driver (DLL) and C Header Files. What I'm trying to do is use SWIG to recompile the header file into a Python Module.

Here are the my files:
- BTICard.i
- BTICard.h
- BTICARD64.dll
- BTICARD64.lib

SWIG interface source

%module BTICard
%include <windows.i>
%{
#define SWIG_FILE_WITH_INIT
#include "BTICard.H"
#define BTICardAPI
%}

In Cygwin, I used the following commands:

swig -python -py3 BTICard.i

Which then generated the following files:
- BTICard.py
- BTICard_wrap.c

In Cygwin, compile for Python Module

gcc -c -fpic BTICARD.H BTICard_wrap.c -I/usr/include/python3.8

Which now allows BTICard to be imported in Python

import BTICard
import ctypes
BTICarddll = ctypes.WinDLL('BTICARD64')
pRec1553 = SEQRECORD1553() # Doesn't initialize

The BTICard.H contains the following:
typedef struct - Used to initialize various field structures
enum - Constant Declarations

According to the SWIG documentation, the typedef structs are supposed to be converted into Python classes. When I tried initializing the class, I got a NameError. I suspect the issue is with my interface file not recognizing these types so it failed to convert them.

Upon further investigation, I tried using the distutils approach and created the setup.py

#!/usr/bin/env python3.8
"""
setup.py file for SWIG
"""
from distutils.core import setup, Extension

example_module = Extension('_BTICard',
    sources=['BTICard_wrap.c', 'BTICard.h'],)

setup (name = 'BTICard',
    version = '0.1',
    author = "TESTER",
    description = """BTICard API""",
    ext_modules = [example_module],
    py_modules = ["BTICard"],
    )

To build the package:

$ python3.8 setup.py build_ext --inplace
running build_ext
building '_BTICard' extension
error: unknown file type '.h' (from 'BTICard.h')

What's the issue here?

Is there a way I can access the Python source file after creating the object from gcc?

All I am trying to do is to validate a separate Python Wrapper that seems to have issues (it's a completely separate topic). Is there another way to create this Python Module?

WeSC
  • 11
  • 3

1 Answers1

1

The .i file isn't including the interface to export. It should look like:

%module BTICard

%{
#include "BTICard.H"    // this just makes the interface available to the wrapper.
%}

%include <windows.i>
%include "BTICard.h"    // This wraps the interface defined in the header.

setup.py knows about SWIG interfaces, so include the .i file directly as a source. Headers are included by the sources and aren't listed as sources. You may need other options but this should get you on the right track. You'll likely need the DLLs export library (BTICard.lib) and need to link to that as well:

example_module = Extension('_BTICard',
    sources=['BTICard.i'],
    libraries=['BTICard.lib'])
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251