Python ctypes.byref() Examples

The following are 30 code examples for showing how to use ctypes.byref(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module ctypes , or try the search function .

Example 1
Project: multibootusb   Author: mbusb   File: gen.py    License: GNU General Public License v2.0 9 votes vote down vote up
def windowsRam(self):
        """
        Uses Windows API to check RAM
        """
        kernel32 = ctypes.windll.kernel32
        c_ulong = ctypes.c_ulong

        class MEMORYSTATUS(ctypes.Structure):
            _fields_ = [
                ("dwLength", c_ulong),
                ("dwMemoryLoad", c_ulong),
                ("dwTotalPhys", c_ulong),
                ("dwAvailPhys", c_ulong),
                ("dwTotalPageFile", c_ulong),
                ("dwAvailPageFile", c_ulong),
                ("dwTotalVirtual", c_ulong),
                ("dwAvailVirtual", c_ulong)
            ]

        memoryStatus = MEMORYSTATUS()
        memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
        kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))

        return int(memoryStatus.dwTotalPhys / 1024 ** 2) 
Example 2
Project: BASS   Author: Cisco-Talos   File: lcs.py    License: GNU General Public License v2.0 7 votes vote down vote up
def hamming_klcs(seqs):
    """
        Implementation of k-LCS as described in Christian Blichmann's thesis "Automatisierte Signaturgenerierung fuer Malware-Staemme" on page 52.
        This algorithm will not forcibly find THE longest common subsequence among all sequences, as the subsequence returned by the 2-LCS algorithm
        might not be the optimal one from the set of longest common subsequences.
        :see: https://static.googleusercontent.com/media/www.zynamics.com/en//downloads/blichmann-christian--diplomarbeit--final.pdf
        :param seqs: List of sequences
        :return: A shared subsequence between the input sequences. Not necessarily the longest one, and only one of several that might exist.
    """
    c_seqs_type = c_char_p * len(seqs)
    c_seqs = c_seqs_type()
    c_seqs[:] = seqs
    c_lens_type = c_size_t * len(seqs)
    c_lens = c_lens_type()
    c_lens[:] = [len(seq) for seq in seqs]
    result = create_string_buffer("\0" * min(len(seq) for seq in seqs))
    result_len = c_size_t(len(result))
    ret = _lib.hamming_klcs_c(c_seqs, c_lens, len(seqs), result, byref(result_len))
    if ret == 0:
        return result[:result_len.value]
    else:
        raise RuntimeError("lcs returned error code %d" % ret) 
Example 3
Project: vergeml   Author: mme   File: display.py    License: MIT License 6 votes vote down vote up
def try_enable_ansi():
            """Try enabling ANSI colors
            https://stackoverflow.com/questions/44482505/setconsolemode-returning-false-when-enabling-ansi-color-under-windows-10"""
            lpMode = wintypes.DWORD()
            handle = WINAPI._GetStdHandle(WINAPI._STDOUT)
            if WINAPI._GetConsoleMode(handle, ctypes.byref(lpMode)):
               
                if not WINAPI._SetConsoleMode(handle, lpMode.value | WINAPI._ENABLE_VIRTUAL_TERMINAL_PROCESSING):
                    return False
            else:
                return False
            
            lpMode = wintypes.DWORD()
            handle = WINAPI._GetStdHandle(WINAPI._STDERR)
            if WINAPI._GetConsoleMode(handle, ctypes.byref(lpMode)):
                if not WINAPI._SetConsoleMode(handle, lpMode.value | WINAPI._ENABLE_VIRTUAL_TERMINAL_PROCESSING):
                    return False
            else:
                return False
            
            return True 
