将C#P/Invoke代码转换为Python ctypes?

2024-09-23 10:34:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用ctypes将C代码转换为python时遇到问题。此代码用于隐藏Windows7启动orb。这是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);

我需要定义吗

^{pr2}$

还是直接用?抱歉,我刚开始使用python ctypes。在

hWnd = win32gui.FindWindowEx (win32gui.GetDesktopWindow(),
None,0xC017 ,None)

Tags: 代码nonelinkstaticprivatectypesdllzero
1条回答
网友
1楼 · 发布于 2024-09-23 10:34:42

如果有你看到的错误信息会很有帮助。但是,这几乎是肯定的,因为您需要使用user32.FindWindowExW(或者{},如果您真的想要ASCII、非Unicode版本),而不是直接使用findwowex。您还需要为所有四个参数指定argtypes。在

这是来自docs的原型:

HWND WINAPI FindWindowEx(
  _In_opt_  HWND hwndParent,
  _In_opt_  HWND hwndChildAfter,
  _In_opt_  LPCTSTR lpszClass,
  _In_opt_  LPCTSTR lpszWindow
);

那这个呢?在

^{pr2}$

您也可以根据链接到的C代码执行FindWindow(而不是FindWindowEx):

>>> FindWindow = ctypes.windll.user32.FindWindowW
>>> FindWindow.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR]
>>> FindWindow.restype = wintypes.HWND
>>> FindWindow('Shell_TrayWnd', '')
65670L

相关问题 更多 >