Comments: Using Ctypes to access Windows API

The recommended way to load user32.dll or gdi32.dll is
user = windll.user32
they are on the default path for dlls.
This makes your code portable, since you don't have to know the path on the system directory.

Cheers, Thomas

Posted by at August 16, 2004 02:41 AM

You are reading the dpi, what if I wanted the actual resolution (pix) ? I can easily use EnumDisplaySettings() in C
--------------------------------------
DEVMODE devMode;
EnumDisplaySettings( NULL, 0, &devMode);
int iPixW = devMode.dmPelsWidth;
int iPixH = devMode.dmPelsHeight;
--------------------------------------
The equivalent call would be gdi.EnumDisplaySettingsA? Just took a quick glimpse at the ctypes homepage. Do I need to buildup a DEVMODE struct by deriving from 'Structure' to do so? DEVMODE is huge ;). Using sizeof(DEVMODE) and "direct" offsets is a no-no.

I suppose I have to look into win32all for this?

Posted by Amanjit Gill at August 16, 2004 12:05 PM

errata: user.EnumDisplaySettings( )

Posted by Amanjit Gill at August 16, 2004 12:13 PM

Last post (remember, this is no spam ;-) )
Ok, I suppose I am stucked; DEVMODE is not wrapped?

http://mail.python.org/pipermail/python-win32/2004-August/002169.html

But since this is nonportable code anyway, I could easily create a C DLL that exports a function.

Posted by Amanjit Gill at August 16, 2004 12:36 PM

Thanks, Thomas for the correction. As Amanjit noted, Ctypes does a good job with C functions which pass simple types back-and-forth via parameters. With structures, it may be a lot more difficult. A growing number of people are using Pyrex and it sounds like Pyrex makes calling C-with-structures easier. However, with so many ways, confusion abounds: Ctypes -- SWIG -- Pyrex -- and there may be others I'm not aware of yet...

Posted by Hoang at August 17, 2004 06:35 AM

Hello - In my specific case I have to say I do not have to use EnumDisplaySettings() at all but simply could use GetDeviceCaps() with the specific index (me stupid) i.e. HORZRES or VERTRES. Changing the resolution would be a problem IMHO. Anyway ctypes looks convenient and straightforward - and you can always resort to an intermediate c-lib and SWIG it if ctype is not enough.

Posted by Amanjit Gill at August 17, 2004 09:03 AM

Hi - Thanks for posting this and to Thomas too for mentioning how to avoid having to do the path explicilty (I was wondering that). I wondered if you could tell me how you know the values for the second argument getDeviceCaps ? I've looked at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_88s3.asp
but that only defines them in terms of constants. Is there a way using CTypes to use those constants or alternatively some way of looking them up ? I appreciate this is a pretty basic question but if anyone could post an answer I'd appreciate it.

thanks

richard.

Posted by Richard Shea at August 17, 2004 02:33 PM

To answer Richard, Visual Studio.Net pops up a hover tab which shows you the value of the constant as you type them in. The traditional way would be to do a grep of the constant name in the Win32 include directory (wherever you installed it). This should give you the constant and its respective value.

Good luck.

Posted by Hoang at August 17, 2004 11:45 PM

To all those asking ctypes questions here: I'm not prepared to answer questions about ctypes here, I only wanted to comment on Hoangs blog entry.

Please ask (and repost) your questions in the ctypes-users mailing list. This list is also available on gmane via http or nntp, and subscribing is not absolutely needed.

This will help to improve ctypes, and avoid using this comment system as yet another discussion forum.

Thanks.

(And yes, there are plans to make wrapping large win32 structures easier)

Posted by Thomas Heller at August 19, 2004 05:05 AM

As a note, the equivalent with win32all would be:

import win32gui, win32ui
h = win32gui.GetDC

xdpi = win32ui.GetDeviceCaps(h, 88)
ydpi = win32ui.GetDeviceCaps(h, 90)

I expect the 88 and 90 are constants that would be available through win32con, but I don't know what to look for there.

Posted by sfb at August 27, 2004 05:26 AM