Example 4
Project: BASS   Author: Cisco-Talos   File: lcs.py    License: GNU General Public License v2.0 6 votes vote down vote up
def lcs(s, t):
    """
        Calculate the longest common subsequence between two sequences in O(min(len(x), len(y))) space and O(len(x) * len(y)) time.
        Implemented in C++.
        Since only one instance from the set of longest common subsequences is returned,
        the algorithm has the unpleasing property of not being commutative (i.e., changing
        the input vectors changes the result).
        :see: https://en.wikipedia.org/wiki/Hirschberg%27s_algorithm
        :param x: First input sequence.
        :param y: Second input sequence.
        :return: LCS(x, y)
    """
    result = create_string_buffer("\0" * min(len(s), len(t)))
    result_len = c_size_t(len(result))
    if isinstance(s, list):
        s = "".join(s)
    if isinstance(t, list):
        t = "".join(t)
    ret = _lib.hirschberg_lcs(s, len(s), t, len(t), result, byref(result_len))
    if ret == 0:
        return result[:result_len.value]
    else:
        raise RuntimeError("lcs returned error code %d" % ret) 
Example 5
Project: cWMI   Author: fireeye   File: wmi.py    License: Apache License 2.0 6 votes vote down vote up
def Put(self, name, val, flavor):
        prototype = ctypes.WINFUNCTYPE(HRESULT,
                                       wintypes.LPCWSTR,
                                       ctypes.POINTER(winapi.VARIANT),
                                       ctypes.c_long)

        paramflags = ((_In_, 'wszName'),
                      (_In_, 'pVal'),
                      (_In_, 'lFlavor'),
                      )

        _Put = prototype(IWbemQualifierSet_Put_Idx,
                         'Put',
                         paramflags)
        _Put.errcheck = winapi.RAISE_NON_ZERO_ERR
        _Put(self.this,
             name,
             ctypes.byref(val) if val else None,
             flavor
             ) 
Example 6
Project: cWMI   Author: fireeye   File: wmi.py    License: Apache License 2.0 6 votes vote down vote up
def Put(self, name, flags, val, type_param):
        prototype = ctypes.WINFUNCTYPE(HRESULT,
                                       wintypes.LPCWSTR,
                                       ctypes.c_long,
                                       ctypes.POINTER(winapi.VARIANT),
                                       CIMTYPE)

        paramflags = ((_In_, 'wszName'),
                      (_In_, 'lFlags'),
                      (_In_, 'pVal'),
                      (_In_, 'Type'),
                      )

        _Put = prototype(IWbemClassObject_Put_Idx,
                         'Put',
                         paramflags)
        _Put.errcheck = winapi.RAISE_NON_ZERO_ERR
        _Put(self.this,
             name,
             flags,
             ctypes.byref(val) if val else None,
             type_param
             ) 
Example 7
Project: cWMI   Author: fireeye   File: wmi.py    License: Apache License 2.0 6 votes vote down vote up
def GetNames(self, qualifier_name, flags, qualifier_val):
        prototype = ctypes.WINFUNCTYPE(HRESULT,
                                       wintypes.LPCWSTR,
                                       ctypes.c_long,
                                       ctypes.POINTER(winapi.VARIANT),
                                       ctypes.POINTER(wintypes.LPVOID))

        paramflags = ((_In_, 'wszQualifierName'),
                      (_In_, 'lFlags'),
                      (_In_, 'pQualifierVal'),
                      (_Out_, 'pNames'),
                      )

        _GetNames = prototype(IWbemClassObject_GetNames_Idx,
                              'GetNames',
                              paramflags)
        _GetNames.errcheck = winapi.RAISE_NON_ZERO_ERR
        return_obj = _GetNames(self.this,
                               qualifier_name,
                               flags,
                               ctypes.byref(qualifier_val) if qualifier_val else None
                               )
        return_obj = ctypes.cast(wintypes.LPVOID(return_obj), ctypes.POINTER(winapi.SAFEARRAY))
        return return_obj 
