Пример #1
0
    def _CreateInstance_(self, clsid, reqIID):
        """Creates a new instance of a **wrapped** object

       This method looks up a "@win32com.server.policy.regSpec@" % clsid entry
       in the registry (using @DefaultPolicy@)
    """
        try:
            classSpec = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT, regSpec % clsid)
        except win32api.error:
            raise error("The object is not correctly registered - %s key can not be read" % (regSpec % clsid))
        myob = call_func(classSpec)
        self._wrap_(myob)
        try:
            return pythoncom.WrapObject(self, reqIID)
        except pythoncom.com_error as xxx_todo_changeme:
            (hr, desc, exc, arg) = xxx_todo_changeme.args
            from win32com.util import IIDToInterfaceName

            desc = "The object '%r' was created, but does not support the " "interface '%s'(%s): %s" % (
                myob,
                IIDToInterfaceName(reqIID),
                reqIID,
                desc,
            )
            raise pythoncom.com_error(hr, desc, exc, arg)
Пример #2
0
    def _CreateInstance_(self, clsid, reqIID):
        """Creates a new instance of a **wrapped** object

        This method looks up a "@win32com.server.policy.regSpec@" % clsid entry
        in the registry (using @DefaultPolicy@)
        """
        try:
            classSpec = win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT,
                                               regSpec % clsid)
        except win32api.error:
            raise error(
                "The object is not correctly registered - %s key can not be read"
                % (regSpec % clsid))
        myob = call_func(classSpec)
        self._wrap_(myob)
        try:
            return pythoncom.WrapObject(self, reqIID)
        except pythoncom.com_error as xxx_todo_changeme:
            (hr, desc, exc, arg) = xxx_todo_changeme.args
            from win32com.util import IIDToInterfaceName

            desc = ("The object '%r' was created, but does not support the "
                    "interface '%s'(%s): %s" %
                    (myob, IIDToInterfaceName(reqIID), reqIID, desc))
            raise pythoncom.com_error(hr, desc, exc, arg)
Пример #3
0
def RegisterPythonServer(filename, progids=None, verbose=0):
    if progids:
        if isinstance(progids, str):
            progids = [progids]
        # we know the CLSIDs we need, but we might not be an admin user
        # and otherwise unable to register them.  So as long as the progids
        # exist and the DLL points at our version, assume it already is.
        why_not = None
        for progid in progids:
            try:
                clsid = pythoncom.MakeIID(progid)
            except pythoncom.com_error:
                # no progid - not registered.
                break
            # have a CLSID - open it.
            try:
                HKCR = winreg.HKEY_CLASSES_ROOT
                hk = winreg.OpenKey(HKCR, "CLSID\\%s" % clsid)
                dll = winreg.QueryValue(hk, "InprocServer32")
            except WindowsError:
                # no CLSID or InProcServer32 - not good!
                break
            ok_files = [os.path.basename(pythoncom.__file__),
                        'pythoncomloader%d%d.dll' % (sys.version_info[0], sys.version_info[1])]
            if os.path.basename(dll) not in ok_files:
                why_not = "%r is registered against a different Python version (%s)" % (progid, dll)
                break
        else:
            #print "Skipping registration of '%s' - already registered" % filename
            return
    # needs registration - see if its likely!
    try:
        from win32com.shell.shell import IsUserAnAdmin
    except ImportError:
        print("Can't import win32com.shell - no idea if you are an admin or not?")
        is_admin = False
    else:
        try:
            is_admin = IsUserAnAdmin()
        except pythoncom.com_error:
            # old, less-secure OS - assume *is* admin.
            is_admin = True
    if not is_admin:
        msg = "%r isn't registered, but I'm not an administrator who can register it." % progids[0]
        if why_not:
            msg += "\n(registration check failed as %s)" % why_not
        # throw a normal "class not registered" exception - we don't report
        # them the same way as "real" errors.
        raise pythoncom.com_error(winerror.CO_E_CLASSSTRING, msg, None, -1)
    # so theoretically we are able to register it.
    cmd = '%s "%s" --unattended > nul 2>&1' % (win32api.GetModuleFileName(0), filename)
    if verbose:
        print("Registering engine", filename)
#       print cmd
    rc = os.system(cmd)
    if rc:
        print("Registration command was:")
        print(cmd)
        raise RuntimeError("Registration of engine '%s' failed" % filename)
Пример #4
0
def RegisterPythonServer(filename, progids=None, verbose=0):
    if progids:
        if isinstance(progids, str):
            progids = [progids]
        # we know the CLSIDs we need, but we might not be an admin user
        # and otherwise unable to register them.  So as long as the progids
        # exist and the DLL points at our version, assume it already is.
        why_not = None
        for progid in progids:
            try:
                clsid = pythoncom.MakeIID(progid)
            except pythoncom.com_error:
                # no progid - not registered.
                break
            # have a CLSID - open it.
            try:
                HKCR = winreg.HKEY_CLASSES_ROOT
                hk = winreg.OpenKey(HKCR, "CLSID\\%s" % clsid)
                dll = winreg.QueryValue(hk, "InprocServer32")
            except WindowsError:
                # no CLSID or InProcServer32 - not good!
                break
            ok_files = [os.path.basename(pythoncom.__file__),
                        'pythoncomloader%d%d.dll' % (sys.version_info[0], sys.version_info[1])]
            if os.path.basename(dll) not in ok_files:
                why_not = "%r is registered against a different Python version (%s)" % (progid, dll)
                break
        else:
            #print "Skipping registration of '%s' - already registered" % filename
            return
    # needs registration - see if its likely!
    try:
        from win32com.shell.shell import IsUserAnAdmin
    except ImportError:
        print("Can't import win32com.shell - no idea if you are an admin or not?")
        is_admin = False
    else:
        try:
            is_admin = IsUserAnAdmin()
        except pythoncom.com_error:
            # old, less-secure OS - assume *is* admin.
            is_admin = True
    if not is_admin:
        msg = "%r isn't registered, but I'm not an administrator who can register it." % progids[0]
        if why_not:
            msg += "\n(registration check failed as %s)" % why_not
        # throw a normal "class not registered" exception - we don't report
        # them the same way as "real" errors.
        raise pythoncom.com_error(winerror.CO_E_CLASSSTRING, msg, None, -1)
    # so theoretically we are able to register it.
    cmd = '%s "%s" --unattended > nul 2>&1' % (win32api.GetModuleFileName(0), filename)
    if verbose:
        print("Registering engine", filename)
#       print cmd
    rc = os.system(cmd)
    if rc:
        print("Registration command was:")
        print(cmd)
        raise RuntimeError("Registration of engine '%s' failed" % filename)
Пример #5
0
 def wrapper(*args):
     try:
         res = function(*args)
     except com_error as error:
         tmp = error
         hr, msg, exc, arg = tmp.args
         raise com_error(str(exc[2]))
     return res
Пример #6
0
 def Read(self, propName, varType, errorLog):
   print "read: name=", propName, "type=", varType
   if propName not in self.data:
     if errorLog:
       hr = 0x80070057
       exc = pythoncom.com_error(0, "Bag.Read", "no such item", None, 0, hr)
       errorLog.AddError(propName, exc)
     raise exception.Exception(scode=hr)
   return self.data[propName]
Пример #7
0
 def Read(self, propName, varType, errorLog):
   print("read: name=", propName, "type=", varType)
   if propName not in self.data:
     if errorLog:
       hr = 0x80070057
       exc = pythoncom.com_error(0, "Bag.Read", "no such item", None, 0, hr)
       errorLog.AddError(propName, exc)
     raise exception.Exception(scode=hr)
   return self.data[propName]
Пример #8
0
 def Commit(self, flags):
     # Testing unicode: 1F600   '😀'; GRINNING FACE
     # Use the 'name' just for fun!
     if flags == 0:
         # A non com-specific exception.
         raise Exception("\N{GRINNING FACE}")
     # An explicit com_error, which is a bit of an edge-case, but might happen if
     # a COM server itself calls another COM object and it fails.
     excepinfo = (
         winerror.E_UNEXPECTED,
         "source",
         "\N{GRINNING FACE}",
         "helpfile",
         1,
         winerror.E_FAIL,
     )
     raise pythoncom.com_error(winerror.E_UNEXPECTED, "desc", excepinfo,
                               None)
Пример #9
0
 def SchedulerSettings(self, a1):
     try:
         gui.show_window()
     except pythoncom.com_error(hr, msg):
         print("The Scheduler call failed with code %d: %s" % (hr, msg))
Пример #10
0
 def SchedulerCall(self, a1):
     try:
         outlook.get_availability()
     except pythoncom.com_error(hr, msg):
         print("The Scheduler call failed with code %d: %s" % (hr, msg))