Пример #1
0
 def __init__(self, lib, **kwargs):
     self.lib = lib
     library_loader = LibraryLoader(CDLL)
     library = library_loader.LoadLibrary(lib)
     self.library = library
     for name, args in kwargs.items():
         self.__dict__[name] = CTypesFunction(library, name, *args)
Пример #2
0
 def insertInto(self, parameterSet, myname):
     from ctypes import LibraryLoader, CDLL
     import platform
     loader = LibraryLoader(CDLL)
     ext = platform.uname()[0] == "Darwin" and "dylib" or "so"
     [loader.LoadLibrary("lib%s.%s" % (l, ext)) for l in self.libraries]
     super(LoadPrerequisiteSource, self).insertInto(parameterSet, myname)
Пример #3
0
 def insertInto(self, parameterSet, myname):
     if "libraries_" in self.__dict__:
         from ctypes import LibraryLoader, CDLL
         import platform
         loader = LibraryLoader(CDLL)
         ext = platform.uname()[0] == "Darwin" and "dylib" or "so"
         [loader.LoadLibrary("lib%s.%s" % (l, ext)) for l in self.libraries_]
     super(_Module,self).insertInto(parameterSet,myname)
Пример #4
0
def Haskell(library, exportPattern="{name}_export"):
    hadll = LibraryLoader(HaDLL)
    hadll.exportPattern = exportPattern

    lib = hadll.LoadLibrary(library)
    lib.hs_init(0, 0)
    yield lib
    lib.hs_exit()
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.

# from winbase.h
STDOUT = -11
STDERR = -12

try:
    import ctypes
    from ctypes import LibraryLoader
    windll = LibraryLoader(ctypes.WinDLL)
    from ctypes import wintypes
except (AttributeError, ImportError):
    windll = None
    SetConsoleTextAttribute = lambda *_: None
    winapi_test = lambda *_: None
else:
    from ctypes import byref, Structure, c_char, POINTER

    COORD = wintypes._COORD

    class CONSOLE_SCREEN_BUFFER_INFO(Structure):
        """struct in wincon.h."""
        _fields_ = [
            ("dwSize", COORD),
            ("dwCursorPosition", COORD),
            ("wAttributes", wintypes.WORD),
            ("srWindow", wintypes.SMALL_RECT),
            ("dwMaximumWindowSize", COORD),
        ]

        def __str__(self):
Пример #6
0
import platform
from ctypes import LibraryLoader, WinDLL, WinError, c_int
from ctypes.wintypes import HANDLE
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from ctypes import Structure
    from typing import Any, Callable, Iterable, Iterator, Tuple

windll = LibraryLoader(WinDLL)

INVALID_HANDLE_VALUE = HANDLE(
    -1).value  # copied from cwinsdk.um.handleapi to prevent cyclic imports
S_OK = 0  # copied from cwinsdk.shared.winerror


class CEnum(c_int):
    pass


class WinApiError(OSError):
    pass


def _value_with_length(values):
    # type: (Iterable, ) -> Iterator

    for value in values:
        if hasattr(value, "_fields_"):
            value = dict(_struct2pairs(value))
        elif hasattr(value, "_length_"):
Пример #7
0

@dataclass
class WindowsConsoleFeatures:
    """Windows features available."""

    vt: bool = False
    truecolor: bool = False


try:
    import ctypes
    from ctypes import wintypes
    from ctypes import LibraryLoader

    windll = LibraryLoader(ctypes.WinDLL)  # type: ignore
except (AttributeError, ImportError, ValueError):

    # Fallback if we can't load the Windows DLL
    def get_windows_console_features() -> WindowsConsoleFeatures:
        features = WindowsConsoleFeatures()
        return features

else:

    STDOUT = -11
    ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
    _GetConsoleMode = windll.kernel32.GetConsoleMode
    _GetConsoleMode.argtypes = [wintypes.HANDLE, wintypes.LPDWORD]
    _GetConsoleMode.restype = wintypes.BOOL
