Python a good choice for experimenting with Win32 API?

Siegfried Heintze siegfried at heintze.com
Fri Sep 12 11:36:18 EDT 2008


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 





More information about the Python-list mailing list