473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Python a good choice for experimenting with Win32 API?

I need to understand some User32.dll functions and I'm making an elaborate
GUI so I can easily experiment with different parameters. This is taking a
long time. I'm new to python and I'm thinking it would sure be nice to have
an interpreter I can type a few lines of code into and test things.

(1) Would CPython be a good choice for this? How about iron python? How
about Jython (probably not).
(2) What about callbacks? Which pythons can handle callbacks? I read on msdn
that you cannot use managed code to call SetWindowsHook and supply a call
back (with two exceptions). I guess that means I cannot use iron python.
What about C-Python for playing with hooks?
(3) How easy it it define the C structs, enums and integer constants
necessary to call the windows API functions?
(4) Here is the code I'm struggling with presently (it is just not
cooperating: no errors just wrong results! I expect to be able give it a
scan code, get a virtual key code, change the arguments, give it the virtual
key code and get the original scan code back again).

public const UInt32 MAPVK_VK_TO_VSC = 0, MAPVK_VSC_TO_VK = 1,
MAPVK_VK_TO_CHAR = 2, MAPVK_VSC_TO_VK_EX = 3, MAPVK_VK_TO_VSC_EX = 4;
public const UInt32 KLF_ACTIVATE = 1, KLF_SUBSTITUTE_OK = 2,
KLF_REORDER = 8, KLF_REPLACELANG = 0x10, KLF_NOTELLSHELL = 0x80,
KLF_SETFORPROCESS = 0x00100, KLF_SHIFTLOCK = 0x10000, KLF_RESET =
0x40000000;
[DllImport("user32.dll")] static extern IntPtr
LoadKeyboardLayout(string pwszKLID, uint Flags);
[DllImport("user32.dll")] static extern bool
UnloadKeyboardLayout(IntPtr hkl);
[DllImport("user32.dll")] static extern uint MapVirtualKeyEx(uint
uCode, uint uMapType, IntPtr dwhkl);
private void Compute()
{
IntPtr hkl = LoadKeyboardLayout(sLangId_, KLF_ACTIVATE |
KLF_SUBSTITUTE_OK | KLF_REPLACELANG);
uResult_ = MapVirtualKeyEx(uCode_, uMapType_, hkl);
UpdateOutput();
}
Would it be easy to execute this in the CPython interpreter or am I better
off sticking with C#?

Thanks!
Siegfried
Sep 12 '08 #1
3 4147
On Sep 12, 10:36*am, "Siegfried Heintze" <siegfr...@heintze.com>
wrote:
I need to understand some User32.dll functions and I'm making an elaborate
GUI so I can easily experiment with different parameters. This is taking a
long time. I'm new to python and I'm thinking it would sure be nice to have
an interpreter I can type a few lines of code into and test things.

(1) Would CPython be a good choice for this? How about iron python? How
about Jython (probably not).
(2) What about callbacks? Which pythons can handle callbacks? I read on msdn
that you cannot use managed code to call SetWindowsHook and supply a call
back (with two exceptions). I guess that means I cannot use iron python.
What about C-Python for playing with hooks?
(3) How easy it it define the C structs, enums and integer constants
necessary to call the windows API functions?
(4) Here is the code I'm struggling with presently (it is just not
cooperating: no errors just wrong results! I expect to be able give it a
scan code, get a virtual key code, change the arguments, give it the virtual
key code and get the original scan code back again).

* * * * public const UInt32 MAPVK_VK_TO_VSC = 0, MAPVK_VSC_TO_VK = 1,
MAPVK_VK_TO_CHAR = 2, MAPVK_VSC_TO_VK_EX = 3, MAPVK_VK_TO_VSC_EX = 4;
* * * * public const UInt32 KLF_ACTIVATE = 1, KLF_SUBSTITUTE_OK= 2,
KLF_REORDER = 8, KLF_REPLACELANG = 0x10, KLF_NOTELLSHELL = 0x80,
KLF_SETFORPROCESS = 0x00100, KLF_SHIFTLOCK = 0x10000, KLF_RESET =
0x40000000;
* * * * [DllImport("user32.dll")] *static extern IntPtr
LoadKeyboardLayout(string pwszKLID, uint Flags);
* * * * [DllImport("user32.dll")] static extern bool
UnloadKeyboardLayout(IntPtr hkl);
* * * * [DllImport("user32.dll")] * static extern uint MapVirtualKeyEx(uint
uCode, uint uMapType, IntPtr dwhkl);
* * * * private void Compute()
* * * * {
* * * * * * IntPtr hkl = LoadKeyboardLayout(sLangId_, KLF_ACTIVATE |
KLF_SUBSTITUTE_OK | KLF_REPLACELANG);
* * * * * * uResult_ = MapVirtualKeyEx(uCode_, uMapType_, hkl);
* * * * * * UpdateOutput();
* * * * }
Would it be easy to execute this in the CPython interpreter or am I better
off sticking with C#?

