使用 python 函数将 "None" 读取为 return 值以连接 dll

Reading "None" as return value using python function to interface dll

我有一些 python 与 dll(C 语言)交互的代码。我有两个版本的 python 代码。我想使用我的第二个版本的代码。但是,当我 运行 第二个版本时,当我打印 return 值时,输出为“None”。在我的第一个版本中,输出分别是 1 和 0。如果有人能指出我的错误,我将不胜感激。谢谢

returns 1 和 0

的第一个代码版本

hello.py

import ctypes

class my_outer_class:

    def __init__(self):

        test = ctypes.WinDLL('C:\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')

        self.py_function_1 = test.function_1
        self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_1.restype = ctypes.c_int

        self.py_function_2 = test.function_2
        self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_2.restype = ctypes.c_int

run_test.py

import hello
import ctypes

myapi = hello.my_outer_class()
            
result = myapi.py_function_1(123,123)
print(result)

result = myapi.py_function_2(123,123)
print(result)

1
0
>>> 

第二个版本打印 None 作为输出

import ctypes

class my_outer_class:

    def __init__(self):

        self.test = ctypes.WinDLL('C:\Users\OneDrive\HelloWorld\HelloWorld\loc\Debug\HelloWorld.dll')

    def func_1(self, argtype, restype):
        self.py_function_1 = self.test.function_1
        self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_1.restype = ctypes.c_int

    def func_2(self, argtype, restype):
        self.py_function_2 = self.test.function_2
        self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_2.restype = ctypes.c_int

run_test.py

import hello
import ctypes

myapi = hello.my_outer_class()
            
result = myapi.func_1(123,123)
print(result)

result = myapi.func_2(123,123)
print(result)

None
None
>>> 

你从不打电话给self.test.py_function_X

import ctypes

class my_outer_class:
    def __init__(self):
        self.test = ctypes.WinDLL(...)

    def func_1(self, a, b):
        self.py_function_1 = self.test.function_1
        self.py_function_1.argtype = ( ctypes.c_uint8, ctypes.c_uint8 )
        self.py_function_1.restype = ctypes.c_int
        return self.py_function_1(a, b);              # Missing

也就是说,每次调用 func_X 时都执行前三行是没有意义的。我不确定你要做什么。也许您想要以下内容:

import ctypes

class my_outer_class:
    def __init__(self):
        self.test = ctypes.WinDLL(...)

        self.py_function_1 = self.test.function_1
        self.py_function_1.argtype = ( ctypes.c_uint8, ctypes.c_uint8 )
        self.py_function_1.restype = ctypes.c_int

    def func_1(self, a, b):
        return self.py_function_1(a, b);

或者您可能试图延迟实例化。我认为没有任何理由这样做。但是如果有的话,你可以使用类似下面的东西:

import ctypes

class my_outer_class:
    def __init__(self):
        self.test = ctypes.WinDLL(...)
        self.py_function_1 = None

    def func_1(self, a, b):
        if not self.py_function_1:
            self.py_function_1 = self.test.function_1
            self.py_function_1.argtype = ( ctypes.c_uint8, ctypes.c_uint8 )
            self.py_function_1.restype = ctypes.c_int

        return self.py_function_1(a, b);

在您的第一个示例中,您将“py_function_1”和“py_function_2”作为属性存储在 class 中,然后您通过使用直接调用这些属性:

myapi.py_function_1(123,123)
myapi.py_function_2(123,123)

这些方法在调用时直接 return 为您提供一个值(根据我假设的 C 代码)。在第二个示例中,您没有调用属性,而是直接调用了在 class 中创建的方法。然后,当您执行此操作时,您的 python 方法(不是您的 C 代码)在方法内部没有定义 return(因此它们 return none)。您可以通过多种方式解决这个问题,以下是我的看法:

import ctypes

class my_outer_class:

    def __init__(self):

        # Initialize anything that has to do with USING your C
        # method in the __init__ method. BEFORE you call it elsewhere.
        # These only need to be defined once during the initialization
        # of your object not every time you call the function.
        self.test = ctypes.WinDLL('dll path here')
        
        self.py_function_1 = self.test.function_1
        self.py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_1.restype = ctypes.c_int
        
        self.py_function_2 = self.test.function_2
        self.py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
        self.py_function_2.restype = ctypes.c_int

    # Now your able to define the functions that use the
    # functions we setup in the __init__ file.

    def func_1(self, val1, val2):
        return self.py_function_1(val1, val2)

    def func_2(self, val1, val2):
        return self.py_function_2(val1, val2)

让我知道这是否有帮助,或者如果您有任何其他问题!