Python a good choice for experimenting with Win32 API?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Siegfried Heintze

    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_CHA R = 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_SETFORPROCE SS = 0x00100, KLF_SHIFTLOCK = 0x10000, KLF_RESET =
    0x40000000;
    [DllImport("user 32.dll")] static extern IntPtr
    LoadKeyboardLay out(string pwszKLID, uint Flags);
    [DllImport("user 32.dll")] static extern bool
    UnloadKeyboardL ayout(IntPtr hkl);
    [DllImport("user 32.dll")] static extern uint MapVirtualKeyEx (uint
    uCode, uint uMapType, IntPtr dwhkl);
    private void Compute()
    {
    IntPtr hkl = LoadKeyboardLay out(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


  • Mike Driscoll

    #2
    Re: Python a good choice for experimenting with Win32 API?

    On Sep 12, 10:36 am, "Siegfried Heintze" <siegfr...@hein tze.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_CHA R = 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_SETFORPROCE SS = 0x00100, KLF_SHIFTLOCK = 0x10000, KLF_RESET =
    0x40000000;
            [DllImport("user 32.dll")]  static extern IntPtr
    LoadKeyboardLay out(string pwszKLID, uint Flags);
            [DllImport("user 32.dll")] static extern bool
    UnloadKeyboardL ayout(IntPtr hkl);
            [DllImport("user 32.dll")]   static extern uint MapVirtualKeyEx (uint
    uCode, uint uMapType, IntPtr dwhkl);
            private void Compute()
            {
                IntPtr hkl = LoadKeyboardLay out(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:



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

    Mike

    Comment

    • sturlamolden

      #3
      Re: Python a good choice for experimenting with Win32 API?


      (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.LoadKe yboardLayout(sL angId_, win32con.KLF_AC TIVATE
      |win32con.KLF_S UBSTITUTE_OK|wi n32con.KLF_REPL ACELANG)
      uResult_ = win32api.MapVir tualKeyEx(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_CHA R = 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_SETFORPROCE SS = 0x00100
      KLF_SHIFTLOCK = 0x10000
      KLF_RESET = 0x40000000

      LoadKeyboardLay out = ctypes.windll.u ser32.LoadKeybo ardLayout
      LoadKeyboardLay out.argtypes = (ctypes.c_wchar _p, ctypes.c_uint)
      LoadKeyboardLay out.restype = ctypes.c_void_p

      UnloadKeyboardL ayout = ctypes.windll.u ser32.UnloadKey boardLayout
      UnloadKeyboardL ayout.argtypes = (ctypes.c_void_ p,)
      UnloadKeyboardL ayout.restype = ctypes.c_int

      MapVirtualKeyEx = ctypes.windll.u ser32.UnloadKey boardLayout
      MapVirtualKeyEx .argtypes = (ctypes.c_uint, ctypes.c_uint,
      ctypes.c_void_p )
      MapVirtualKeyEx .restype = ctypes.c_uint

      def compute(sLangId _, uCode_, uMapType_):
      hkl = LoadKeyboardLay out(sLangId_, KLF_ACTIVATE|KL F_SUBSTITUTE_OK |
      KLF_REPLACELANG )
      uResult_ = MapVirtualKeyEx (uCode_, uMapType_, hkl)
      return uResult_

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




      Comment

      • Siegfried Heintze

        #4
        Re: Python a good choice for experimenting with Win32 API?

        I found this but have not tried it yet:


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

        Thanks!
        Siegfried


        Comment

        Working...