Win32api in Sharpcap python lib

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
zerolatitude
Posts: 156
Joined: Mon Mar 01, 2021 5:24 am

Win32api in Sharpcap python lib

#1

Post by zerolatitude »

Hi,

I'm trying to use Metaguide from within Sharpcap using a series of small scripts which will be called from inside the SC sequencer using the "Run python script" command. The commands etc are from Metaguide documentation and developer. Sample is below:

===============
import win32api
import win32con
import win32com.client
import sys

idguide = win32api.RegisterWindowMessage("MG_RemoteGuide")
win32api.PostMessage(win32con.HWND_BROADCAST, idguide, 0, 0)
sys.exit("Started guiding")
=======================

The script I've written works fine in Python 3.9.6 shell/IDE, but when running from SC it gives the error "no module named win32api".

Right now I've worked around by converting the scripts into exe and then calling from the sharpcap sequencer, but its painful.

Presumably the SC python lib doesn't include pywin32? If so, can it be included?

Thanks
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Win32api in Sharpcap python lib

#2

Post by admin »

Hi,

I think that the Win32api python library is likely to include C/C++ code, which means it cannot be loaded into the IronPython environment used inside SharpCap. For instance you can see here that there is C++ code - https://github.com/mhammond/pywin32/tre ... /win32/src

You need to use an alternative approach to call windows APIs from IronPython - essentially search for how to call them from the C# language and then base your approach on that. In a lot of cases you will need to use a 'Pinvoke' technique to define the functions to be called and where they can be found. Below is an example from the IronPyton unit test framework that shows how to do this for two simple functions.

cheers,

Robin




Code: Select all

    class NativeMethods(object):
        # Note that you could also the "ctypes" modules instead of pinvoke declarations
        __metaclass__ = clrtype.ClrClass

        from System.Runtime.InteropServices import DllImportAttribute, PreserveSigAttribute
        DllImport = clrtype.attribute(DllImportAttribute)
        PreserveSig = clrtype.attribute(PreserveSigAttribute)

        @staticmethod
        @DllImport("user32.dll")
        @PreserveSig()
        @clrtype.accepts(System.Char)
        @clrtype.returns(System.Boolean)
        def IsCharAlpha(c): raise RuntimeError("this should not get called")

        @staticmethod
        @DllImport("user32.dll")
        @PreserveSig()
        @clrtype.accepts(System.IntPtr, System.String, System.String, System.UInt32)
        @clrtype.returns(System.Int32)
        def MessageBox(hwnd, text, caption, type): raise RuntimeError("this should not get called")
zerolatitude
Posts: 156
Joined: Mon Mar 01, 2021 5:24 am

Re: Win32api in Sharpcap python lib

#3

Post by zerolatitude »

Thanks.

I think I'll just stick to making exes for now :-)
Post Reply