Example 8
Project: cWMI   Author: fireeye   File: wmi.py    License: Apache License 2.0 6 votes vote down vote up
def SetValue(self, name, flags, value):
        prototype = ctypes.WINFUNCTYPE(HRESULT,
                                       wintypes.LPCWSTR,
                                       ctypes.c_long,
                                       ctypes.POINTER(winapi.VARIANT))

        paramflags = ((_In_, 'wszName'),
                      (_In_, 'lFlags'),
                      (_In_, 'pValue'),
                      )

        _SetValue = prototype(IWbemContext_SetValue_Idx,
                              'SetValue',
                              paramflags)
        _SetValue.errcheck = winapi.RAISE_NON_ZERO_ERR
        _SetValue(self.this,
                  name,
                  flags,
                  ctypes.byref(value) if value else None
                  ) 
Example 9
Project: cWMI   Author: fireeye   File: com.py    License: Apache License 2.0 6 votes vote down vote up
def QueryInterface(self, riid):
        prototype = ctypes.WINFUNCTYPE(HRESULT,
                                       ctypes.POINTER(winapi.GUID),
                                       ctypes.POINTER(wintypes.LPVOID))

        paramflags = ((_In_, 'riid'),
                      (_Out_, 'ppvObject', ctypes.pointer(wintypes.LPVOID(None)))
                      )

        _QueryInterface = prototype(IUnknown_QueryInterface_Idx,
                                    'QueryInterface',
                                    paramflags)
        _QueryInterface.errcheck = winapi.RAISE_NON_ZERO_ERR
        return_ptr = _QueryInterface(self.this,
                                     ctypes.byref(riid))
        return IUnknown(return_ptr.contents) 
Example 10
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: opencv.py    License: Apache License 2.0 6 votes vote down vote up
def imdecode(str_img, flag=1):
    """Decode image from str buffer.
    Wrapper for cv2.imdecode that uses mx.nd.NDArray

    Parameters
    ----------
    str_img : str
        str buffer read from image file
    flag : int
        same as flag for cv2.imdecode
    Returns
    -------
    img : NDArray
        decoded image in (width, height, channels)
        with BGR color channel order
    """
    hdl = NDArrayHandle()
    check_call(_LIB.MXCVImdecode(ctypes.c_char_p(str_img),
                                 mx_uint(len(str_img)),
                                 flag, ctypes.byref(hdl)))
    return mx.nd.NDArray(hdl) 
Example 11
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: opencv.py    License: Apache License 2.0 6 votes vote down vote up
def resize(src, size, interpolation=cv2.INTER_LINEAR):
    """Decode image from str buffer.
    Wrapper for cv2.imresize that uses mx.nd.NDArray

    Parameters
    ----------
    src : NDArray
        image in (width, height, channels)
    size : tuple
        target size in (width, height)
    interpolation : int
        same as interpolation for cv2.imresize

    Returns
    -------
    img : NDArray
        resized image
    """
    hdl = NDArrayHandle()
    check_call(_LIB.MXCVResize(src.handle, mx_uint(size[0]), mx_uint(size[1]),
                               interpolation, ctypes.byref(hdl)))
    return mx.nd.NDArray(hdl) 
Example 12
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: opencv.py    License: Apache License 2.0 6 votes vote down vote up
def copyMakeBorder(src, top, bot, left, right, border_type=cv2.BORDER_CONSTANT, value=0):
    """Pad image border
    Wrapper for cv2.copyMakeBorder that uses mx.nd.NDArray

    Parameters
    ----------
    src : NDArray
        Image in (width, height, channels).
        Others are the same with cv2.copyMakeBorder

    Returns
    -------
    img : NDArray
        padded image
    """
    hdl = NDArrayHandle()
    check_call(_LIB.MXCVcopyMakeBorder(src.handle, ctypes.c_int(top), ctypes.c_int(bot),
                                       ctypes.c_int(left), ctypes.c_int(right),
                                       ctypes.c_int(border_type), ctypes.c_double(value),
                                       ctypes.byref(hdl)))
    return mx.nd.NDArray(hdl) 