Thanks!
Siegfried
I use the Python interpreter all the time for heavy experimentation.
While I use PyWin32, I haven't done what you are attempting. I would
recommend re-posting to the PyWin32 group as the PyWin32 creators use
that list much more:

http://mail.python.org/mailman/listinfo/python-win32

And there's lots of other knowledgeable users there too. Good luck!

Mike
Sep 12 '08 #2

(1) Would CPython be a good choice for this? How about iron python? How
about Jython (probably not).
You can use CPython without any problems. Alternatives are pywin32,
ctypes, cython, pyrex, Python C API.

You can use .NET platform invoke from IronPython.

You can use JNI from Jython, or any Win32 API wrapper that may exist
for Java.

(2) What about callbacks? Which pythons can handle callbacks?
Yes you can use Python functions or bound methods as callbacks.

(3) How easy it it define the C structs, enums and integer constants
necessary to call the windows API functions?
It is easy if you can read C header files.

(4) Here is the code I'm struggling with presently (it is just not
cooperating: no errors just wrong results!
Here is what it will look like with the pywin32 package:
import win32api
import win32con

def compute(sLangId_, uCode_, uMapType_):
hkl = win32api.LoadKeyboardLayout(sLangId_, win32con.KLF_ACTIVATE
|win32con.KLF_SUBSTITUTE_OK|win32con.KLF_REPLACELA NG)
uResult_ = win32api.MapVirtualKeyEx(uCode_, uMapType_, hkl)
return uResult_

With ctypes there is a little more work to do:
import ctypes

MAPVK_VK_TO_VSC = 0
MAPVK_VSC_TO_VK = 1
MAPVK_VK_TO_CHAR = 2
MAPVK_VSC_TO_VK_EX = 3
MAPVK_VK_TO_VSC_EX = 4
KLF_ACTIVATE = 1,
KLF_SUBSTITUTE_OK = 2
KLF_REORDER = 8
KLF_REPLACELANG = 0x10
KLF_NOTELLSHELL = 0x80
KLF_SETFORPROCESS = 0x00100
KLF_SHIFTLOCK = 0x10000
KLF_RESET = 0x40000000

LoadKeyboardLayout = ctypes.windll.user32.LoadKeyboardLayout
LoadKeyboardLayout.argtypes = (ctypes.c_wchar_p, ctypes.c_uint)
LoadKeyboardLayout.restype = ctypes.c_void_p

UnloadKeyboardLayout = ctypes.windll.user32.UnloadKeyboardLayout
UnloadKeyboardLayout.argtypes = (ctypes.c_void_p,)
UnloadKeyboardLayout.restype = ctypes.c_int

MapVirtualKeyEx = ctypes.windll.user32.UnloadKeyboardLayout
MapVirtualKeyEx.argtypes = (ctypes.c_uint, ctypes.c_uint,
ctypes.c_void_p)
MapVirtualKeyEx.restype = ctypes.c_uint

def compute(sLangId_, uCode_, uMapType_):
hkl = LoadKeyboardLayout(sLangId_, KLF_ACTIVATE|KLF_SUBSTITUTE_OK|
KLF_REPLACELANG)
uResult_ = MapVirtualKeyEx(uCode_, uMapType_, hkl)
return uResult_

This is similar to your code, so it will have the same bugs.


Sep 12 '08 #3
I found this but have not tried it yet:
http://aspn.activestate.com/ASPN/Mai...Python/1775844

How different is ActiveState Python from CPython? Can they both be used with
pywin32 and the other packages?

Thanks!
Siegfried
Sep 14 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bezeee | last post by:
At my work we are in the process of building a tool to test an XML based API. Basically, XML in and XML out over http. Currently, there are two engines that do all of the schema validations, xml...
6
by: Fernando Rodriguez | last post by:
Hi, Any recommendation for a good book on multithreaded programming with Python? O:-) TIA
59
by: Hugh Macdonald | last post by:
I've recently been trying out various different GUI frameworks in Python and was wondering if I could get your input on the pros and cons of the different ones... wxPython: I love the...
6
by: Graeme Matthew | last post by:
Hi All I have noticed that there are a numbe rof client libraries for connecting to Postgres. Can anyone tell me what is the recommended Python library for database connections many...
5
by: Aaron Ginn | last post by:
I'm investigating the feasibility of using Python instead of Visual Basic for a commercial software package that I'm planning on developing. Now I'm absolutely a Python zealot. I use it for most...
3
by: arielgr | last post by:
Hi, My company is involved in the development of many data marts and data-warehouses, and I currently looking into migrating our old set of tools (written in Korn) to a new, more dynamic and...
18
by: W. Watson | last post by:
What do I download to use Python with MX XP Pro on an ASUS 4 year old motherboard? I would guess a good book source for starters would be the O'Reilly book. Wayne T. Watson (Watson Adventures,...
8
by: Edward Cormier | last post by:
Which computer books are the best to begin learning Python 2.5 with? I've heard that Learning Python 3rd Edition is a good choice - can anyone give any more advice on this? Thanks.
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.