我有一個C語言的DLL庫,我設法在python中初始化了一個接收和發送資料的函式。現在我想重新組織python代碼,把所有的DLL python函式放在一個大類中。
這就是我在Visual Studio中的情況。
這是 Hello.h
#pragma once。
#include<stdio.h>
#include<stdint.h>
#ifdef SOME_API_EXPORTS
#define SOME_API __declspec(dllexport)
#else
#define SOME_API __declspec(dllimport)
#endif
SOME_API int sum(int a, int b) 。
SOME_API int mul(int a, int b) 。
SOME_API void message(void);
#define max_length 63U[/span
typedef struct
{
uint8_t param1。
uint32_t param2;
uint8_t param3;
uint8_t param4[max_length]。
} struct_1;
SOME_API void message_2(struct_1* request)。
SOME_API int function_1(uint8_t param_1, uint8_t param_2)。
SOME_API int function_2(uint8_t param_1, uint8_t param_2) 。
這就是Hello.C
#pragma once。
#include <stdio.h>
#include <stdlib.h>
#include "Hello.h"
#include <inttypes.h>
int sum(int a,int b) {
return a b;
}
int mul(int a, int b) {
return a * b。
}
空白資訊(void)
{
puts("hello from dll
")。)
}
SOME_API void message_2(struct_1* request)
{
printf("data: request=[%d %d %d %s]
"。
request->param1, request->param2, request->param3, request->param4)。)
//做一些處理
}
SOME_API int function_1(uint8_t param_1, uint8_t param_2)
{
int status。
//做一些處理
printf("%u , %u
", param_1, param_2)。)
status = 1;
return status。
}
SOME_API int function_2(uint8_t param_1, uint8_t param_2)
{
int status。
//做一些處理
printf("%u , %u
", param_1, param_2)。)
status = 0;
return status。
}
這是我的原始Python代碼,不含類。
import ctypes
test = ctypes.WinDLL('Project.dll')
max_length=63
class struct_1(ctypes.Structure)。
_fields_ = [("param1", ctypes.c_ubyte) 。
("param2", ctypes.c_uint32) 。
("param3", ctypes.c_ubyte) 。
("param4", ctypes.c_char * max_length) ]
test.message()
##req = struct_1(1, 2 , 3, "test".encode('utf-8'))。
##result = test.message_2(ctypes.byref(req))。
py_function_1 = test.function_1
py_function_1.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
py_function_1.restype = ctypes.c_int
py_function_1(255,255)
return_val = py_function_1()
print(return_val)
py_function_2 = test.function_2
py_function_2.argtype = (ctypes.c_uint8,ctypes.c_uint8 )
py_function_2.restype = ctypes.c_int
py_function_2(255,255)
return_val = py_function_2()
print(return_val)
這是我的python測驗代碼,試圖實作包含所有dll函式init的類。我現在只是用function_1和function_2來測驗。我將在未來添加其他函式。
import ctypes
test = ctypes.WinDLL('Project.dll')
max_length=63
class struct_1(ctypes.Structure)。
_fields_ = [("param1", ctypes.c_ubyte) 。
("param2", ctypes.c_uint32) 。
("param3", ctypes.c_ubyte) 。
("param4", ctypes.c_char * max_length) ]
class my_outer_class。
def __init__(self,py_function_1,py_function_2)。
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
myapi = my_outer_class()
result = myapi.py_function_1(255,255)
print(result)
result = myapi.py_function_2(255,255)
print(result)
我得到的錯誤是
Traceback (most recent call last):
檔案 "C:UsersDudeOneDriveDesktopTest.py", 行 25, in < module>
myapi = my_outer_class()
TypeError: __init__() 缺少2必要的位置引數。'py_function_1' and 'py_function_2'
我希望得到任何建議。謝謝。
uj5u.com熱心網友回復:
這是因為你定義類的init有py_function_1和py_function_2作為建構式。因此,你應該在初始化物件時傳遞test.function_1和test.function_2,即:
myapi = my_outer_class(test.function_1, test.function_2)
然后在類的init中正確使用它,即:
class my_outer_class。
def __init__(self, py_function_1, py_function_2)。
self.py_function_1 = py_function_1 # 改變這一點以使用建構式。
... # 其他行。
self.py_function_2 = py_function_2 # 改為使用建構式。
... # other lines
上面的解決方案意味著用戶有能力在建構式中提供其他可呼叫的功能。
另一種方法是限制用戶只能使用來自.dll的那個。一個可能的解決方案是在評論部分提到的那個。然而,我不認為這是最好的做法,因為它使用全域范圍來尋找函式。當你試圖擴展你的腳本時,比如說,通過在另一個檔案中分離類,這將是一個問題。你將需要在那里加入非類的代碼。
如果你想限制用戶不改變function_1和function_2,更好的解決方案是在類的init中加載.dll,而不是在全域范圍內。
class my_outer_class。
def __init__(self):
test = ctypes.WinDLL('Project.dll')
self.py_function_1 = test.function_1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/310483.html
標籤:
上一篇:Cstrcat將垃圾插入字串中
