# busco el directorio de instalación (global para que no cambie si usan otra dll) INSTALL_DIR = WSAA.InstallDir = get_install_dir() if __name__ == "__main__": safe_console() if '--register' in sys.argv or '--unregister' in sys.argv: import pythoncom if TYPELIB: if '--register' in sys.argv: tlb = os.path.abspath( os.path.join(INSTALL_DIR, "typelib", "wsaa.tlb")) print "Registering %s" % (tlb, ) tli = pythoncom.LoadTypeLib(tlb) pythoncom.RegisterTypeLib(tli, tlb) elif '--unregister' in sys.argv: k = WSAA pythoncom.UnRegisterTypeLib(k._typelib_guid_, k._typelib_version_[0], k._typelib_version_[1], 0, pythoncom.SYS_WIN32) print "Unregistered typelib" import win32com.server.register win32com.server.register.UseCommandLine(WSAA) elif "/Automate" in sys.argv: # MS seems to like /automate to run the class factories. import win32com.server.localserver #win32com.server.localserver.main() # start the server.
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 Resolve(self): if self.dll is None: return 0 tlb = pythoncom.LoadTypeLib(self.dll) self.FromTypelib(tlb, None) return 1
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) importlib.invalidate_caches() if bToGenDir: progress.SetDescription("Importing module") gencache.AddModuleToCache(info.clsid, info.lcid, info.major, info.minor) progress.Close()