在 Python 中应用 cdll 的处理程序问题
Handler Issues Applying cdll in Python
我正在尝试使用 ctypes 通过 Python 移植一些 C dll(FANUC FOCAS 库 - 用于 CNC)代码,所以我编写了移植代码。 (如下),但是在加载DLL和调用函数时得到一个很奇怪的结果。就我而言,我不明白在 python.
中使用处理程序
我想在 python 中应用以下 C 代码。
声明(for c)
#include "fwlib64.h"
FWLIBAPI short WINAPI cnc_allclibhndl3(const char *ipaddr,unsigned short port,
long timeout, unsigned short *FlibHndl);
示例代码(在focas library manual for c)
#include "fwlib64.h"
void example( void )
{
unsigned short h;
short ret;
ODBST buf;
ret = cnc_allclibhndl3( "192.168.0.100", 8193, 1, &h ) ;
//
if ( !ret ) {
cnc_statinfo( h, &buf ) ;
cnc_freelibhndl( h ) ;
} else {
printf( "ERROR!(%d)\n", ret ) ;
}
}
Testfocas.py
from ctypes import *
mylib = cdll.LoadLibrary('./Fwlib64.dll')
class ODBSYS(Structure):
pass
_fields_ =[
("dummy", c_ushort),
("max_axis", c_char*2),
("cnc_type", c_char*2),
("mt_type",c_char*2),
("series",c_char*4),
("version",c_char*4),
("axes",c_char*2),]
h=c_ushort()
pt=pointer(h)
ret=c_short()
buf=ODBSYS()
ret=mylib.cnc_allclibhndl3('192.168.0.100',8193,1,pt)
mylib.cnc_statinfo(h,buf)
mylib.cnc_freelibhndl(h)
我希望函数为 return 0 或 -16,但在我的例子中,函数 return 是
cnc_allclibhndl3 = 65520 (i guess open port)
cnc_statinfo = -8
cnc_freelibhndl -8
Return 数据状态 Window 函数
EW_OK(0) Normal termination
EW_SOCKET(-16) Socket communication error Check the power supply of CNC, Ethernet I/F board, Ethernet connection cable.
EW_HANDLE(-8) Allocation of handle number is failed.
我不知道我哪里错了。
CDLL
用于 __cdecl
调用约定。 cdll
不推荐使用,因为它是跨模块的共享实例。
WINAPI
定义为__stdcall
,所以使用WinDLL
:
mylib = WinDLL.LoadLibrary('./Fwlib64.dll')
接下来,为函数的参数和结果类型定义 argtypes
和 restype
:
mylib.cnc_allclibhndl3.argtypes = c_char_p,c_ushort,c_long,POINTER(c_ushort)
mylib.cnc_allclibhndl3.restype = c_short
最后,通过引用传递输出参数。它比创建 pointer
:
更有效
h = c_ushort()
ret = mylib.cnc_allclibhndl3('192.168.0.100',8193,1,byref(h))
未提供 cnc_statinfo
和 cnc_freelibhndl
的原型。也为它们定义 argtypes
和 restype
。
我正在尝试使用 ctypes 通过 Python 移植一些 C dll(FANUC FOCAS 库 - 用于 CNC)代码,所以我编写了移植代码。 (如下),但是在加载DLL和调用函数时得到一个很奇怪的结果。就我而言,我不明白在 python.
中使用处理程序我想在 python 中应用以下 C 代码。
声明(for c)
#include "fwlib64.h"
FWLIBAPI short WINAPI cnc_allclibhndl3(const char *ipaddr,unsigned short port,
long timeout, unsigned short *FlibHndl);
示例代码(在focas library manual for c)
#include "fwlib64.h"
void example( void )
{
unsigned short h;
short ret;
ODBST buf;
ret = cnc_allclibhndl3( "192.168.0.100", 8193, 1, &h ) ;
//
if ( !ret ) {
cnc_statinfo( h, &buf ) ;
cnc_freelibhndl( h ) ;
} else {
printf( "ERROR!(%d)\n", ret ) ;
}
}
Testfocas.py
from ctypes import *
mylib = cdll.LoadLibrary('./Fwlib64.dll')
class ODBSYS(Structure):
pass
_fields_ =[
("dummy", c_ushort),
("max_axis", c_char*2),
("cnc_type", c_char*2),
("mt_type",c_char*2),
("series",c_char*4),
("version",c_char*4),
("axes",c_char*2),]
h=c_ushort()
pt=pointer(h)
ret=c_short()
buf=ODBSYS()
ret=mylib.cnc_allclibhndl3('192.168.0.100',8193,1,pt)
mylib.cnc_statinfo(h,buf)
mylib.cnc_freelibhndl(h)
我希望函数为 return 0 或 -16,但在我的例子中,函数 return 是
cnc_allclibhndl3 = 65520 (i guess open port)
cnc_statinfo = -8
cnc_freelibhndl -8
Return 数据状态 Window 函数
EW_OK(0) Normal termination
EW_SOCKET(-16) Socket communication error Check the power supply of CNC, Ethernet I/F board, Ethernet connection cable.
EW_HANDLE(-8) Allocation of handle number is failed.
我不知道我哪里错了。
CDLL
用于 __cdecl
调用约定。 cdll
不推荐使用,因为它是跨模块的共享实例。
WINAPI
定义为__stdcall
,所以使用WinDLL
:
mylib = WinDLL.LoadLibrary('./Fwlib64.dll')
接下来,为函数的参数和结果类型定义 argtypes
和 restype
:
mylib.cnc_allclibhndl3.argtypes = c_char_p,c_ushort,c_long,POINTER(c_ushort)
mylib.cnc_allclibhndl3.restype = c_short
最后,通过引用传递输出参数。它比创建 pointer
:
h = c_ushort()
ret = mylib.cnc_allclibhndl3('192.168.0.100',8193,1,byref(h))
未提供 cnc_statinfo
和 cnc_freelibhndl
的原型。也为它们定义 argtypes
和 restype
。