相关疑难解决方法(0)

使用 Python Windows 扩展获取窗口 Z 顺序

有没有办法使用Python Windows 扩展来获取窗口的 z 顺序?或者,有没有办法使用另一个模块来做到这一点?通常的方法是使用GetTopWindowand GetNextWindow,但这些函数都没有出现在win32gui模块中。

目前我正在这样做,但它没有考虑到 Windows 的 z 顺序:

import win32gui
def get_windows():
    def callback(hwnd, lst):
        lst.append(hwnd)
    lst = []
    win32gui.EnumWindows(callback, lst)
    return lst
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想要这样的东西:(这不起作用)

import win32gui
import win32con
def get_windows():
    '''Returns windows in z-order (top first)'''
    lst = []
    top = win32gui.GetTopWindow()
    if top is None: return lst
    lst.append(top)
    while True:
        next = win32gui.GetNextWindow(lst[-1], win32con.GW_HWNDNEXT)
        if next is None: break
        lst.append(next)
    return lst
Run Code Online (Sandbox Code Playgroud)

但是,缺少GetTopWindowGetNextWindow功能,所以我不能。

更新:

我想我寻求帮助的速度有点太快了。我用 …

python winapi pywin32 z-order

6
推荐指数
1
解决办法
1984
查看次数

Python Ctypes - 加载 dll 引发 OSError: [WinError 193] %1 不是有效的 Win32 应用程序

我试图运行一个使用 ctypes 从库中获取函数的 python 代码示例。可以在此处找到示例。我按照说明进行操作,除了一个小的修改外,我还使用了完全相同的代码。我一直在尝试在 Windows 10(64 位)、python 3.7(64 位)上运行它,但收到此错误消息:

Traceback (most recent call last):
  File "C:/Users/gifr9302/PycharmProjects/testpytoc/myfunc.py", line 128, in <module>
    libmyfunc = npct.load_library('myfunc.dll', os.path.dirname(os.path.abspath(__file__)))
  File "C:\Users\gifr9302\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\ctypeslib.py", line 152, in load_library
    return ctypes.cdll[libpath]
  File "C:\Users\gifr9302\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 431, in __getitem__
    return getattr(self, name)
  File "C:\Users\gifr9302\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 426, in __getattr__
    dll = self._dlltype(name)
  File "C:\Users\gifr9302\AppData\Local\Programs\Python\Python37\lib\ctypes\__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 n’est pas une application Win32 valide
Run Code Online (Sandbox Code Playgroud)

翻译:

OSError: [WinError 193] %1 is not …

c python windows dll ctypes

6
推荐指数
1
解决办法
2万
查看次数

ctypes CDLL 默认路径是什么?

我正在尝试使用编译为/usr/local/lib/libName.so但同时运行需要此文件的 python 脚本的库:

from ctypes import CDLL
[...]
__lib = CDLL('libName.so')
Run Code Online (Sandbox Code Playgroud)

我得到:

OSError: libName.so: cannot open shared object file: No such file or directory

所以我想知道我需要在哪里复制 .so 文件,以便这个 CDLL 调用正常工作。

python linux ctypes python-2.7

5
推荐指数
1
解决办法
2142
查看次数

不同操作系统上的 python ctypes 问题

我正在尝试转换 C 函数以供 python 3.6 使用。

代码如下:

lib = ctypes.WinDLL('ftrScanAPI.dll') # provided by fingerprint scanner
class FTRSCAN_IMAGE_SIZE(ctypes.Structure):
    _fields_ = [
    ("nWidth", ctypes.c_int),
    ("nHeight", ctypes.c_int),
    ("nImageSize", ctypes.c_int)
]

print('Open device and get device handle...')
hDevice = lib.ftrScanOpenDevice()
print('handle is', hDevice)
print('Get image size...')
Image_size = FTRSCAN_IMAGE_SIZE(0, 0, 0)
if lib.ftrScanGetImageSize(hDevice, ctypes.byref(Image_size)):
    print('Get image size succeed...')
    print('  W', Image_size.nWidth)
    print('  H', Image_size.nHeight)
    print('  Size', Image_size.nImageSize)
else:
    print('Get image size failed...')
Run Code Online (Sandbox Code Playgroud)

函数定义:

typedef struct FTR_PACKED __FTRSCAN_IMAGE_SIZE {
    int nWidth;
    int nHeight;
    int nImageSize; …
Run Code Online (Sandbox Code Playgroud)

c python windows ctypes python-3.x

5
推荐指数
1
解决办法
2696
查看次数

具有 Ctypes 的独立 CDLL 库实例

我正在尝试使用 ctypes 并两次加载相同的编译 Fortran 库,这样我就有两个独立的实例,这样库包含的任何模块变量都不会存储在相同的内存位置。描述的一般解决方案(例如,此处:https : //mail.python.org/pipermail/python-list/2010-May/575368.html)是提供库的完整路径,而不仅仅是其名称。但是,我无法让它像这样工作。这是一个演示问题的最小工作示例:

测试.f90:

module test
    use iso_c_binding, only: c_int
    implicit none
    integer :: n
contains
    integer(c_int) function get() bind(c, name='get')
        get = n
    end function get

    subroutine set(new_n) bind(c, name='set')
        integer(c_int), intent(in) :: new_n
        n = new_n
    end subroutine set
end module test
Run Code Online (Sandbox Code Playgroud)

测试.py:

import os
from ctypes import cdll, c_int, byref

if __name__ == '__main__':
    lib1 = cdll.LoadLibrary(os.path.abspath('test.so'))
    lib2 = cdll.LoadLibrary(os.path.abspath('test.so'))

    lib1.set(byref(c_int(0)))
    lib2.set(byref(c_int(1)))

    print(lib1.get())
Run Code Online (Sandbox Code Playgroud)

Fortran 库是使用以下命令编译的:

gfortran -shared -fPIC -o test.so test.f90 …
Run Code Online (Sandbox Code Playgroud)

python fortran ctypes

5
推荐指数
1
解决办法
697
查看次数

与pywinauto一起使用kivy时ctypes.ArgumentError

我有一个kivy应用程序,可以使用pywinauto模块与其他窗口进行交互。该应用程序在Linux(未使用pywinauto)上运行良好,但是在Windows中,我收到以下错误消息,该应用程序甚至无法启动:

C:\Program Files (x86)\Python36_64\lib\site-packages\pywinauto\__init__.py:80: UserWarning: Revert to STA COM threading mode
    warnings.warn("Revert to STA COM threading mode", UserWarning)
[INFO   ] [GL          ] NPOT texture support is available
[INFO   ] [Base        ] Start application main loop
Traceback (most recent call last):
File ".\application.py", line 368, in <module>
    Application().run()
File "C:\Program Files (x86)\Python36_64\lib\site-packages\kivy\app.py", line 826, in run
    runTouchApp()
File "C:\Program Files (x86)\Python36_64\lib\site-packages\kivy\base.py", line 477, in runTouchApp
    EventLoop.start()
File "C:\Program Files (x86)\Python36_64\lib\site-packages\kivy\base.py", line 164, in start
    provider.start()
File "C:\Program Files (x86)\Python36_64\lib\site-packages\kivy\input\providers\wm_touch.py", line 68, …
Run Code Online (Sandbox Code Playgroud)

python ctypes pywinauto kivy

5
推荐指数
1
解决办法
482
查看次数

如何将二维数组从 Python 传递给 C?

我正在尝试使用 ctypes 将一个二维数组从 Python 传递到 C。数组数据类型是 uint16。我写了一个简单的代码来理解它是如何工作的:

C:

#include <stdint.h>

__declspec(dllexport) uint16_t Test(uint16_t **arr)
{
     return (arr[5][5]);
}

Run Code Online (Sandbox Code Playgroud)

Python:

import numpy as np
from ctypes import cdll
import ctypes
from numpy.ctypeslib import ndpointer

_p = ndpointer(dtype=np.uint16, ndim=2, shape=(10, 10), flags='C')
mydll = cdll.LoadLibrary("mydll.dll")
_func = mydll.Test
_func.argtypes = [_p]
_func.restypes = ctypes.c_uint16

data = np.empty([10, 10], dtype=np.uint16)
data[5, 5] = 5
print(_func(data))
Run Code Online (Sandbox Code Playgroud)

我收到 OSError:访问冲突读数 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 我做错了什么,我该如何解决?

c python arrays ctypes pointers

5
推荐指数
1
解决办法
1284
查看次数

尽管我在 python ctypes 中设置了信号处理程序,但不会调用它

我已经尝试使用 sigaction 和 ctypes 设置信号处理程序。(我知道它可以在 python 中使用信号模块,但我想尝试学习。)

当我将 SIGTERM 发送到这个进程,但它没有调用我设置的处理程序时,只打印“终止”。为什么它不调用处理程序?

我使用 Ubuntu 19.10 和 Python 3.7.5 x64。

import ctypes
from ctypes import *
from ctypes.util import *
from os import getpid


class sigset_t(Structure):
    __fields__ = [
        ("__val",               c_ulong*16)        
    ]

class sigval_t(Union):
    __fields__ = [
        ("sival_int",           c_int),
        ("sival_ptr",           c_void_p)
    ]



class siginfo_t(Structure):
    __fields__ = [
        ("si_signo",            c_int),
        ("si_errno",            c_int),
        ("si_code",             c_int),
        ("si_trapno",           c_int),
        ("si_pid",              c_uint),
        ("si_status",           c_int),
        ("si_utime",            c_long),
        ("si_stime",            c_long),
        ("si_value",            sigval_t),
        ("si_int",              c_int),
        ("si_ptr",              c_void_p),
        ("si_overrun",          c_int),
        ("si_timerid",          c_int), …
Run Code Online (Sandbox Code Playgroud)

python linux ubuntu ctypes signals

5
推荐指数
1
解决办法
248
查看次数

Python 以编程方式更改控制台字体大小

我发现下面的代码应该以编程方式更改控制台字体大小。我在 Windows 10 上。

但是,无论我调整什么值,我似乎都无法控制字体大小,而且由于某种原因,运行此脚本时打开的控制台非常宽。

我不知道 ctypes 是如何工作的 - 我想要的只是从 Python 内部修改控制台字体的大小。

任何实际的工作解决方案?

import ctypes

LF_FACESIZE = 32
STD_OUTPUT_HANDLE = -11

class COORD(ctypes.Structure):
    _fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]

class CONSOLE_FONT_INFOEX(ctypes.Structure):
    _fields_ = [("cbSize", ctypes.c_ulong),
                ("nFont", ctypes.c_ulong),
                ("dwFontSize", COORD),
                ("FontFamily", ctypes.c_uint),
                ("FontWeight", ctypes.c_uint),
                ("FaceName", ctypes.c_wchar * LF_FACESIZE)]

font = CONSOLE_FONT_INFOEX()
font.cbSize = ctypes.sizeof(CONSOLE_FONT_INFOEX)
font.nFont = 12
font.dwFontSize.X = 11
font.dwFontSize.Y = 18
font.FontFamily = 54
font.FontWeight = 400
font.FaceName = "Lucida Console"

handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
ctypes.windll.kernel32.SetCurrentConsoleFontEx(
        handle, ctypes.c_long(False), ctypes.pointer(font))


print("Foo")
Run Code Online (Sandbox Code Playgroud)

python fonts ctypes cmd

4
推荐指数
1
解决办法
1万
查看次数

如何为 Python 打包的 libcrypto 和 libssl 启用 FIPS 模式?

我有一个 Python 应用程序,它与 Python 和 Libcrypto 以及 LibSSL 共享对象一起打包。该应用程序是使用 Openssl Fips Module 2.0 构建的。Python 的请求模块和 urllib3 在后台使用这些共享对象来发出 TLS 请求。

我在构建应用程序的环境中启用了OPENSSL_FIPS标志。现在,如果要检查共享对象是否在我将它们从开发环境中取出并放入另一台机器时启用了 fips 模式,我该怎么做?

如何检查 fips 模式是否启用?如果不是,我如何为这些共享对象启用 fips 模式?

可能有帮助的其他详细信息:

OpenSSL 版本:1.0.2h(从源代码构建)

Fips 模块:2.0.12(从源代码构建)

蟒蛇:3.6

操作系统:Ubuntu 16.04 LTS

如果需要任何其他详细信息,请告诉我。

谢谢!

python ssl ctypes openssl python-3.x

3
推荐指数
1
解决办法
8053
查看次数

标签 统计

python ×10

ctypes ×9

c ×3

linux ×2

python-3.x ×2

windows ×2

arrays ×1

cmd ×1

dll ×1

fonts ×1

fortran ×1

kivy ×1

openssl ×1

pointers ×1

python-2.7 ×1

pywin32 ×1

pywinauto ×1

signals ×1

ssl ×1

ubuntu ×1

winapi ×1

z-order ×1