HashcalcAdapter 代码
HashcalcAdapter.py 实现了一个 python 的 class HashcalcAdapter,HashcalcAdapter 对 hashtcalc.dl 的 C 语言接口进行了封装,使得其他 python 模块可以直接通过 HashcalcAdapter 使用 hashtcalc.dll 中实现的 hash 算法。具体的代码如下:
清单 13. HashcalcAdapter.py 代码
from ctypes import windll
from ctypes import *
class HashcalcAdapter(object):
def __init__(self, dllpath):
self._dllpath = dllpath
self._libc = windll.LoadLibrary(self._dllpath)
def calc_CRC32(self, filename):
new_filename = c_char_p(filename)
return self._libc.calc_CRC32(new_filename)
def calc_MD5(self, filename):
new_filename = c_char_p(filename)
return self._libc.calc_MD5(new_filename)
def calc_SHA1(self, filename):
new_filename = c_char_p(filename)
return self._libc.calc_SHA1(new_filename)
from ctypes import *
class HashcalcAdapter(object):
def __init__(self, dllpath):
self._dllpath = dllpath
self._libc = windll.LoadLibrary(self._dllpath)
def calc_CRC32(self, filename):
new_filename = c_char_p(filename)
return self._libc.calc_CRC32(new_filename)
def calc_MD5(self, filename):
new_filename = c_char_p(filename)
return self._libc.calc_MD5(new_filename)
def calc_SHA1(self, filename):
new_filename = c_char_p(filename)
return self._libc.calc_SHA1(new_filename)
运行界面
图 2. 工具的运行界面
总结
在软件开发过程中同时运用 Python 语言和 C 语言,既能够在加快开发速度的同时,也能够保证软件的运行性能。