我在使用 ctypes 将此 C# 代码转换为 python 时遇到问题。此代码用于隐藏 Windows 7 启动球。这是link .
[DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(
IntPtr parentHwnd,
IntPtr childAfterHwnd,
IntPtr className,
string windowText);
IntPtr hwndOrb = FindWindowEx(IntPtr.Zero, IntPtr.Zero, (IntPtr)0xC017, null);
我必须定义
FindWindow = ctypes.windll.user32.FindWindowEx
FindWindow.restype = wintypes.HWND
FindWindow.argtypes = [
wintypes.HWND, ##hWnd
wintypes.HWND, ##hWnd
]
还是直接使用?抱歉,我是使用 python ctypes 的新手。
hWnd = win32gui.FindWindowEx (win32gui.GetDesktopWindow(),
None,0xC017 ,None)
最佳答案
如果您看到了错误消息,将会很有帮助。但是,这几乎可以肯定是因为您需要使用 user32.FindWindowExW
(如果您确实想要 ASCII、非 Unicode 版本,则需要使用 user32.FindWindowExA
)而不是直接使用 FindWindowEx。您还需要为所有四个参数指定 argtypes。
这是 docs 的原型(prototype):
HWND WINAPI FindWindowEx(
_In_opt_ HWND hwndParent,
_In_opt_ HWND hwndChildAfter,
_In_opt_ LPCTSTR lpszClass,
_In_opt_ LPCTSTR lpszWindow
);
那这个呢?
FindWindowEx = ctypes.windll.user32.FindWindowExW
FindWindowEx.argtypes = [
wintypes.HWND,
wintypes.HWND,
wintypes.LPCWSTR,
wintypes.LPCWSTR,
]
FindWindowEx.restype = wintypes.HWND
您还可以按照链接到的 C# 代码执行 FindWindow(而不是 FindWindowEx):
>>> FindWindow = ctypes.windll.user32.FindWindowW
>>> FindWindow.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR]
>>> FindWindow.restype = wintypes.HWND
>>> FindWindow('Shell_TrayWnd', '')
65670L
关于python - 将 C# P/Invoke 代码转换为 Python ctypes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13300833/