Example 13
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: engine.py    License: Apache License 2.0 6 votes vote down vote up
def set_bulk_size(size):
    """Set size limit on bulk execution.

    Bulk execution bundles many operators to run together.
    This can improve performance when running a lot of small
    operators sequentially.

    Parameters
    ----------
    size : int
        Maximum number of operators that can be bundled in a bulk.

    Returns
    -------
    int
        Previous bulk size.
    """
    prev = ctypes.c_int()
    check_call(_LIB.MXEngineSetBulkSize(
        ctypes.c_int(size), ctypes.byref(prev)))
    return prev.value 
Example 14
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: recordio.py    License: Apache License 2.0 6 votes vote down vote up
def tell(self):
        """Returns the current position of write head.

        Example usage:
        ----------
        >>> record = mx.recordio.MXIndexedRecordIO('tmp.idx', 'tmp.rec', 'w')
        >>> print(record.tell())
        0
        >>> for i in range(5):
        ...     record.write_idx(i, 'record_%d'%i)
        ...     print(record.tell())
        16
        32
        48
        64
        80
        """
        assert self.writable
        pos = ctypes.c_size_t()
        check_call(_LIB.MXRecordIOWriterTell(self.handle, ctypes.byref(pos)))
        return pos.value 
Example 15
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: context.py    License: Apache License 2.0 6 votes vote down vote up
def num_gpus():
    """Query CUDA for the number of GPUs present.

    Raises
    ------
    Will raise an exception on any CUDA error.

    Returns
    -------
    count : int
        The number of GPUs.

    """
    count = ctypes.c_int()
    check_call(_LIB.MXGetGPUCount(ctypes.byref(count)))
    return count.value 
Example 16
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    License: Apache License 2.0 6 votes vote down vote up
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t):
    """Return a new handle with specified shape and context.

    Empty handle is only used to hold results.

    Returns
    -------
    handle
        A new empty `NDArray` handle.
    """
    hdl = NDArrayHandle()
    check_call(_LIB.MXNDArrayCreateEx(
        c_array_buf(mx_uint, native_array('I', shape)),
        mx_uint(len(shape)),
        ctypes.c_int(ctx.device_typeid),
        ctypes.c_int(ctx.device_id),
        ctypes.c_int(int(delay_alloc)),
        ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])),
        ctypes.byref(hdl)))
    return hdl 
Example 17
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    License: Apache License 2.0 6 votes vote down vote up
def shape(self):
        """Tuple of array dimensions.

        Examples
        --------
        >>> x = mx.nd.array([1, 2, 3, 4])
        >>> x.shape
        (4L,)
        >>> y = mx.nd.zeros((2, 3, 4))
        >>> y.shape
        (2L, 3L, 4L)
        """
        ndim = mx_uint()
        pdata = ctypes.POINTER(mx_uint)()
        check_call(_LIB.MXNDArrayGetShape(
            self.handle, ctypes.byref(ndim), ctypes.byref(pdata)))
        return tuple(pdata[:ndim.value]) # pylint: disable=invalid-slice-index 
Example 18
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    License: Apache License 2.0 6 votes vote down vote up
def context(self):
        """Device context of the array.

        Examples
        --------
        >>> x = mx.nd.array([1, 2, 3, 4])
        >>> x.context
        cpu(0)
        >>> type(x.context)
        <class 'mxnet.context.Context'>
        >>> y = mx.nd.zeros((2,3), mx.gpu(0))
        >>> y.context
        gpu(0)
        """
        dev_typeid = ctypes.c_int()
        dev_id = ctypes.c_int()
        check_call(_LIB.MXNDArrayGetContext(
            self.handle, ctypes.byref(dev_typeid), ctypes.byref(dev_id)))
        return Context(Context.devtype2str[dev_typeid.value], dev_id.value) 
