我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用pythoncom.CoCreateInstance()。
def resolve_shortcut(filename): """resolve_shortcut("Notepad.lnk") => "C:\WINDOWS\system32\notepad.exe" Returns the path refered to by a windows shortcut (.lnk) file. """ shell_link = pythoncom.CoCreateInstance( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink) persistant_file = shell_link.QueryInterface(pythoncom.IID_IPersistFile) persistant_file.Load(filename) shell_link.Resolve(0, 0) linked_to_file = shell_link.GetPath(shell.SLGP_UNCPRIORITY)[0] return linked_to_file
def __init__(self, path=None, arguments=None, description=None, workingdir=None, iconpath=None, iconidx=0): self._base = pythoncom.CoCreateInstance( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink ) data = map(None, ['"%s"' % os.path.abspath(path), arguments, description, os.path.abspath(workingdir), os.path.abspath(iconpath)], ("SetPath", "SetArguments", "SetDescription", "SetWorkingDirectory") ) for value, function in data: if value and function: # call function on each non-null value getattr(self, function)(value) if iconpath: self.SetIconLocation(iconpath, iconidx)
def TestVTableMI(): clsctx = pythoncom.CLSCTX_SERVER ob = pythoncom.CoCreateInstance("Python.Test.PyCOMTestMI", None, clsctx, pythoncom.IID_IUnknown) # This inherits from IStream. ob.QueryInterface(pythoncom.IID_IStream) # This implements IStorage, specifying the IID as a string ob.QueryInterface(pythoncom.IID_IStorage) # IDispatch should always work ob.QueryInterface(pythoncom.IID_IDispatch) iid = pythoncom.InterfaceNames["IPyCOMTest"] try: ob.QueryInterface(iid) except TypeError: # Python can't actually _use_ this interface yet, so this is # "expected". Any COM error is not. pass
def testShellLink(self): desktop = str(shell.SHGetSpecialFolderPath(0, CSIDL_DESKTOP)) num = 0 shellLink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink) persistFile = shellLink.QueryInterface(pythoncom.IID_IPersistFile) names = [os.path.join(desktop, n) for n in os.listdir(desktop)] programs = str(shell.SHGetSpecialFolderPath(0, CSIDL_PROGRAMS)) names.extend([os.path.join(programs, n) for n in os.listdir(programs)]) for name in names: try: persistFile.Load(name,STGM_READ) except pythoncom.com_error: continue # Resolve is slow - avoid it for our tests. #shellLink.Resolve(0, shell.SLR_ANY_MATCH | shell.SLR_NO_UI) fname, findData = shellLink.GetPath(0) unc = shellLink.GetPath(shell.SLGP_UNCPRIORITY)[0] num += 1 if num == 0: # This isn't a fatal error, but is unlikely. print "Could not find any links on your desktop or programs dir, which is unusual"
def __init__(self, interfaceMaker = None, processName = None): if processName is None: processName = "Python Process" if interfaceMaker is None: interfaceMaker = SimpleHostStyleInterfaceMaker() self.pydebugger = adb.Debugger() self.pdm=pythoncom.CoCreateInstance(axdebug.CLSID_ProcessDebugManager,None,pythoncom.CLSCTX_ALL, axdebug.IID_IProcessDebugManager) self.app, self.root = interfaceMaker.MakeInterfaces(self.pdm) self.app.SetName(processName) self.interfaceMaker = interfaceMaker expressionProvider = _wrap(expressions.ProvideExpressionContexts(), axdebug.IID_IProvideExpressionContexts) self.expressionCookie = self.app.AddGlobalExpressionContextProvider(expressionProvider) contProvider = CodeContainerProvider(self) self.pydebugger.AttachApp(self.app, contProvider)
def create_shortcut(path, description, filename, arguments="", workdir="", iconpath="", iconindex=0): import pythoncom from win32com.shell import shell, shellcon ilink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink) ilink.SetPath(path) ilink.SetDescription(description) if arguments: ilink.SetArguments(arguments) if workdir: ilink.SetWorkingDirectory(workdir) if iconpath or iconindex: ilink.SetIconLocation(iconpath, iconindex) # now save it. ipf = ilink.QueryInterface(pythoncom.IID_IPersistFile) ipf.Save(filename, 0) # Support the same list of "path names" as bdist_wininst.
def read_scexec(scfile): """ read shortcut and return executable path """ if sys.platform != 'win32': return "Only available for windows platforms! returning" try: import pythoncom from win32com.shell import shell, shellcon except: return "pythoncom module not found! \n download from http://sourceforge.net/projects/pywin32/files/pywin32/" shortcut = pythoncom.CoCreateInstance ( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink ) shortcut.QueryInterface (pythoncom.IID_IPersistFile).Load (scfile, 0) cmd, _ = shortcut.GetPath (shell.SLGP_UNCPRIORITY) args = shortcut.GetArguments () work_dir = shortcut.GetWorkingDirectory() return "Executing " + cmd, cmd, args, work_dir, False, False
def __init__( self ): self._base = pythoncom.CoCreateInstance( shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink )
def DumpLink(fname): shellLink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink) persistFile = shellLink.QueryInterface(pythoncom.IID_IPersistFile) persistFile.Load(fname,STGM_READ) shellLink.Resolve(0, shell.SLR_ANY_MATCH | shell.SLR_NO_UI) fname, findData = shellLink.GetPath(0) print "Filename:", fname, ", UNC=", shellLink.GetPath(shell.SLGP_UNCPRIORITY)[0] print "Description:", shellLink.GetDescription() print "Working Directory:", shellLink.GetWorkingDirectory() print "Icon:", shellLink.GetIconLocation()
def __init__( self ): self._base = pythoncom.CoCreateInstance( shell.CLSID_InternetShortcut, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IUniformResourceLocator )
def set_shortcut(): startup_path = shell.SHGetPathFromIDList(shell.SHGetSpecialFolderLocation(0,shellcon.CSIDL_STARTUP)) shortcut = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, \ shell.IID_IShellLink) shortcut.SetPath(os.getcwd()+'\\Email My PC Launcher.exe') shortcut.SetWorkingDirectory(os.getcwd()) shortcut.SetIconLocation(os.getcwd()+'\\ui\\images\\Icon.ico',0) shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(startup_path+'\\Emai My PC.lnk',0) #??????????
def _cat_registrar(): return pythoncom.CoCreateInstance( pythoncom.CLSID_StdComponentCategoriesMgr, None, pythoncom.CLSCTX_INPROC_SERVER, pythoncom.IID_ICatRegister )
def GetSubList(self): catinf=pythoncom.CoCreateInstance(pythoncom.CLSID_StdComponentCategoriesMgr,None,pythoncom.CLSCTX_INPROC,pythoncom.IID_ICatInformation) enum=util.Enumerator(catinf.EnumCategories()) ret = [] try: for catid, lcid, desc in enum: ret.append(HLICategory((catid, lcid, desc))) except pythoncom.com_error: # Registered categories occasionally seem to give spurious errors. pass # Use what we already have. return ret
def GetSubList(self): win32ui.DoWaitCursor(1) catid, lcid, desc = self.myobject catinf=pythoncom.CoCreateInstance(pythoncom.CLSID_StdComponentCategoriesMgr,None,pythoncom.CLSCTX_INPROC,pythoncom.IID_ICatInformation) ret = [] for clsid in util.Enumerator(catinf.EnumClassesOfCategories((catid,),())): ret.append(HLICLSID(clsid)) win32ui.DoWaitCursor(0) return ret
def _GetGoodDispatch(IDispatch, clsctx = pythoncom.CLSCTX_SERVER): # quick return for most common case if isinstance(IDispatch, PyIDispatchType): return IDispatch if isinstance(IDispatch, _GoodDispatchTypes): try: IDispatch = pythoncom.connect(IDispatch) except pythoncom.ole_error: IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) else: # may already be a wrapped class. IDispatch = getattr(IDispatch, "_oleobj_", IDispatch) return IDispatch
def TestVTable(clsctx=pythoncom.CLSCTX_ALL): # Any vtable interfaces marked as dual *should* be able to be # correctly implemented as IDispatch. ob = win32com.client.Dispatch("Python.Test.PyCOMTest") TestLocalVTable(ob) # Now test it via vtable - use some C++ code to help here as Python can't do it directly yet. tester = win32com.client.Dispatch("PyCOMTest.PyCOMTest") testee = pythoncom.CoCreateInstance("Python.Test.PyCOMTest", None, clsctx, pythoncom.IID_IUnknown) # check we fail gracefully with None passed. try: tester.TestMyInterface(None) except pythoncom.com_error, details: pass # and a real object. tester.TestMyInterface(testee)
def CreateGIT(): return pythoncom.CoCreateInstance(pythoncom.CLSID_StdGlobalInterfaceTable, None, pythoncom.CLSCTX_INPROC, pythoncom.IID_IGlobalInterfaceTable)
def __init__(self, site, engine): self.eScript = self.eParse = self.eSafety = None if type(engine) == type(''): engine = pythoncom.CoCreateInstance(engine, None, pythoncom.CLSCTX_SERVER, pythoncom.IID_IUnknown) self.eScript = engine.QueryInterface(axscript.IID_IActiveScript) self.eParse = engine.QueryInterface(axscript.IID_IActiveScriptParse) self.eSafety = engine.QueryInterface(axscript.IID_IObjectSafety) self.eScript.SetScriptSite(site) self.eParse.InitNew()
def TestEngine(): model = {'Test' : util.wrap(ObjectModel()) } scriptDir = "." site = MySite(model) pyEngine = site._AddEngine("Python") # pyEngine2 = site._AddEngine("Python") vbEngine = site._AddEngine("VBScript") # forthEngine = site._AddEngine("ForthScript") try: # code = open(os.path.join(scriptDir, "debugTest.4ths"),"rb").read() # forthEngine.AddCode(code) code = open(os.path.join(scriptDir, "debugTest.pys"),"rb").read() pyEngine.AddCode(code) code = open(os.path.join(scriptDir, "debugTest.vbs"),"rb").read() vbEngine.AddCode(code) # code = open(os.path.join(scriptDir, "debugTestFail.pys"),"rb").read() # pyEngine2.AddCode(code) # from win32com.axdebug import axdebug # sessionProvider=pythoncom.CoCreateInstance(axdebug.CLSID_DefaultDebugSessionProvider,None,pythoncom.CLSCTX_ALL, axdebug.IID_IDebugSessionProvider) # sessionProvider.StartDebugSession(None) raw_input("Press enter to continue") # forthEngine.Start() pyEngine.Start() # Actually run the Python code vbEngine.Start() # Actually run the VB code except pythoncom.com_error, details: print "Script failed: %s (0x%x)" % (details[1], details[0]) # Now run the code expected to fail! # try: # pyEngine2.Start() # Actually run the Python code that fails! # print "Script code worked when it should have failed." # except pythoncom.com_error: # pass site._Close()
def dumpall(): dm=pythoncom.CoCreateInstance(axdebug.CLSID_MachineDebugManager,None,pythoncom.CLSCTX_ALL, axdebug.IID_IMachineDebugManager) e=Enumerator(dm.EnumApplications()) for app in e: print "Application: %s" % app.GetName() node = app.GetRootNode() # of type PyIDebugApplicationNode->PyIDebugDocumentProvider->PyIDebugDocumentInfo DumpDebugApplicationNode(node)
def TestVTable2(): # We once crashed creating our object with the native interface as # the first IID specified. We must do it _after_ the tests, so that # Python has already had the gateway registered from last run. ob = win32com.client.Dispatch("Python.Test.PyCOMTest") iid = pythoncom.InterfaceNames["IPyCOMTest"] clsid = "Python.Test.PyCOMTest" clsctx = pythoncom.CLSCTX_SERVER try: testee = pythoncom.CoCreateInstance(clsid, None, clsctx, iid) except TypeError: # Python can't actually _use_ this interface yet, so this is # "expected". Any COM error is not. pass
def DumpLink(fname): shellLink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink) persistFile = shellLink.QueryInterface(pythoncom.IID_IPersistFile) persistFile.Load(fname,STGM_READ) shellLink.Resolve(0, shell.SLR_ANY_MATCH | shell.SLR_NO_UI) fname, findData = shellLink.GetPath(0) print("Filename:", fname, ", UNC=", shellLink.GetPath(shell.SLGP_UNCPRIORITY)[0]) print("Description:", shellLink.GetDescription()) print("Working Directory:", shellLink.GetWorkingDirectory()) print("Icon:", shellLink.GetIconLocation())
def TestEngine(): model = {'Test' : util.wrap(ObjectModel()) } scriptDir = "." site = MySite(model) pyEngine = site._AddEngine("Python") # pyEngine2 = site._AddEngine("Python") vbEngine = site._AddEngine("VBScript") # forthEngine = site._AddEngine("ForthScript") try: # code = open(os.path.join(scriptDir, "debugTest.4ths"),"rb").read() # forthEngine.AddCode(code) code = open(os.path.join(scriptDir, "debugTest.pys"),"rb").read() pyEngine.AddCode(code) code = open(os.path.join(scriptDir, "debugTest.vbs"),"rb").read() vbEngine.AddCode(code) # code = open(os.path.join(scriptDir, "debugTestFail.pys"),"rb").read() # pyEngine2.AddCode(code) # from win32com.axdebug import axdebug # sessionProvider=pythoncom.CoCreateInstance(axdebug.CLSID_DefaultDebugSessionProvider,None,pythoncom.CLSCTX_ALL, axdebug.IID_IDebugSessionProvider) # sessionProvider.StartDebugSession(None) input("Press enter to continue") # forthEngine.Start() pyEngine.Start() # Actually run the Python code vbEngine.Start() # Actually run the VB code except pythoncom.com_error as details: print("Script failed: %s (0x%x)" % (details[1], details[0])) # Now run the code expected to fail! # try: # pyEngine2.Start() # Actually run the Python code that fails! # print "Script code worked when it should have failed." # except pythoncom.com_error: # pass site._Close()
def dumpall(): dm=pythoncom.CoCreateInstance(axdebug.CLSID_MachineDebugManager,None,pythoncom.CLSCTX_ALL, axdebug.IID_IMachineDebugManager) e=Enumerator(dm.EnumApplications()) for app in e: print("Application: %s" % app.GetName()) node = app.GetRootNode() # of type PyIDebugApplicationNode->PyIDebugDocumentProvider->PyIDebugDocumentInfo DumpDebugApplicationNode(node)