我有一些與 dll 介面的 python 代碼(在 C 中)。我有兩個版本的 python 代碼。我想使用我的第二個版本的代碼。但是,當我運行第二個版本時,當我列印回傳值時,輸出為“無”。在我的第一個版本中,輸出分別為 1 和 0。如果有人能指出我的錯誤,我將不勝感激。謝謝
回傳 1 和 0 的代碼的第一個版本
你好.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
運行_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
運行_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
>>>
uj5u.com熱心網友回復:
你從不打電話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);
uj5u.com熱心網友回復:
在您的第一個示例中,您將“py_function_1”和“py_function_2”作為屬性存盤在類中,然后您通過使用以下方法直接呼叫這些屬性:
myapi.py_function_1(123,123)
myapi.py_function_2(123,123)
這些方法在呼叫時直接向您回傳一個值(根據我假設的您的 C 代碼)。在您的第二個示例中,您沒有呼叫屬性,而是直接呼叫您在類中創建的方法。然后,當您執行此操作時,您的 Python 方法(不是您的 C 代碼)在方法中沒有定義回傳值(因此它們不回傳任何值)。有很多方法可以解決這個問題,以下是我的看法:
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)
讓我知道這是否有幫助或者您有任何其他問題!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364024.html