Example 19
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: ndarray.py    License: Apache License 2.0 6 votes vote down vote up
def dtype(self):
        """Data-type of the array's elements.

        Returns
        -------
        numpy.dtype
            This NDArray's data type.

        Examples
        --------
        >>> x = mx.nd.zeros((2,3))
        >>> x.dtype
        <type 'numpy.float32'>
        >>> y = mx.nd.zeros((2,3), dtype='int32')
        >>> y.dtype
        <type 'numpy.int32'>
        """
        mx_dtype = ctypes.c_int()
        check_call(_LIB.MXNDArrayGetDType(
            self.handle, ctypes.byref(mx_dtype)))
        return _DTYPE_MX_TO_NP[mx_dtype.value] 
Example 20
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: symbol.py    License: Apache License 2.0 6 votes vote down vote up
def __deepcopy__(self, _):
        """Returns a deep copy of the input object.

        This function returns a deep copy of the input object including the current state
        of all its parameters such as weights, biases, etc.

        Any changes made to the deep copy do not reflect in the original object.

        Example
        -------
        >>> import copy
        >>> data = mx.sym.Variable('data')
        >>> data_1 = copy.deepcopy(data)
        >>> data_1 = 2*data
        >>> data_1.tojson()
        >>> data_1 is data    # Data got modified
        False
        """
        handle = SymbolHandle()
        check_call(_LIB.MXSymbolCopy(self.handle,
                                     ctypes.byref(handle)))
        return Symbol(handle) 
Example 21
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: symbol.py    License: Apache License 2.0 6 votes vote down vote up
def name(self):
        """Gets name string from the symbol, this function only works for non-grouped symbol.

        Returns
        -------
        value : str
            The name of this symbol, returns ``None`` for grouped symbol.
        """
        ret = ctypes.c_char_p()
        success = ctypes.c_int()
        check_call(_LIB.MXSymbolGetName(
            self.handle, ctypes.byref(ret), ctypes.byref(success)))
        if success.value != 0:
            return py_str(ret.value)
        else:
            return None 
Example 22
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: symbol.py    License: Apache License 2.0 6 votes vote down vote up
def list_attr(self, recursive=False):
        """Gets all attributes from the symbol.

        Example
        -------
        >>> data = mx.sym.Variable('data', attr={'mood': 'angry'})
        >>> data.list_attr()
        {'mood': 'angry'}

        Returns
        -------
        ret : Dict of str to str
            A dictionary mapping attribute keys to values.
        """
        if recursive:
            raise DeprecationWarning("Symbol.list_attr with recursive=True has been deprecated. "
                                     "Please use attr_dict instead.")
        size = mx_uint()
        pairs = ctypes.POINTER(ctypes.c_char_p)()
        f_handle = _LIB.MXSymbolListAttrShallow
        check_call(f_handle(self.handle, ctypes.byref(size), ctypes.byref(pairs)))
        return {py_str(pairs[i * 2]): py_str(pairs[i * 2 + 1]) for i in range(size.value)} 
Example 23
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: symbol.py    License: Apache License 2.0 6 votes vote down vote up
def list_arguments(self):
        """Lists all the arguments in the symbol.

        Example
        -------
        >>> a = mx.sym.var('a')
        >>> b = mx.sym.var('b')
        >>> c = a + b
        >>> c.list_arguments
        ['a', 'b']

        Returns
        -------
        args : list of string
            List containing the names of all the arguments required to compute the symbol.
        """
        size = ctypes.c_uint()
        sarr = ctypes.POINTER(ctypes.c_char_p)()
        check_call(_LIB.MXSymbolListArguments(
            self.handle, ctypes.byref(size), ctypes.byref(sarr)))
        return [py_str(sarr[i]) for i in range(size.value)] 
