2

I'm currently getting into the ctypes module and I'm trying to call the user32 function GetWindowText with a HWND handle I already received by using FindWindow. This time though i wanted to process a step further and use a function prototype instead of calling the function with ctypes.windll.user32.GetWindowText. Although I´m having problems declaring the lpString arguement as output parameter.

My first attempt looked this way:

GetWindowText = cfunc("GetWindowTextA",windll.user32,c_int,
                  ("hWnd",HWND,1),
                  ("lpString",LPCSTR,2),
                  ("nMaxCount",c_int,1)
                  )

(cfunc is a little wrapper that I found here)

This prototype yields the following exception as soon as it is called:

    chars,name = user32.GetWindowText(handle,255)
TypeError: c_char_p 'out' parameter must be passed as default value

I thought that any output variables must be a POINTER(...) type, so I changed my definition to:

GetWindowText = cfunc("GetWindowTextA",windll.user32,c_int,
                      ("hWnd",HWND,1),
                      ("lpString",POINTER(c_char),2),
                      ("nMaxCount",c_int,1)
                      )

But this yields an exception too:

    chars,name = user32.GetWindowText(handle,255)
ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: wrong type

I hope somebody knows how to call the GetWindowText function correctly using ctypes prototyping.

Edit:

Through further research I could get it to work, at least somehow. The first issue I fixed was the usage of cfunc() which had wrong calling specifiers. I defined a exact copy of that function and named it winfunc() and replaced return CFUNCTYPE(result, *atypes)((name, dll), tuple(aflags)) with return WINFUNCTYPE(result, *atypes)((name, dll), tuple(aflags)).

Then I inspected prototyping further. As it seems if you pass somewhat like ("someParameter",POINTER(aType),2) to WINFUNCTYPE it will create a aType object on call and passes a pointer to that object to the function. In the returned tuple you can then access the aType object. This brings up another problem. A cstring is a array of chars; so one needs to tell ctypes to create a c_char array. This means that something like:

GetWindowText = winfunc("GetWindowTextA",windll.user32,c_int,
                  ("hWnd",HWND,1),
                  ("lpString",POINTER(c_char*255),2),
                  ("nMaxCount",c_int,1)
                  )

works just fine. But unfortunately, ctypes will now pass a pointer to a cstring which is ALWAYS 255 chars long ignoring the size specified by nMaxCount.

In my opinion, I think theres no way one could get that function to work with a dynamically sized cstring defined as output parameter. The only possibility seems to be simply going without the output parameter feature and defining a LPCSTR as input parameter. The callee then needs to create a buffer by his own with ctypes.create_string_buffer() and pass it to the function (just as in C).

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77

3 Answers3

3

You have to create a string buffer for out parameters. You can wrap the function to make it somewhat transparent:

# python3
from ctypes import *

_GetWindowText = WinDLL('user32').GetWindowTextW
_GetWindowText.argtypes = [c_void_p,c_wchar_p,c_int]
_GetWindowText.restype = c_int

def GetWindowText(h):
    b = create_unicode_buffer(255)
    _GetWindowText(h,b,255)
    return b.value

FindWindow = WinDLL('user32').FindWindowW
FindWindow.argtypes = [c_wchar_p,c_wchar_p]
FindWindow.restype = c_void_p

h = FindWindow(None,'Untitled - Notepad')
print(GetWindowText(h))

Or in this case you can just use pywin32:

import win32gui
h = win32gui.FindWindow(None,'Untitled - Notepad')
print(win32gui.GetWindowText(h))
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Yes I already saw pywin32 but I dont want to use because I need ctypes in conjuction with win32 otherwhere and I want persistent code (I probably even need to exchange handles etc.). Meanwhile I encountered another problem with the prototype pattern (one can just access the output parameters not the normal return value; not in a generic fashion) and Im seriously considering going a wrapper solution as you approached or simply going completly (also for functions where it would work) without the "output parameter" feature which seems pretty untested to me. – Sebastian Hoffmann Jun 05 '12 at 23:13
  • I'm not sure what you mean by "persistent code", but the pywin32 `PyHANDLE` object can attach/detach from raw Window handles if you need to exchange handles. See http://docs.activestate.com/activepython/2.4/pywin32/PyHANDLE.html documentation. Also note the the Python `c_char_p` type is meant for passing input strings only. Only use `create_string_buffer(255)` for example to make buffers that can be used for output. – Mark Tolonen Jun 05 '12 at 23:27
1

Yep. You have to create the buffer for every call. If you let the function definition do it, how will you able to access the buffer later?

You also seem to have to tell it to expect a pointer c_char with POINTER(c_char), and not simply c_char_p or LPSTR. Not sure why that happens though.

Anyway, this should work:

from ctypes import *
from ctypes.wintypes import *

# defs

FindWindowF = WINFUNCTYPE(HWND, LPSTR, LPSTR)
FindWindow = FindWindowF(windll.user32.FindWindowA)

GetWindowTextF = WINFUNCTYPE(c_int, HWND, POINTER(c_char), c_int)
GetWindowText = GetWindowTextF(windll.user32.GetWindowTextA)

# code

text = create_string_buffer(255)

hwnd = FindWindow(None, 'Untitled - Notepad')
GetWindowText(hwnd, text, sizeof(text))

print text.value
kichik
  • 33,220
  • 7
  • 94
  • 114
  • This does ofc work as I wrote in my edit and I think it is currently the only solution.The problem this questions tries to face is using the "output parameter" functionality with variable sized cstrings. Concerning the `GetWindowRect()` function having a output Rect* parameter to a Rect structure on heap, one can define the prototype as `prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))` and then specifying `paramflags = (1, "hwnd"), (2, "lprect")`.Finally getting the func `GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)`.Now one can call `rect = GetWindowRect(handle)` – Sebastian Hoffmann Jun 05 '12 at 23:00
  • The problem with the pointers concerns about the following: c_char_p is actually a native type defined in _ctyped.pyd as far as i know. This means that c_char_p != POINTER(c_char). As it seems to me, the paramlist arguement always forces you to the POINTER(x) type if you want to use the output parameter feature. This way it creates a object of x at call and passes a POINTER(x) to the function. You finally get the x object back. If your although not using that feature, you can of course use c_char_p or in this case LPCSTR. – Sebastian Hoffmann Jun 05 '12 at 23:03
0

I recently ran into a problem with the same error message, except for c_void_p instead. This question is very old and this will not be directly related, but this is the only search result that addresses this particular error, so I want to lay this here for anyone with this problem in the future.

My problem was with CoInitializeEx instead. The answer turned out to be very simple though: I was passing a LPVOID to the output parameter, when in fact a LPVOID* is expected, so a pointer to a pointer. In this COM nested pointer madness, I simply did not notice it until the day after. I imagine this is an easy mistake to make.

Now more closely related to the question, this error message seems to occur when ctypes mistakenly interprets our issues as something related to default values, which they are not. If you get this error and it doesn't make sense to you in your context, I recommend checking if there are any mistakes elsewhere.

Huzzah
  • 1