Пример #8
0
        def __init__(self, name):
            self.msg = ("Failed to load dynlib/dll %r. "
                        "Most probably this dynlib/dll was not found "
                        "when the application was frozen.") % name
            self.args = (self.msg,)

    class PyInstallerCDLL(ctypes.CDLL):
        def __init__(self, name, *args, **kwargs):
            name = _frozen_name(name)
            try:
                super(PyInstallerCDLL, self).__init__(name, *args, **kwargs)
            except Exception as base_error:
                raise PyInstallerImportError(name)

    ctypes.CDLL = PyInstallerCDLL
    ctypes.cdll = LibraryLoader(PyInstallerCDLL)

    class PyInstallerPyDLL(ctypes.PyDLL):
        def __init__(self, name, *args, **kwargs):
            name = _frozen_name(name)
            try:
                super(PyInstallerPyDLL, self).__init__(name, *args, **kwargs)
            except Exception as base_error:
                raise PyInstallerImportError(name)

    ctypes.PyDLL = PyInstallerPyDLL
    ctypes.pydll = LibraryLoader(PyInstallerPyDLL)

    if sys.platform.startswith('win'):
        class PyInstallerWinDLL(ctypes.WinDLL):
            def __init__(self, name,*args, **kwargs):
Пример #9
0
from . import win32defines, win32structures
from .actionlogger import ActionLogger
from ctypes import c_short
from ctypes import WINFUNCTYPE
from ctypes import c_void_p
from ctypes import c_int
from ctypes import byref
from ctypes import POINTER
from ctypes import c_ubyte
from ctypes import c_size_t

# Quote: "If you want cached libs without polluting ctypes.cdll or
# ctypes.windll, just create your own instance such as
# windll = ctypes.LibraryLoader(ctypes.WinDLL)."
# see https://bugs.python.org/issue22552
windll = LibraryLoader(WinDLL)

SHORT = c_short

CreateBrushIndirect = windll.gdi32.CreateBrushIndirect
CreateBrushIndirect.restype = wintypes.HBRUSH
CreateBrushIndirect.argtypes = [
    c_void_p,
]
CreateDC = windll.gdi32.CreateDCW
CreateDC.restype = wintypes.HDC
CreateDC.argtypes = [
    wintypes.LPCWSTR,
    wintypes.LPCWSTR,
    wintypes.LPCWSTR,
    c_void_p,
Пример #10
0
    """
    def __init__(self, *args, **kwargs):
        super(WinDll, self).__init__(*args, **kwargs)

    def _error_check(self, ret, func, args):
        lasterror = GetLastError()
        errfn = getattr(func, 'errfn', None)
        if errfn:
            # custom error return check function defined
            failed = errfn(ret, args)
        else:
            failed = not ret
        if failed:
            #print '%s failed, lasterror=%x' % (func.__name__, lasterror)
            """
            self._log.critical('call to {0} failed with error {1} ({2})' \
                                '(hr=0x{3:08x})'.format(func.__name__, \
                            lasterror, FormatError(lasterror), \
                            lasterror&0xffffffff))
            """
            raise WinError(lasterror)
        return ret

    def __getitem__(self, name_or_ordinal):
        func = super(WinDll, self).__getitem__(name_or_ordinal)
        func.errcheck = self._error_check
        return func


windll = LibraryLoader(WinDll)
Пример #11
0
           use_last_error=False):  # EXPORT

    return current_session.load_library(dll_name=name,
                                        dll_type='windll',
                                        dll_param={
                                            'mode': mode,
                                            'use_errno': use_errno,
                                            'use_last_error': use_last_error
                                        })


def OleDLL(name,
           mode=DEFAULT_MODE,
           handle=None,
           use_errno=False,
           use_last_error=False):  # EXPORT

    return current_session.load_library(dll_name=name,
                                        dll_type='oledll',
                                        dll_param={
                                            'mode': mode,
                                            'use_errno': use_errno,
                                            'use_last_error': use_last_error
                                        })


# Set up and expose dll library loader objects
cdll = LibraryLoader(CDLL)  # EXPORT
windll = LibraryLoader(WinDLL)  # EXPORT
oledll = LibraryLoader(OleDLL)  # EXPORT
Пример #12
0

def use_color_output():
    return ENABLE_COLOR_OUTPUT


def running_on_win():
    return sys.platform == 'win32'


if sys.platform == 'win32':
    try:
        import ctypes
        from ctypes import LibraryLoader

        windll = LibraryLoader(ctypes.WinDLL)  # mypy: ignore
        from ctypes import wintypes
    except (AttributeError, ImportError):
        ENABLE_COLOR_OUTPUT = False

    else:
        from ctypes import POINTER, Structure, byref

        COORD = wintypes._COORD

        class ConsoleScreenBufferInfo(Structure):
            """struct in wincon.h."""
            _fields_ = [
                ("dwSize", COORD),
                ("dwCursorPosition", COORD),
                ("wAttributes", wintypes.WORD),