Example 24
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: symbol.py    License: Apache License 2.0 6 votes vote down vote up
def __len__(self):
        """Get number of outputs for the symbol.

        Example
        -------
        >>> a = mx.sym.var('a')
        >>> b = mx.sym.var('b')
        >>> c = a + b
        >>> len(c)

        Returns
        -------
        len(self): Number of outputs
            Number of outputs
        """
        output_count = mx_uint()
        check_call(_LIB.MXSymbolGetNumOutputs(self.handle, ctypes.byref(output_count)))
        return output_count.value 
Example 25
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: symbol.py    License: Apache License 2.0 6 votes vote down vote up
def list_inputs(self):
        """Lists all arguments and auxiliary states of this Symbol.

        Returns
        -------
        inputs : list of str
            List of all inputs.

        Examples
        --------
        >>> bn = mx.sym.BatchNorm(name='bn')
        >>> bn.list_arguments()
        ['bn_data', 'bn_gamma', 'bn_beta']
        >>> bn.list_auxiliary_states()
        ['bn_moving_mean', 'bn_moving_var']
        >>> bn.list_inputs()
        ['bn_data', 'bn_gamma', 'bn_beta', 'bn_moving_mean', 'bn_moving_var']
        """
        size = ctypes.c_uint()
        sarr = ctypes.POINTER(ctypes.c_char_p)()
        check_call(_LIB.NNSymbolListInputNames(
            self.handle, 0, ctypes.byref(size), ctypes.byref(sarr)))
        return [py_str(sarr[i]) for i in range(size.value)] 
Example 26
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: symbol.py    License: Apache License 2.0 6 votes vote down vote up
def get_backend_symbol(self, backend):
        """Return symbol for target backend.

        Parameters
        ----------
        backend : str
            The backend names.

        Returns
        -------
        out : Symbol
            The created Symbol for target backend.
        """
        out = SymbolHandle()
        check_call(_LIB.MXGenBackendSubgraph(self.handle, c_str(backend), ctypes.byref(out)))
        return Symbol(out) 
Example 27
Project: vergeml   Author: mme   File: display.py    License: MIT License 5 votes vote down vote up
def winapi_test():
            def _winapi_test(handle):
                csbi = CONSOLE_SCREEN_BUFFER_INFO()
                success = WINAPI._GetConsoleScreenBufferInfo(
                    handle, byref(csbi))
                return bool(success)

            return any(_winapi_test(h) for h in
                    (WINAPI._GetStdHandle(WINAPI._STDOUT), WINAPI._GetStdHandle(WINAPI._STDERR))) 
Example 28
Project: vergeml   Author: mme   File: display.py    License: MIT License 5 votes vote down vote up
def _terminal_size(handle):
            csbi = CONSOLE_SCREEN_BUFFER_INFO()
            if not WINAPI._GetConsoleScreenBufferInfo(handle, byref(csbi)):
                raise ctypes.WinError()  # Subclass of OSError.
            else:
                columns = csbi.srWindow.Right - csbi.srWindow.Left + 1
                rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1
                return columns, rows 
Example 29
Project: vergeml   Author: mme   File: display.py    License: MIT License 5 votes vote down vote up
def get_font():
            handle = WINAPI._GetStdHandle(WINAPI._STDOUT)
            font = CONSOLE_FONT_INFOEX()
            font.cbSize = sizeof(CONSOLE_FONT_INFOEX)
            if not WINAPI._GetCurrentConsoleFontEx(handle, False, byref(font)):
                return None
            else:
                return font.FaceName 
Example 30
Project: RF-Monitor   Author: EarToEarOak   File: receive.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __capture(self, data, _sdr):
        timestamp = time.time()
        dst = ctypes.byref(self._capture, 0)
        ctypes.memmove(dst, data, len(data))

        iq = self.__stream_to_complex(self._capture)
        l, f = psd(iq, BINS, SAMPLE_RATE,
                   scale_by_freq=False,
                   noverlap=-SAMPLES / 64)
        f /= 1e6
        f += self._freq

        event = Event(Events.SCAN_DATA, timestamp=timestamp, l=l, f=f)
        post_event(self._eventHandler, event)