def test():
from ctypes import cdll
if os.name == "nt":
print cdll.msvcrt
print cdll.load("msvcrt")
print find_library("msvcrt")
if os.name == "posix":
# find and load_version
print find_library("m")
print find_library("c")
print find_library("bz2")
# getattr
## print cdll.m
## print cdll.bz2
# load
if sys.platform == "darwin":
print cdll.LoadLibrary("libm.dylib")
print cdll.LoadLibrary("libcrypto.dylib")
print cdll.LoadLibrary("libSystem.dylib")
print cdll.LoadLibrary("System.framework/System")
else:
print cdll.LoadLibrary("libm.so")
print cdll.LoadLibrary("libcrypt.so")
print find_library("crypt")
python类LoadLibrary()的实例源码
def test():
from ctypes import cdll
if os.name == "nt":
print(cdll.msvcrt)
print(cdll.load("msvcrt"))
print(find_library("msvcrt"))
if os.name == "posix":
# find and load_version
print(find_library("m"))
print(find_library("c"))
print(find_library("bz2"))
# getattr
## print cdll.m
## print cdll.bz2
# load
if sys.platform == "darwin":
print(cdll.LoadLibrary("libm.dylib"))
print(cdll.LoadLibrary("libcrypto.dylib"))
print(cdll.LoadLibrary("libSystem.dylib"))
print(cdll.LoadLibrary("System.framework/System"))
else:
print(cdll.LoadLibrary("libm.so"))
print(cdll.LoadLibrary("libcrypt.so"))
print(find_library("crypt"))
def check_tk_availability():
"""Check that Tk is installed and available."""
global _tk_unavailable
if _tk_unavailable is None:
_tk_unavailable = False
if sys.platform == 'darwin':
# The Aqua Tk implementations on OS X can abort the process if
# being called in an environment where a window server connection
# cannot be made, for instance when invoked by a buildbot or ssh
# process not running under the same user id as the current console
# user. To avoid that, raise an exception if the window manager
# connection is not available.
from ctypes import cdll, c_int, pointer, Structure
from ctypes.util import find_library
app_services = cdll.LoadLibrary(find_library("ApplicationServices"))
if app_services.CGMainDisplayID() == 0:
_tk_unavailable = "cannot run without OS X window manager"
else:
class ProcessSerialNumber(Structure):
_fields_ = [("highLongOfPSN", c_int),
("lowLongOfPSN", c_int)]
psn = ProcessSerialNumber()
psn_p = pointer(psn)
if ( (app_services.GetCurrentProcess(psn_p) < 0) or
(app_services.SetFrontProcess(psn_p) < 0) ):
_tk_unavailable = "cannot run without OS X gui process"
else: # not OS X
import Tkinter
try:
Tkinter.Button()
except Tkinter.TclError as msg:
# assuming tk is not available
_tk_unavailable = "tk not available: %s" % msg
if _tk_unavailable:
raise unittest.SkipTest(_tk_unavailable)
return
def test():
from ctypes import cdll
if os.name == "nt":
print cdll.msvcrt
print cdll.load("msvcrt")
print find_library("msvcrt")
if os.name == "posix":
# find and load_version
print find_library("m")
print find_library("c")
print find_library("bz2")
# getattr
## print cdll.m
## print cdll.bz2
# load
if sys.platform == "darwin":
print cdll.LoadLibrary("libm.dylib")
print cdll.LoadLibrary("libcrypto.dylib")
print cdll.LoadLibrary("libSystem.dylib")
print cdll.LoadLibrary("System.framework/System")
else:
print cdll.LoadLibrary("libm.so")
print cdll.LoadLibrary("libcrypt.so")
print find_library("crypt")
def test():
from ctypes import cdll
if os.name == "nt":
print cdll.msvcrt
print cdll.load("msvcrt")
print find_library("msvcrt")
if os.name == "posix":
# find and load_version
print find_library("m")
print find_library("c")
print find_library("bz2")
# getattr
## print cdll.m
## print cdll.bz2
# load
if sys.platform == "darwin":
print cdll.LoadLibrary("libm.dylib")
print cdll.LoadLibrary("libcrypto.dylib")
print cdll.LoadLibrary("libSystem.dylib")
print cdll.LoadLibrary("System.framework/System")
else:
print cdll.LoadLibrary("libm.so")
print cdll.LoadLibrary("libcrypt.so")
print find_library("crypt")
def test():
from ctypes import cdll
if os.name == "nt":
print(cdll.msvcrt)
print(cdll.load("msvcrt"))
print(find_library("msvcrt"))
if os.name == "posix":
# find and load_version
print(find_library("m"))
print(find_library("c"))
print(find_library("bz2"))
# getattr
## print cdll.m
## print cdll.bz2
# load
if sys.platform == "darwin":
print(cdll.LoadLibrary("libm.dylib"))
print(cdll.LoadLibrary("libcrypto.dylib"))
print(cdll.LoadLibrary("libSystem.dylib"))
print(cdll.LoadLibrary("System.framework/System"))
else:
print(cdll.LoadLibrary("libm.so"))
print(cdll.LoadLibrary("libcrypt.so"))
print(find_library("crypt"))
def __init__(self, debug=""):
self.dll = cdll.LoadLibrary("libaravis-0.4.so")
if debug:
self.dll.arv_debug_enable(debug)
self.g = cdll.LoadLibrary("libgobject-2.0.so")
self.g.g_type_init(None)
self.gt = cdll.LoadLibrary("libgthread-2.0.so")
self.gt.g_thread_init(None)
self.dll.arv_debug_enable(None)
def test():
from ctypes import cdll
if os.name == "nt":
print cdll.msvcrt
print cdll.load("msvcrt")
print find_library("msvcrt")
if os.name == "posix":
# find and load_version
print find_library("m")
print find_library("c")
print find_library("bz2")
# getattr
## print cdll.m
## print cdll.bz2
# load
if sys.platform == "darwin":
print cdll.LoadLibrary("libm.dylib")
print cdll.LoadLibrary("libcrypto.dylib")
print cdll.LoadLibrary("libSystem.dylib")
print cdll.LoadLibrary("System.framework/System")
else:
print cdll.LoadLibrary("libm.so")
print cdll.LoadLibrary("libcrypt.so")
print find_library("crypt")
def suppress_alsa_errors():
asound = cdll.LoadLibrary('libasound.so')
asound.snd_lib_error_set_handler(C_ERROR_HANDLER)
yield
asound.snd_lib_error_set_handler(None)
def test():
from ctypes import cdll
if os.name == "nt":
print(cdll.msvcrt)
print(cdll.load("msvcrt"))
print(find_library("msvcrt"))
if os.name == "posix":
# find and load_version
print(find_library("m"))
print(find_library("c"))
print(find_library("bz2"))
# getattr
## print cdll.m
## print cdll.bz2
# load
if sys.platform == "darwin":
print(cdll.LoadLibrary("libm.dylib"))
print(cdll.LoadLibrary("libcrypto.dylib"))
print(cdll.LoadLibrary("libSystem.dylib"))
print(cdll.LoadLibrary("System.framework/System"))
else:
print(cdll.LoadLibrary("libm.so"))
print(cdll.LoadLibrary("libcrypt.so"))
print(find_library("crypt"))
def set_proc_name(newname):
from ctypes import cdll, byref, create_string_buffer
libc = cdll.LoadLibrary('libc.so.6')
buff = create_string_buffer(len(newname)+1)
buff.value = newname.encode("ascii")
libc.prctl(15, byref(buff), 0, 0, 0)
def ssdeepMe(path):
# Load ssdeep lib
ssdeepdll = cdll.LoadLibrary(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'fuzzy.dll'))
if path is not None:
result = create_string_buffer(FUZZY_MAX_RESULT)
ssdeepdll.fuzzy_hash_filename(path, result)
return result.value
else:
print("Ssdeep: provide a file path")
return ""
def set_process_name(name: str) -> bool:
"""Set process name and cpu priority."""
name = str(name).lower().strip()
try:
libc = cdll.LoadLibrary("libc.so.6") # set process name
buff = create_string_buffer(len(name) + 1)
buff.value = bytes(name.encode("utf-8"))
libc.prctl(15, byref(buff), 0, 0, 0)
except Exception as error:
log.warning(error)
return False # this may fail on windows and its normal, so be silent.
else:
log.debug(f"Process with PID {os.getpid()} Name set to: {name}.")
return True
def __init__(self):
# make log dir for api log
# logdir = os.path.join(sys.path[0], "log")
# if not os.path.exists(logdir):
# os.mkdir(logdir)
dlldir = os.path.join(
os.path.split(os.path.realpath(__file__))[0], "dll")
if not os.path.exists(dlldir):
print('??DLL????')
return
# change work directory
cur_path = os.getcwd()
os.chdir(dlldir)
if isWindowsSystem():
self.h = CDLL("ctp_Quote.dll")
else:
self.h = cdll.LoadLibrary("./ctp_quote.so")
self.h.CreateApi.argtypes = []
self.h.CreateApi.restype = c_void_p
self.h.CreateSpi.argtypes = []
self.h.CreateSpi.restype = c_void_p
self.api = None
self.spi = None
self.nRequestID = 0
self.h.Release.argtypes = [c_void_p]
self.h.Release.restype = c_void_p
self.h.Init.argtypes = [c_void_p]
self.h.Init.restype = c_void_p
self.h.Join.argtypes = [c_void_p]
self.h.Join.restype = c_void_p
self.h.GetTradingDay.argtypes = [c_void_p]
self.h.GetTradingDay.restype = c_void_p
self.h.RegisterFront.argtypes = [c_void_p, c_char_p]
self.h.RegisterFront.restype = c_void_p
self.h.RegisterNameServer.argtypes = [c_void_p, c_char_p]
self.h.RegisterNameServer.restype = c_void_p
self.h.RegisterFensUserInfo.argtypes = [c_void_p, c_void_p]
self.h.RegisterFensUserInfo.restype = c_void_p
self.h.RegisterSpi.argtypes = [c_void_p, c_void_p]
self.h.RegisterSpi.restype = c_void_p
self.h.ReqUserLogin.argtypes = [c_void_p, c_void_p, c_int32]
self.h.ReqUserLogin.restype = c_void_p
self.h.ReqUserLogout.argtypes = [c_void_p, c_void_p, c_int32]
self.h.ReqUserLogout.restype = c_void_p
# restore work directory
os.chdir(cur_path)
def __init__(self):
# make log dir for api log
# logdir = os.path.join(sys.path[0], "log")
# if not os.path.exists(logdir):
# os.mkdir(logdir)
dlldir = os.path.join(
os.path.split(os.path.realpath(__file__))[0], "dll")
if not os.path.exists(dlldir):
print('??DLL????')
return
# change work directory
cur_path = os.getcwd()
os.chdir(dlldir)
if isWindowsSystem():
self.h = CDLL("ctp_Quote.dll")
else:
self.h = cdll.LoadLibrary("./ctp_quote.so")
self.h.CreateApi.argtypes = []
self.h.CreateApi.restype = c_void_p
self.h.CreateSpi.argtypes = []
self.h.CreateSpi.restype = c_void_p
self.api = None
self.spi = None
self.nRequestID = 0
self.h.Release.argtypes = [c_void_p]
self.h.Release.restype = c_void_p
self.h.Init.argtypes = [c_void_p]
self.h.Init.restype = c_void_p
self.h.Join.argtypes = [c_void_p]
self.h.Join.restype = c_void_p
self.h.GetTradingDay.argtypes = [c_void_p]
self.h.GetTradingDay.restype = c_void_p
self.h.RegisterFront.argtypes = [c_void_p, c_char_p]
self.h.RegisterFront.restype = c_void_p
self.h.RegisterNameServer.argtypes = [c_void_p, c_char_p]
self.h.RegisterNameServer.restype = c_void_p
self.h.RegisterFensUserInfo.argtypes = [c_void_p, c_void_p]
self.h.RegisterFensUserInfo.restype = c_void_p
self.h.RegisterSpi.argtypes = [c_void_p, c_void_p]
self.h.RegisterSpi.restype = c_void_p
self.h.ReqUserLogin.argtypes = [c_void_p, c_void_p, c_int32]
self.h.ReqUserLogin.restype = c_void_p
self.h.ReqUserLogout.argtypes = [c_void_p, c_void_p, c_int32]
self.h.ReqUserLogout.restype = c_void_p
# restore work directory
os.chdir(cur_path)
def _CFSetup():
sc = cdll.LoadLibrary(find_library("SystemConfiguration"))
cf = cdll.LoadLibrary(find_library("CoreFoundation"))
sctable = [
('SCDynamicStoreCopyProxies', [c_void_p], c_void_p),
]
cftable = [
('CFArrayGetCount', [c_void_p], c_int64),
('CFArrayGetValueAtIndex', [c_void_p, c_int64], c_void_p),
('CFDictionaryGetValue', [c_void_p, c_void_p], c_void_p),
('CFStringCreateWithCString', [c_void_p, c_char_p, c_int32], c_void_p),
('CFStringGetLength', [c_void_p], c_int32),
('CFStringGetCString', [c_void_p, c_char_p, c_int32, c_int32], c_int32),
('CFNumberGetValue', [c_void_p, c_int, c_void_p], c_int32),
('CFRelease', [c_void_p], None),
]
scconst = [
'kSCPropNetProxiesExceptionsList',
'kSCPropNetProxiesExcludeSimpleHostnames',
'kSCPropNetProxiesHTTPEnable',
'kSCPropNetProxiesHTTPProxy',
'kSCPropNetProxiesHTTPPort',
'kSCPropNetProxiesHTTPSEnable',
'kSCPropNetProxiesHTTPSProxy',
'kSCPropNetProxiesHTTPSPort',
'kSCPropNetProxiesFTPEnable',
'kSCPropNetProxiesFTPProxy',
'kSCPropNetProxiesFTPPort',
'kSCPropNetProxiesGopherEnable',
'kSCPropNetProxiesGopherProxy',
'kSCPropNetProxiesGopherPort',
]
class CFProxy(object):
def __init__(self):
for mod, table in [(sc, sctable), (cf, cftable)]:
for fname, argtypes, restype in table:
func = getattr(mod, fname)
func.argtypes = argtypes
func.restype = restype
setattr(self, fname, func)
for k in scconst:
v = None
try:
v = c_void_p.in_dll(sc, k)
except ValueError:
v = None
setattr(self, k, v)
return CFProxy()