19 lines
535 B
Python
19 lines
535 B
Python
"""Balanced COM initialization without tearing down an existing MTA caller."""
|
|
|
|
|
|
def initialize_com():
|
|
import pythoncom
|
|
try:
|
|
pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
|
|
except pythoncom.com_error as error:
|
|
if error.hresult != -2147417850: # RPC_E_CHANGED_MODE
|
|
raise
|
|
return False # Existing apartment is usable; no reference was added.
|
|
return True
|
|
|
|
|
|
def uninitialize_com(initialized):
|
|
if initialized:
|
|
import pythoncom
|
|
pythoncom.CoUninitialize()
|