def GetTypeLibsForSpec(arg): """Given an argument on the command line (either a file name, library description, or ProgID of an object) return a list of actual typelibs to use. """ typelibs = [] try: try: tlb = pythoncom.LoadTypeLib(arg) spec = selecttlb.TypelibSpec(None, 0, 0, 0) spec.FromTypelib(tlb, arg) typelibs.append((tlb, spec)) except pythoncom.com_error: # See if it is a description tlbs = selecttlb.FindTlbsWithDescription(arg) if len(tlbs) == 0: # Maybe it is the name of a COM object? try: ob = Dispatch(arg) # and if so, it must support typelib info tlb, index = ob._oleobj_.GetTypeInfo( ).GetContainingTypeLib() spec = selecttlb.TypelibSpec(None, 0, 0, 0) spec.FromTypelib(tlb) tlbs.append(spec) except pythoncom.com_error: pass if len(tlbs) == 0: print("Could not locate a type library matching '%s'" % (arg)) for spec in tlbs: # Version numbers not always reliable if enumerated from registry. # (as some libs use hex, other's dont. Both examples from MS, of course.) if spec.dll is None: tlb = pythoncom.LoadRegTypeLib(spec.clsid, spec.major, spec.minor, spec.lcid) else: tlb = pythoncom.LoadTypeLib(spec.dll) # We have a typelib, but it may not be exactly what we specified # (due to automatic version matching of COM). So we query what we really have! attr = tlb.GetLibAttr() spec.major = attr[3] spec.minor = attr[4] spec.lcid = attr[1] typelibs.append((tlb, spec)) return typelibs except pythoncom.com_error: t, v, tb = sys.exc_info() sys.stderr.write("Unable to load type library from '%s' - %s\n" % (arg, v)) tb = None # Storing tb in a local is a cycle! sys.exit(1)
def OnFileOpen(self, id, code): openFlags = win32con.OFN_OVERWRITEPROMPT | win32con.OFN_FILEMUSTEXIST fspec = "Type Libraries (*.tlb, *.olb)|*.tlb;*.olb|OCX Files (*.ocx)|*.ocx|DLL's (*.dll)|*.dll|All Files (*.*)|*.*||" dlg = win32ui.CreateFileDialog(1, None, None, openFlags, fspec) if dlg.DoModal() == win32con.IDOK: try: self.tlb = pythoncom.LoadTypeLib(dlg.GetPathName()) except pythoncom.ole_error: self.MessageBox("The file does not contain type information") self.tlb = None self._SetupTLB()
def __init__(self, typefile=None): TypeBrowseDialog_Parent.__init__(self, self.GetTemplate()) try: if typefile: self.tlb = pythoncom.LoadTypeLib(typefile) else: self.tlb = None except pythoncom.ole_error: self.MessageBox("The file does not contain type information") self.tlb = None self.HookCommand(self.CmdTypeListbox, self.IDC_TYPELIST) self.HookCommand(self.CmdMemberListbox, self.IDC_MEMBERLIST)
def GetSubList(self): ret = [] ret.append(browser.MakeHLI(self.myobject, "Filename")) try: tlb = pythoncom.LoadTypeLib(self.myobject) except pythoncom.com_error: return [browser.MakeHLI("%s can not be loaded" % self.myobject)] for i in range(tlb.GetTypeInfoCount()): try: ret.append(HLITypeKinds[tlb.GetTypeInfoType(i)][0]((tlb, i))) except pythoncom.com_error: ret.append(browser.MakeHLI("The type info can not be loaded!")) ret.sort() return ret
def Resolve(self): if self.dll is None: return 0 tlb = pythoncom.LoadTypeLib(self.dll) self.FromTypelib(tlb, None) return 1
def RegisterClasses(*classes, **flags): quiet = 'quiet' in flags and flags['quiet'] debugging = 'debug' in flags and flags['debug'] for cls in classes: clsid = cls._reg_clsid_ progID = _get(cls, '_reg_progid_') desc = _get(cls, '_reg_desc_', progID) spec = _get(cls, '_reg_class_spec_') verProgID = _get(cls, '_reg_verprogid_') defIcon = _get(cls, '_reg_icon_') threadingModel = _get(cls, '_reg_threading_', 'both') catids = _get(cls, '_reg_catids_', []) options = _get(cls, '_reg_options_', {}) policySpec = _get(cls, '_reg_policy_spec_') clsctx = _get(cls, '_reg_clsctx_') tlb_filename = _get(cls, '_reg_typelib_filename_') # default to being a COM category only when not frozen. addPyComCat = not _get(cls, '_reg_disable_pycomcat_', pythoncom.frozen != 0) addnPath = None if debugging: # If the class has a debugging dispatcher specified, use it, otherwise # use our default dispatcher. dispatcherSpec = _get(cls, '_reg_debug_dispatcher_spec_') if dispatcherSpec is None: dispatcherSpec = "win32com.server.dispatcher.DefaultDebugDispatcher" # And remember the debugging flag as servers may wish to use it at # runtime. debuggingDesc = "(for debugging)" options['Debugging'] = "1" else: dispatcherSpec = _get(cls, '_reg_dispatcher_spec_') debuggingDesc = "" options['Debugging'] = "0" if spec is None: moduleName = cls.__module__ if moduleName == '__main__': # Use argv[0] to determine the module name. try: # Use the win32api to find the case-sensitive name moduleName = os.path.splitext( win32api.FindFiles(sys.argv[0])[0][8])[0] except (IndexError, win32api.error): # Can't find the script file - the user must explicitely # set the _reg_... attribute. raise TypeError( "Can't locate the script hosting the COM object - please set _reg_class_spec_ in your object" ) spec = moduleName + "." + cls.__name__ # Frozen apps don't need their directory on sys.path if not pythoncom.frozen: scriptDir = os.path.split(sys.argv[0])[0] if not scriptDir: scriptDir = "." addnPath = win32api.GetFullPathName(scriptDir) RegisterServer(clsid, spec, desc, progID, verProgID, defIcon, threadingModel, policySpec, catids, options, addPyComCat, dispatcherSpec, clsctx, addnPath) if not quiet: print('Registered:', progID or spec, debuggingDesc) # Register the typelibrary if tlb_filename: tlb_filename = os.path.abspath(tlb_filename) typelib = pythoncom.LoadTypeLib(tlb_filename) pythoncom.RegisterTypeLib(typelib, tlb_filename) if not quiet: print('Registered type library:', tlb_filename) extra = flags.get('finalize_register') if extra: extra()
def GenerateFromTypeLibSpec(typelibInfo, file=None, verboseLevel=None, progressInstance=None, bUnicodeToString=None, bForDemand=bForDemandDefault, bBuildHidden=1): assert bUnicodeToString is None, "this is deprecated and will go away" if verboseLevel is None: verboseLevel = 0 # By default, we use no gui and no verbose level! if bForDemand and file is not None: raise RuntimeError( "You can only perform a demand-build when the output goes to the gen_py directory" ) if isinstance(typelibInfo, tuple): # Tuple typelibCLSID, lcid, major, minor = typelibInfo tlb = pythoncom.LoadRegTypeLib(typelibCLSID, major, minor, lcid) spec = selecttlb.TypelibSpec(typelibCLSID, lcid, major, minor) spec.FromTypelib(tlb, str(typelibCLSID)) typelibs = [(tlb, spec)] elif isinstance(typelibInfo, selecttlb.TypelibSpec): if typelibInfo.dll is None: # Version numbers not always reliable if enumerated from registry. tlb = pythoncom.LoadRegTypeLib(typelibInfo.clsid, typelibInfo.major, typelibInfo.minor, typelibInfo.lcid) else: tlb = pythoncom.LoadTypeLib(typelibInfo.dll) typelibs = [(tlb, typelibInfo)] elif hasattr(typelibInfo, "GetLibAttr"): # A real typelib object! # Could also use isinstance(typelibInfo, PyITypeLib) instead, but PyITypeLib is not directly exposed by pythoncom. # pythoncom.TypeIIDs[pythoncom.IID_ITypeLib] seems to work tla = typelibInfo.GetLibAttr() guid = tla[0] lcid = tla[1] major = tla[3] minor = tla[4] spec = selecttlb.TypelibSpec(guid, lcid, major, minor) typelibs = [(typelibInfo, spec)] else: typelibs = GetTypeLibsForSpec(typelibInfo) if progressInstance is None: progressInstance = SimpleProgress(verboseLevel) progress = progressInstance bToGenDir = (file is None) for typelib, info in typelibs: gen = genpy.Generator(typelib, info.dll, progress, bBuildHidden=bBuildHidden) if file is None: this_name = gencache.GetGeneratedFileName(info.clsid, info.lcid, info.major, info.minor) full_name = os.path.join(gencache.GetGeneratePath(), this_name) if bForDemand: try: os.unlink(full_name + ".py") except os.error: pass try: os.unlink(full_name + ".pyc") except os.error: pass try: os.unlink(full_name + ".pyo") except os.error: pass if not os.path.isdir(full_name): os.mkdir(full_name) outputName = os.path.join(full_name, "__init__.py") else: outputName = full_name + ".py" fileUse = gen.open_writer(outputName) progress.LogBeginGenerate(outputName) else: fileUse = file worked = False try: gen.generate(fileUse, bForDemand) worked = True finally: if file is None: gen.finish_writer(outputName, fileUse, worked) if bToGenDir: progress.SetDescription("Importing module") gencache.AddModuleToCache(info.clsid, info.lcid, info.major, info.minor) progress.Close()