localserver.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # LocalServer .EXE support for Python.
  2. #
  3. # This is designed to be used as a _script_ file by pythonw.exe
  4. #
  5. # In some cases, you could also use Python.exe, which will create
  6. # a console window useful for debugging.
  7. #
  8. # NOTE: When NOT running in any sort of debugging mode,
  9. # 'print' statements may fail, as sys.stdout is not valid!!!
  10. #
  11. # Usage:
  12. # wpython.exe LocalServer.py clsid [, clsid]
  13. import sys
  14. sys.coinit_flags = 2
  15. import pythoncom
  16. import win32api
  17. from win32com.server import factory
  18. usage = """\
  19. Invalid command line arguments
  20. This program provides LocalServer COM support
  21. for Python COM objects.
  22. It is typically run automatically by COM, passing as arguments
  23. The ProgID or CLSID of the Python Server(s) to be hosted
  24. """
  25. def serve(clsids):
  26. infos = factory.RegisterClassFactories(clsids)
  27. pythoncom.EnableQuitMessage(win32api.GetCurrentThreadId())
  28. pythoncom.CoResumeClassObjects()
  29. pythoncom.PumpMessages()
  30. factory.RevokeClassFactories( infos )
  31. pythoncom.CoUninitialize()
  32. def main():
  33. if len(sys.argv)==1:
  34. win32api.MessageBox(0, usage, "Python COM Server")
  35. sys.exit(1)
  36. serve(sys.argv[1:])
  37. if __name__=='__main__':
  38. main()