我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用ctypes.cdll()。
def _copyWindows(text): GMEM_DDESHARE = 0x2000 CF_UNICODETEXT = 13 d = ctypes.windll # cdll expects 4 more bytes in user32.OpenClipboard(0) if not isinstance(text, text_type): text = text.decode('mbcs') d.user32.OpenClipboard(0) d.user32.EmptyClipboard() hCd = d.kernel32.GlobalAlloc(GMEM_DDESHARE, len(text.encode('utf-16-le')) + 2) pchData = d.kernel32.GlobalLock(hCd) ctypes.cdll.msvcrt.wcscpy(ctypes.c_wchar_p(pchData), text) d.kernel32.GlobalUnlock(hCd) d.user32.SetClipboardData(CF_UNICODETEXT, hCd) d.user32.CloseClipboard()
def load_library(libname, loader_path): if ctypes.__version__ < '1.0.1': import warnings warnings.warn("All features of ctypes interface may not work " \ "with ctypes < 1.0.1") ext = os.path.splitext(libname)[1] if not ext: # Try to load library with platform-specific name, otherwise # default to libname.[so|pyd]. Sometimes, these files are built # erroneously on non-linux platforms. from numpy.distutils.misc_util import get_shared_lib_extension so_ext = get_shared_lib_extension() libname_ext = [libname + so_ext] # mac, windows and linux >= py3.2 shared library and loadable # module have different extensions so try both so_ext2 = get_shared_lib_extension(is_python_ext=True) if not so_ext2 == so_ext: libname_ext.insert(0, libname + so_ext2) else: libname_ext = [libname] loader_path = os.path.abspath(loader_path) if not os.path.isdir(loader_path): libdir = os.path.dirname(loader_path) else: libdir = loader_path for ln in libname_ext: libpath = os.path.join(libdir, ln) if os.path.exists(libpath): try: return ctypes.cdll[libpath] except OSError: ## defective lib file raise ## if no successful return in the libname_ext loop: raise OSError("no file with expected extension")
def on_parent_exit(signame): """ Return a function to be run in a child process which will trigger SIGNAME to be sent when the parent process dies """ signum = getattr(signal, signame) def set_parent_exit_signal(): # http://linux.die.net/man/2/prctl result = cdll['libc.so.6'].prctl(PR_SET_PDEATHSIG, signum) if result != 0: raise PrCtlError('prctl failed with error code %s' % result) return set_parent_exit_signal # ------------------------------------------------ # ------------------------------------------------
def enable_library_ordinals(library_num): idaname = "ida64" if Const.EA64 else "ida" if sys.platform == "win32": dll = ctypes.windll[idaname + ".wll"] elif sys.platform == "linux2": dll = ctypes.cdll["lib" + idaname + ".so"] elif sys.platform == "darwin": dll = ctypes.cdll["lib" + idaname + ".dylib"] else: print "[ERROR] Failed to enable ordinals" return idati = ctypes.POINTER(TypeLibrary.til_t).in_dll(dll, "idati") dll.enable_numbered_types(idati.contents.base[library_num], True)
def _pasteCygwin(): CF_UNICODETEXT = 13 d = ctypes.cdll d.user32.OpenClipboard(0) handle = d.user32.GetClipboardData(CF_UNICODETEXT) data = ctypes.c_wchar_p(handle).value d.user32.CloseClipboard() return data
def _copyCygwin(text): GMEM_DDESHARE = 0x2000 CF_UNICODETEXT = 13 d = ctypes.cdll if not isinstance(text, text_type): text = text.decode('mbcs') d.user32.OpenClipboard(0) d.user32.EmptyClipboard() hCd = d.kernel32.GlobalAlloc(GMEM_DDESHARE, len(text.encode('utf-16-le')) + 2) pchData = d.kernel32.GlobalLock(hCd) ctypes.cdll.msvcrt.wcscpy(ctypes.c_wchar_p(pchData), text) d.kernel32.GlobalUnlock(hCd) d.user32.SetClipboardData(CF_UNICODETEXT, hCd) d.user32.CloseClipboard()
def on_executor_exit(signame): """ Return a function to be run in a child process which will trigger SIGNAME to be sent when the parent process dies """ signum = getattr(signal, signame) def set_parent_exit_signal(): # http://linux.die.net/man/2/prctl PR_SET_PDEATHSIG = 1 result = cdll['libc.so.6'].prctl(PR_SET_PDEATHSIG, signum) if result != 0: raise Exception('prctl failed with error code %s' % result) return set_parent_exit_signal
def test_libpulse_simple_not_loadable(monkeypatch): def LoadLibrary(_): raise OSError('foobar') monkeypatch.setattr(ctypes.cdll, 'LoadLibrary', LoadLibrary) with pytest.raises(ImportError): simple_client._load_libpulse_simple() # Now undo the changes and reload the library properly, otherwise some weird side effects may occur. monkeypatch.undo() simple_client._load_libpulse_simple()
def platform_specific_functions(): # use stddecl on windows, cdecl on all other platforms d = {'library_loader' : ctypes.cdll ,'function_pointer' : ctypes.CFUNCTYPE } if platform.system() in ('Windows', 'Microsoft'): d['library_loader'] = ctypes.windll d['function_pointer'] = ctypes.WINFUNCTYPE return d
def jvm_load(prefer=None): """ Loads the JVM dynamic library. The `prefer` argument works like for `jvm_find`. If the library is already loaded, this produces a warning if `prefer` doesn't match the loaded library, otherwise it does nothing. """ global libjvm, libjvm_type if libjvm is not None: if prefer is not None and prefer != libjvm_type: import warnings warnings.warn('Already loaded JVM "%s", so preferred JVM "%s" is unloadable'%(libjvm_type,prefer)) return jvm,libjvm_type = jvm_find(prefer) libjvm = (ctypes.windll if is_win else ctypes.cdll).LoadLibrary(jvm)
def open_tablebases_native(directory, libgtb=None, LibraryLoader=ctypes.cdll): """ Opens a collection of tablebases for probing using libgtb. In most cases :func:`~chess.gaviota.open_tablebases()` should be used. Use this function only if you do not want to downgrade to pure Python tablebase probing. Raises :exc:`RuntimeError` or :exc:`OSError` when libgtb can not be used. """ libgtb = libgtb or ctypes.util.find_library("gtb") or "libgtb.so.1.0.1" return NativeTablebases(directory, LibraryLoader.LoadLibrary(libgtb))
def open_tablebases(directory=None, libgtb=None, LibraryLoader=ctypes.cdll): """ Opens a collection of tablebases for probing. First native access via the shared library libgtb is tried. You can optionally provide a specific library name or a library loader. The shared library has global state and caches, so only one instance can be open at a time. Second pure Python probing code is tried. """ try: if LibraryLoader: return open_tablebases_native(directory, libgtb, LibraryLoader) except (OSError, RuntimeError) as err: LOGGER.info("Falling back to pure Python tablebases: %r", err) try: import lzma except ImportError: try: from backports import lzma except ImportError: LOGGER.exception("You should install backports.lzma") return PythonTablebases(directory, lzma)