Advertisement
Guest User

pythoncom

a guest
Jul 18th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.11 KB | None | 0 0
  1. #import pythoncom
  2. #import sys
  3.  
  4. #class HelloWorld:
  5. #    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
  6. #    _reg_clsid_ = "{E5AF2971-0583-4804-9C9A-C6BF5DE33559}"
  7. #    _reg_desc_ = "Python Test COM Server"
  8. #    _reg_progid_ = "Python.TestServer"
  9. #    _public_methods_ = ['Hello']
  10. #    _public_attrs_ = ['softspace', 'noCalls']
  11. #    _readonly_attrs_ = ['noCalls']
  12.  
  13. #    def __init__(self):
  14. #        self.softspace = 1
  15. #        self.noCalls = 0
  16.  
  17. #    def Hello(self, who):
  18. #        self.noCalls = self.noCalls + 1
  19. #        # insert "softspace" number of spaces
  20. #        return "Hello" + " " * self.softspace + str(who)
  21.  
  22. #if __name__ == '__main__':
  23. #    if '--register' in sys.argv[1:]  or '--unregister' in sys.argv[1:]:
  24. #        import win32com.server.register
  25. #        win32com.server.register.UseCommandLine(HelloWorld)
  26. #    else:
  27. #        # start the server.
  28. #        from win32com.server import localserver
  29. #        localserver.serve(['{E5AF2971-0583-4804-9C9A-C6BF5DE33559}'])
  30.        
  31. from subprocess import Popen, PIPE, STDOUT, CREATE_NEW_CONSOLE
  32. import subprocess
  33.  
  34.  
  35.  
  36.  
  37.  
  38. # A little test server, complete with typelib, we can use for testing.
  39. # Originally submitted with bug:
  40. # [ 753154 ] memory leak wrapping object having _typelib_guid_ attribute
  41. # but modified by mhammond for use as part of the test suite.
  42. import sys, os
  43. import pythoncom
  44. import win32com
  45. import winerror
  46. from win32com.server.util import wrap
  47.  
  48. try:
  49.     __file__ # 2.3 only for __main__
  50. except NameError:
  51.     __file__ = sys.argv[0]
  52.  
  53. class CPippo:
  54.     #
  55.     # COM declarations    
  56.     #
  57.     _reg_clsid_ = "{05AC1CCE-3F9B-4d9a-B0B5-DFE8BE45AFA8}"
  58.     _reg_desc_ = "Pippo Python test object"
  59.     _reg_progid_ = "Python.Test.Pippo"
  60.     #_reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER    
  61.     ###
  62.     ### Link to typelib
  63.     _typelib_guid_ = '{41059C57-975F-4B36-8FF3-C5117426647A}'
  64.     _typelib_version_ = 1, 0
  65.     _com_interfaces_ = ['IPippo']
  66.  
  67.     def __init__(self):
  68.         self.MyProp1 = 10
  69.  
  70.     def Method1(self):
  71.         return wrap(CPippo())
  72.  
  73. def BuildTypelib():
  74.     from distutils.dep_util import newer
  75.     this_dir = os.path.dirname(__file__)
  76.     idl = os.path.abspath(os.path.join(this_dir, "pippo.idl"))
  77.     tlb=os.path.splitext(idl)[0] + '.tlb'
  78.     if newer(idl, tlb):
  79.         print "Compiling %s" % (idl,)
  80.        
  81.         #cmd = os.path.abspath(os.path.join(os.path.dirname(__file__), "vcvarsall.bat.lnk"))
  82.         #cmd = '"' + cmd +'"'
  83.         #print cmd
  84.         #cmd1 = 'vcvarsall.bat.lnk'
  85.         #cmd2 = 'midl "%s"' % (idl,)
  86.         #cmdx = "echo x"
  87.  
  88.         #subprocess.call(cmd)
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.         #rc = os.system ('midl "%s"' % (idl,))
  96.         #if rc:
  97.         #    raise RuntimeError, "Compiling MIDL failed!"
  98.         # Can't work out how to prevent MIDL from generating the stubs.
  99.         # just nuke them
  100.         #for fname in "dlldata.c pippo_i.c pippo_p.c pippo.h".split():
  101.         #    os.remove(os.path.join(this_dir, fname))
  102.    
  103.     print "Registering %s" % (tlb,)
  104.     tli=pythoncom.LoadTypeLib(tlb)
  105.     pythoncom.RegisterTypeLib(tli,tlb)
  106.  
  107. def UnregisterTypelib():
  108.     k = CPippo
  109.     try:
  110.         pythoncom.UnRegisterTypeLib(k._typelib_guid_,
  111.                                     k._typelib_version_[0],
  112.                                     k._typelib_version_[1],
  113.                                     0,
  114.                                     pythoncom.SYS_WIN32)
  115.         print "Unregistered typelib"
  116.     except pythoncom.error, details:
  117.         if details[0]==winerror.TYPE_E_REGISTRYACCESS:
  118.             pass
  119.         else:
  120.             raise
  121.  
  122. def main(argv=None):
  123.     if argv is None: argv = sys.argv[1:]
  124.     if '--unregister' in argv:
  125.         # Unregister the type-libraries.
  126.         UnregisterTypelib()
  127.     else:
  128.         # Build and register the type-libraries.
  129.         BuildTypelib()
  130.     import win32com.server.register
  131.     win32com.server.register.UseCommandLine(CPippo)
  132.     # start the server.
  133.     print "run server"
  134.     from win32com.server import localserver
  135.     localserver.serve(['{05AC1CCE-3F9B-4d9a-B0B5-DFE8BE45AFA8}'])
  136.  
  137.  
  138.  
  139. if __name__=='__main__':
  140.     main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement