Python a good choice for experimenting with Win32 API?

Mike Driscoll kyosohma at gmail.com
Fri Sep 12 18:07:50 CEST 2008


On Sep 12, 10:36 am, "Siegfried Heintze" <siegfr... at 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



More information about the Python-list mailing list