Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_manager(self):
"""Instantiates and returns pointer to interface documented by Microsoft.
:returns: pointer to :class:`IVirtualDesktopManager`
"""
return comtypes.CoCreateInstance(
CLSID_VirtualDesktopManager, IVirtualDesktopManager
)
def GetInterfaceFromGlobal(self, cookie, interface=IUnknown):
ptr = POINTER(interface)()
self.__com_GetInterfaceFromGlobal(cookie, interface._iid_, ptr)
return ptr
def RevokeInterfaceFromGlobal(self, cookie):
self.__com_RevokeInterfaceFromGlobal(cookie)
# It was a pain to get this CLSID: it's neither in the registry, nor
# in any header files. I had to compile a C program, and find it out
# with the debugger. Apparently it is in uuid.lib.
CLSID_StdGlobalInterfaceTable = GUID("{00000323-0000-0000-C000-000000000046}")
git = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
interface=IGlobalInterfaceTable,
clsctx=CLSCTX_INPROC_SERVER)
RevokeInterfaceFromGlobal = git.RevokeInterfaceFromGlobal
RegisterInterfaceInGlobal = git.RegisterInterfaceInGlobal
GetInterfaceFromGlobal = git.GetInterfaceFromGlobal
__all__ = ["RegisterInterfaceInGlobal", "RevokeInterfaceFromGlobal", "GetInterfaceFromGlobal"]
if __name__ == "__main__":
from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib
tlib = CreateTypeLib("foo.bar") # we don not save it later
assert (tlib.AddRef(), tlib.Release()) == (2, 1)
cookie = RegisterInterfaceInGlobal(tlib)
'clsctx' specifies how to create the object, use the CLSCTX_... constants.
'machine' allows to specify a remote machine to create the object on.
'sink' specifies an optional object to receive COM events.
'sourceinterface' is the interface that sends events. If not specified,
the default source interface is used.
You can also later request to receive events with GetEvents().
"""
clsid = comtypes.GUID.from_progid(progid)
logger.debug("%s -> %s", progid, clsid)
if interface is None:
interface = getattr(progid, "_com_interfaces_", [None])[0]
if machine is None:
logger.debug("CoCreateInstance(%s, clsctx=%s, interface=%s)",
clsid, clsctx, interface)
obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
else:
logger.debug("CoCreateInstanceEx(%s, clsctx=%s, interface=%s, machine=%s)",
clsid, clsctx, interface, machine)
obj = comtypes.CoCreateInstanceEx(clsid, clsctx=clsctx, interface=interface, machine=machine)
return _manage(obj, clsid,
interface=interface,
sink=sink,
sourceinterface=sourceinterface)
def ActiveXObject(progid,
interface=comtypes.automation.IDispatch,
clsctx=comtypes.CLSCTX_ALL):
# XXX Should we also allow GUID instances for the first parameter?
# Or strings containing guids?
clsid = comtypes.GUID.from_progid(progid)
p = comtypes.CoCreateInstance(clsid, interface=interface, clsctx=clsctx)
return _Dynamic(p)
@staticmethod
def GetSpeakers():
"""
get the speakers (1st render + multimedia) device
"""
deviceEnumerator = comtypes.CoCreateInstance(
CLSID_MMDeviceEnumerator,
IMMDeviceEnumerator,
comtypes.CLSCTX_INPROC_SERVER)
speakers = deviceEnumerator.GetDefaultAudioEndpoint(
EDataFlow.eRender.value, ERole.eMultimedia.value)
return speakers
def __init__(self):
self.UIA_dll = comtypes.client.GetModule('UIAutomationCore.dll')
self.ui_automation_client = comtypes.gen.UIAutomationClient
self.iuia = comtypes.CoCreateInstance(
self.ui_automation_client.CUIAutomation().IPersist_GetClassID(),
interface=self.ui_automation_client.IUIAutomation,
clsctx=comtypes.CLSCTX_INPROC_SERVER
)
self.true_condition = self.iuia.CreateTrueCondition()
self.tree_scope = {
'ancestors': self.UIA_dll.TreeScope_Ancestors,
'children': self.UIA_dll.TreeScope_Children,
'descendants': self.UIA_dll.TreeScope_Descendants,
'element': self.UIA_dll.TreeScope_Element,
'parent': self.UIA_dll.TreeScope_Parent,
'subtree': self.UIA_dll.TreeScope_Subtree,
}
self.root = self.iuia.GetRootElement()
self.get_focused_element = self.iuia.GetFocusedElement
def _get_service_provider(self):
"""Instantiates and returns pointer to service provider.
:returns: pointer to :class:`IServiceProvider`
"""
return comtypes.CoCreateInstance(
CLSID_ImmersiveShell, IServiceProvider, comtypes.CLSCTX_LOCAL_SERVER
)