2009年11月12日星期四

Python ctypes 使用方法(2)

介紹一些使用方法:
define C library 的 header 檔
from ctypes import *

WS_MAX_USER_LEN=10
WS_MAX_PIN_LEN =4
WS_MAX_LMK_ID =1
WS_MAX_IP_ADDR_LEN =10
WS_MAX_LABEL=5
WS_MAX_DESC=10
WS_MAX_LOG_ENTRY_LEN=100
WS_MAX_ZMK_KEY_ID_LEN=10

WS_VOID=c_void_p
WS_BYTE=c_ubyte
WS_CHAR=WS_BYTE
WS_BOOL=WS_BYTE
WS_USHORT=c_ushort
WS_ULONG=c_ulong
WS_LONG=c_long

WS_RV=WS_ULONG
WS_UNIT_ID=WS_ULONG
WS_DEVICE_ID=WS_ULONG
WS_CLUSTER_ID=WS_ULONG
WS_FLAGS=WS_ULONG
WS_UNIT_TYPE=WS_ULONG
# WebSentry  Specific Return Codes #
WSR_OK= 0x00000000
WSR_CANCEL = 0x00000001
WSR_HOST_MEMORY= 0x00000002
WSR_UNIT_ID_INVALID =0x00000003
WSR_GENERAL_ERROR=0x00000005

class WS_ITEM(Structure):
_fields_=[('pData',POINTER(WS_BYTE)),('len', WS_ULONG)]

class WS_VERSION(Structure):
_fields_=[('major', WS_BYTE), ('minor', WS_BYTE)]

from wapich  import *   #import python wrap data
from ctypes import *

libc=cdll.LoadLibrary('wapicd.dll')
WS_Initialize=libc.WS_Initialize  #define  function
WS_Initialize.restype=WS_RV    # set return type  = WS_RV, if none, always return int

rv=WSR_OK
rv=WS_Initialize()

if rv != WSR_OK:
print rv
else:
print 'WS_Initialize is Ok'

WS_GetInfo=libc.WS_GetInfo
WS_GetInfo.restype=WS_RV

myInfo=WS_INFO()
myInfo_p=pointer(myInfo)
myInfo_p.contents=myInfo   # assign  myInfo_ptr pointer to myInfo

rv=WSR_OK
rv=WS_GetInfo(myInfo_p)
if rv !=WSR_OK:
print rv

print myInfo.manufacturerID[:]
print myInfo.description[:]
print myInfo.version.major
print myInfo.version.minor

沒有留言: