def __init__( self ):
self._base = pythoncom.CoCreateInstance(
shell.CLSID_ShellLink, None,
pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink
)
python类CoCreateInstance()的实例源码
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 __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 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 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 CreateGIT():
return pythoncom.CoCreateInstance(pythoncom.CLSID_StdGlobalInterfaceTable,
None,
pythoncom.CLSCTX_INPROC,
pythoncom.IID_IGlobalInterfaceTable)
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 __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)
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)
def _cat_registrar():
return pythoncom.CoCreateInstance(
pythoncom.CLSID_StdComponentCategoriesMgr,
None,
pythoncom.CLSCTX_INPROC_SERVER,
pythoncom.IID_ICatRegister
)
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