1

I'm having problem converting this C# code to python using ctypes. This code is for hiding windows 7 start orb. Here's the 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);

do i have to define

FindWindow = ctypes.windll.user32.FindWindowEx
FindWindow.restype = wintypes.HWND
FindWindow.argtypes = [
    wintypes.HWND, ##hWnd
    wintypes.HWND, ##hWnd
]

Or just use it directly? Sorry I'm new in using python ctypes.

hWnd = win32gui.FindWindowEx (win32gui.GetDesktopWindow(),
None,0xC017 ,None)
Community
  • 1
  • 1
unice
  • 2,655
  • 5
  • 43
  • 78
  • 1
    Have you possibly considered using `IronPython` which should provide more convenient access to the .NET framework? – Jon Clements Nov 09 '12 at 02:02

1 Answers1

2

It'd be helpful to have the error message you're seeing. However, this is almost certainly because you need to use user32.FindWindowExW (or user32.FindWindowExA if you really want the ASCII, non-Unicode version) rather than straight FindWindowEx. You also need to specify argtypes for all four parameters.

Here's the prototype from the docs:

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

So what about this?

FindWindowEx = ctypes.windll.user32.FindWindowExW
FindWindowEx.argtypes = [
    wintypes.HWND,
    wintypes.HWND,
    wintypes.LPCWSTR,
    wintypes.LPCWSTR,
]
FindWindowEx.restype = wintypes.HWND

You can also do FindWindow (rather than FindWindowEx) as per the C# code you linked to:

>>> FindWindow = ctypes.windll.user32.FindWindowW
>>> FindWindow.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR]
>>> FindWindow.restype = wintypes.HWND
>>> FindWindow('Shell_TrayWnd', '')
65670L
Ben Hoyt
  • 10,694
  • 5
  • 60
  • 84
  • Also note: yes, unless it's a function just taking an int, you'll need to define the restype/argtypes explicitly like this. Otherwise it won't work correctly between 32-bit and 64-bit code with pointer types. See also [my post about this here](http://tech.oyster.com/cherrypy-ctypes-and-being-explicit/). – Ben Hoyt Nov 09 '12 at 04:30
  • thanks @Ben Hoyt for sample code. But how can i make FindWindowEx accept (IntPtr)0xC017? What wintypes or ctypes will be used? – unice Nov 09 '12 at 04:59
  • Oh, I see: `FindWindowEx()` has a very strange double-use of that third argument -- it can be either a string or an int. I guess in this case you'd use `ctypes.cast(0xC017, wintypes.LPCWSTR)`. If you're using it with ints often, define the argtypes with a correctly-sized integer instead of an LPCWSTR. – Ben Hoyt Nov 09 '12 at 07:09