1. C++でライブラリーを作る Linux編
2. pythonからC++のライブラリーを利用する Linux編
3. C++でライブラリー(DLL)を作る Windows編
4. pythonからC++のライブラリー(DLL)を利用する Windows編
5. C#からC++のライブラリー(DLL)を利用する
6. python・C#とC++間で複素数を受け渡す
7. C(C++)からライブラリー(DLL)を利用する 続編
8. pythonからC++のライブラリー(DLL)を利用する 続編
概要
前述( Windows Linux )のように、以前はインポート関数一つずつにpythonの関数を作成していたのですが、インポート関数、引数、戻り値を定義するグローバル変数をまとめて定義する方が簡潔であることに気付きましたのでご紹介します。
なおライブラリー(DLL)は前回( Windows Linux )と同じものを使用します。
サンプルコード
Windows用のサンプルコードです。
Linuxの場合は、ライブラリー名の変更以外に、文字コードセットを mbcs から utf-8 へ変更することで動きました。
CallLib.py
import os
import sys
import ctypes
class tagLibParameter(ctypes.Structure):
_fields_ = [("dInA", ctypes.c_double),
("dInB", ctypes.c_double),
("dOut", ctypes.c_double),
("sData", ctypes.c_char * 64)]
def main(argv):
LocalLoadLibrary()
#================================================================
nA = 1
nB = 2
#---------------------
nRet = LLibSum(nA, nB)
#---------------------
print('%d + %d = %d' % (nA, nB, nRet))
#================================================================
dsArray = ctypes.c_double * 3
dsIn = dsArray()
dsOut = dsArray()
dsIn[0] = 1.1
dsIn[1] = 2.2
dsIn[2] = 3.3
#--------------------------
LLibCalcX10(dsOut, dsIn, 3)
#--------------------------
print('in[%.1lf %.1lf %.1lf] out[%.1lf %.1lf %.1lf]' % (\
(dsIn[0]), (dsIn[1]), (dsIn[2]),\
(dsOut[0]), (dsOut[1]), (dsOut[2]) ))
#================================================================
sRxBuf = ctypes.create_string_buffer(256)
sTxBase = 'App->DLL: Hello !'
sTxBuf = sTxBase.encode('mbcs')
#-----------------------------------
LLibTxRxMessage(sRxBuf, sTxBuf, 256)
#-----------------------------------
print('%s' % (sRxBuf.value.decode('mbcs')))
#================================================================
LP = tagLibParameter()
(LP.dInA) = 1.1
(LP.dInB) = 2.2
#-----------------------------
LLibInOutStr(ctypes.byref(LP))
#-----------------------------
print('%.1lf + %.1lf = %.1lf [%s]' % (\
(LP.dInA), (LP.dInB), (LP.dOut), (LP.sData.decode('mbcs'))))
def LocalLoadLibrary():
path = os.getcwd()
sFnDll = path + '\\LocalLib.dll'
pLib = ctypes.cdll.LoadLibrary(sFnDll)
#------------------------------------------------
global LLibSum
LLibSum = pLib.LLibSum
LLibSum.argtypes = (ctypes.c_int, ctypes.c_int)
LLibSum.restype = ctypes.c_int
#------------------------------------------------
global LLibCalcX10
LLibCalcX10 = pLib.LLibCalcX10
LLibCalcX10.argtypes = (ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int)
LLibCalcX10.restype = ctypes.c_int
#------------------------------------------------
global LLibTxRxMessage
LLibTxRxMessage = pLib.LLibTxRxMessage
LLibTxRxMessage.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int)
LLibTxRxMessage.restype = ctypes.c_int
#------------------------------------------------
global LLibInOutStr
LLibInOutStr = pLib.LLibInOutStr
LLibInOutStr.argtype = ctypes.c_void_p
LLibInOutStr.restype = ctypes.c_int
if __name__ == '__main__':
main(sys.argv)
実行例
D:\>python CallLib.py
start DLL
1 + 2 = 3
in[1.1 2.2 3.3] out[11.0 22.0 33.0]
DLL->App: Hi ! rcvd[App->DLL: Hello !]
1.1 + 2.2 = 3.3 [2026 09/27 15:36:25 151]
end DLL