register.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. """Utilities for registering objects.
  2. This module contains utility functions to register Python objects as
  3. valid COM Servers. The RegisterServer function provides all information
  4. necessary to allow the COM framework to respond to a request for a COM object,
  5. construct the necessary Python object, and dispatch COM events.
  6. """
  7. import sys
  8. import win32api
  9. import win32con
  10. import pythoncom
  11. import winerror
  12. import os
  13. CATID_PythonCOMServer = "{B3EF80D0-68E2-11D0-A689-00C04FD658FF}"
  14. def _set_subkeys(keyName, valueDict, base=win32con.HKEY_CLASSES_ROOT):
  15. hkey = win32api.RegCreateKey(base, keyName)
  16. try:
  17. for key, value in valueDict.items():
  18. win32api.RegSetValueEx(hkey, key, None, win32con.REG_SZ, value)
  19. finally:
  20. win32api.RegCloseKey(hkey)
  21. def _set_string(path, value, base=win32con.HKEY_CLASSES_ROOT):
  22. "Set a string value in the registry."
  23. win32api.RegSetValue(base,
  24. path,
  25. win32con.REG_SZ,
  26. value)
  27. def _get_string(path, base=win32con.HKEY_CLASSES_ROOT):
  28. "Get a string value from the registry."
  29. try:
  30. return win32api.RegQueryValue(base, path)
  31. except win32api.error:
  32. return None
  33. def _remove_key(path, base=win32con.HKEY_CLASSES_ROOT):
  34. "Remove a string from the registry."
  35. try:
  36. win32api.RegDeleteKey(base, path)
  37. except win32api.error as xxx_todo_changeme1:
  38. (code, fn, msg) = xxx_todo_changeme1.args
  39. if code != winerror.ERROR_FILE_NOT_FOUND:
  40. raise win32api.error(code, fn, msg)
  41. def recurse_delete_key(path, base=win32con.HKEY_CLASSES_ROOT):
  42. """Recursively delete registry keys.
  43. This is needed since you can't blast a key when subkeys exist.
  44. """
  45. try:
  46. h = win32api.RegOpenKey(base, path)
  47. except win32api.error as xxx_todo_changeme2:
  48. (code, fn, msg) = xxx_todo_changeme2.args
  49. if code != winerror.ERROR_FILE_NOT_FOUND:
  50. raise win32api.error(code, fn, msg)
  51. else:
  52. # parent key found and opened successfully. do some work, making sure
  53. # to always close the thing (error or no).
  54. try:
  55. # remove all of the subkeys
  56. while 1:
  57. try:
  58. subkeyname = win32api.RegEnumKey(h, 0)
  59. except win32api.error as xxx_todo_changeme:
  60. (code, fn, msg) = xxx_todo_changeme.args
  61. if code != winerror.ERROR_NO_MORE_ITEMS:
  62. raise win32api.error(code, fn, msg)
  63. break
  64. recurse_delete_key(path + '\\' + subkeyname, base)
  65. # remove the parent key
  66. _remove_key(path, base)
  67. finally:
  68. win32api.RegCloseKey(h)
  69. def _cat_registrar():
  70. return pythoncom.CoCreateInstance(
  71. pythoncom.CLSID_StdComponentCategoriesMgr,
  72. None,
  73. pythoncom.CLSCTX_INPROC_SERVER,
  74. pythoncom.IID_ICatRegister
  75. )
  76. def _find_localserver_exe(mustfind):
  77. if not sys.platform.startswith("win32"):
  78. return sys.executable
  79. if pythoncom.__file__.find("_d") < 0:
  80. exeBaseName = "pythonw.exe"
  81. else:
  82. exeBaseName = "pythonw_d.exe"
  83. # First see if in the same directory as this .EXE
  84. exeName = os.path.join( os.path.split(sys.executable)[0], exeBaseName )
  85. if not os.path.exists(exeName):
  86. # See if in our sys.prefix directory
  87. exeName = os.path.join( sys.prefix, exeBaseName )
  88. if not os.path.exists(exeName):
  89. # See if in our sys.prefix/pcbuild directory (for developers)
  90. if "64 bit" in sys.version:
  91. exeName = os.path.join( sys.prefix, "PCbuild", "amd64", exeBaseName )
  92. else:
  93. exeName = os.path.join( sys.prefix, "PCbuild", exeBaseName )
  94. if not os.path.exists(exeName):
  95. # See if the registry has some info.
  96. try:
  97. key = "SOFTWARE\\Python\\PythonCore\\%s\\InstallPath" % sys.winver
  98. path = win32api.RegQueryValue( win32con.HKEY_LOCAL_MACHINE, key )
  99. exeName = os.path.join( path, exeBaseName )
  100. except (AttributeError,win32api.error):
  101. pass
  102. if not os.path.exists(exeName):
  103. if mustfind:
  104. raise RuntimeError("Can not locate the program '%s'" % exeBaseName)
  105. return None
  106. return exeName
  107. def _find_localserver_module():
  108. import win32com.server
  109. path = win32com.server.__path__[0]
  110. baseName = "localserver"
  111. pyfile = os.path.join(path, baseName + ".py")
  112. try:
  113. os.stat(pyfile)
  114. except os.error:
  115. # See if we have a compiled extension
  116. if __debug__:
  117. ext = ".pyc"
  118. else:
  119. ext = ".pyo"
  120. pyfile = os.path.join(path, baseName + ext)
  121. try:
  122. os.stat(pyfile)
  123. except os.error:
  124. raise RuntimeError("Can not locate the Python module 'win32com.server.%s'" % baseName)
  125. return pyfile
  126. def RegisterServer(clsid,
  127. pythonInstString=None,
  128. desc=None,
  129. progID=None, verProgID=None,
  130. defIcon=None,
  131. threadingModel="both",
  132. policy=None,
  133. catids=[], other={},
  134. addPyComCat=None,
  135. dispatcher = None,
  136. clsctx = None,
  137. addnPath = None,
  138. ):
  139. """Registers a Python object as a COM Server. This enters almost all necessary
  140. information in the system registry, allowing COM to use the object.
  141. clsid -- The (unique) CLSID of the server.
  142. pythonInstString -- A string holding the instance name that will be created
  143. whenever COM requests a new object.
  144. desc -- The description of the COM object.
  145. progID -- The user name of this object (eg, Word.Document)
  146. verProgId -- The user name of this version's implementation (eg Word.6.Document)
  147. defIcon -- The default icon for the object.
  148. threadingModel -- The threading model this object supports.
  149. policy -- The policy to use when creating this object.
  150. catids -- A list of category ID's this object belongs in.
  151. other -- A dictionary of extra items to be registered.
  152. addPyComCat -- A flag indicating if the object should be added to the list
  153. of Python servers installed on the machine. If None (the default)
  154. then it will be registered when running from python source, but
  155. not registered if running in a frozen environment.
  156. dispatcher -- The dispatcher to use when creating this object.
  157. clsctx -- One of the CLSCTX_* constants.
  158. addnPath -- An additional path the COM framework will add to sys.path
  159. before attempting to create the object.
  160. """
  161. ### backwards-compat check
  162. ### Certain policies do not require a "class name", just the policy itself.
  163. if not pythonInstString and not policy:
  164. raise TypeError('You must specify either the Python Class or Python Policy which implement the COM object.')
  165. keyNameRoot = "CLSID\\%s" % str(clsid)
  166. _set_string(keyNameRoot, desc)
  167. # Also register as an "Application" so DCOM etc all see us.
  168. _set_string("AppID\\%s" % clsid, progID)
  169. # Depending on contexts requested, register the specified server type.
  170. # Set default clsctx.
  171. if not clsctx:
  172. clsctx = pythoncom.CLSCTX_INPROC_SERVER | pythoncom.CLSCTX_LOCAL_SERVER
  173. # And if we are frozen, ignore the ones that don't make sense in this
  174. # context.
  175. if pythoncom.frozen:
  176. assert sys.frozen, "pythoncom is frozen, but sys.frozen is not set - don't know the context!"
  177. if sys.frozen == "dll":
  178. clsctx = clsctx & pythoncom.CLSCTX_INPROC_SERVER
  179. else:
  180. clsctx = clsctx & pythoncom.CLSCTX_LOCAL_SERVER
  181. # Now setup based on the clsctx left over.
  182. if clsctx & pythoncom.CLSCTX_INPROC_SERVER:
  183. # get the module to use for registration.
  184. # nod to Gordon's installer - if sys.frozen and sys.frozendllhandle
  185. # exist, then we are being registered via a DLL - use this DLL as the
  186. # file name.
  187. if pythoncom.frozen:
  188. if hasattr(sys, "frozendllhandle"):
  189. dllName = win32api.GetModuleFileName(sys.frozendllhandle)
  190. else:
  191. raise RuntimeError("We appear to have a frozen DLL, but I don't know the DLL to use")
  192. else:
  193. # Normal case - running from .py file, so register pythoncom's DLL.
  194. # Although now we prefer a 'loader' DLL if it exists to avoid some
  195. # manifest issues (the 'loader' DLL has a manifest, but pythoncom does not)
  196. pythoncom_dir = os.path.dirname(pythoncom.__file__)
  197. suffix = "_d" if "_d" in pythoncom.__file__ else ""
  198. # Always register with the full path to the DLLs.
  199. loadername = os.path.join(
  200. pythoncom_dir,
  201. "pythoncomloader%d%d%s.dll" % (sys.version_info[0], sys.version_info[1], suffix)
  202. )
  203. dllName = loadername if os.path.isfile(loadername) else pythoncom.__file__
  204. _set_subkeys(keyNameRoot + "\\InprocServer32",
  205. { None : dllName,
  206. "ThreadingModel" : threadingModel,
  207. })
  208. else: # Remove any old InProcServer32 registrations
  209. _remove_key(keyNameRoot + "\\InprocServer32")
  210. if clsctx & pythoncom.CLSCTX_LOCAL_SERVER:
  211. if pythoncom.frozen:
  212. # If we are frozen, we write "{exe} /Automate", just
  213. # like "normal" .EXEs do
  214. exeName = win32api.GetShortPathName(sys.executable)
  215. command = '%s /Automate' % (exeName,)
  216. else:
  217. # Running from .py sources - we need to write
  218. # 'python.exe win32com\server\localserver.py {clsid}"
  219. exeName = _find_localserver_exe(1)
  220. exeName = win32api.GetShortPathName(exeName)
  221. pyfile = _find_localserver_module()
  222. command = '%s "%s" %s' % (exeName, pyfile, str(clsid))
  223. _set_string(keyNameRoot + '\\LocalServer32', command)
  224. else: # Remove any old LocalServer32 registrations
  225. _remove_key(keyNameRoot + "\\LocalServer32")
  226. if pythonInstString:
  227. _set_string(keyNameRoot + '\\PythonCOM', pythonInstString)
  228. else:
  229. _remove_key(keyNameRoot + '\\PythonCOM')
  230. if policy:
  231. _set_string(keyNameRoot + '\\PythonCOMPolicy', policy)
  232. else:
  233. _remove_key(keyNameRoot + '\\PythonCOMPolicy')
  234. if dispatcher:
  235. _set_string(keyNameRoot + '\\PythonCOMDispatcher', dispatcher)
  236. else:
  237. _remove_key(keyNameRoot + '\\PythonCOMDispatcher')
  238. if defIcon:
  239. _set_string(keyNameRoot + '\\DefaultIcon', defIcon)
  240. else:
  241. _remove_key(keyNameRoot + '\\DefaultIcon')
  242. if addnPath:
  243. _set_string(keyNameRoot + "\\PythonCOMPath", addnPath)
  244. else:
  245. _remove_key(keyNameRoot + "\\PythonCOMPath")
  246. if addPyComCat is None:
  247. addPyComCat = pythoncom.frozen == 0
  248. if addPyComCat:
  249. catids = catids + [ CATID_PythonCOMServer ]
  250. # Set up the implemented categories
  251. if catids:
  252. regCat = _cat_registrar()
  253. regCat.RegisterClassImplCategories(clsid, catids)
  254. # set up any other reg values they might have
  255. if other:
  256. for key, value in other.items():
  257. _set_string(keyNameRoot + '\\' + key, value)
  258. if progID:
  259. # set the progID as the most specific that was given to us
  260. if verProgID:
  261. _set_string(keyNameRoot + '\\ProgID', verProgID)
  262. else:
  263. _set_string(keyNameRoot + '\\ProgID', progID)
  264. # Set up the root entries - version independent.
  265. if desc:
  266. _set_string(progID, desc)
  267. _set_string(progID + '\\CLSID', str(clsid))
  268. # Set up the root entries - version dependent.
  269. if verProgID:
  270. # point from independent to the current version
  271. _set_string(progID + '\\CurVer', verProgID)
  272. # point to the version-independent one
  273. _set_string(keyNameRoot + '\\VersionIndependentProgID', progID)
  274. # set up the versioned progID
  275. if desc:
  276. _set_string(verProgID, desc)
  277. _set_string(verProgID + '\\CLSID', str(clsid))
  278. def GetUnregisterServerKeys(clsid, progID=None, verProgID=None, customKeys = None):
  279. """Given a server, return a list of of ("key", root), which are keys recursively
  280. and uncondtionally deleted at unregister or uninstall time.
  281. """
  282. # remove the main CLSID registration
  283. ret = [("CLSID\\%s" % str(clsid), win32con.HKEY_CLASSES_ROOT)]
  284. # remove the versioned ProgID registration
  285. if verProgID:
  286. ret.append((verProgID, win32con.HKEY_CLASSES_ROOT))
  287. # blow away the independent ProgID. we can't leave it since we just
  288. # torched the class.
  289. ### could potentially check the CLSID... ?
  290. if progID:
  291. ret.append((progID, win32con.HKEY_CLASSES_ROOT))
  292. # The DCOM config tool may write settings to the AppID key for our CLSID
  293. ret.append( ("AppID\\%s" % str(clsid), win32con.HKEY_CLASSES_ROOT) )
  294. # Any custom keys?
  295. if customKeys:
  296. ret = ret + customKeys
  297. return ret
  298. def UnregisterServer(clsid, progID=None, verProgID=None, customKeys = None):
  299. """Unregisters a Python COM server."""
  300. for args in GetUnregisterServerKeys(clsid, progID, verProgID, customKeys ):
  301. recurse_delete_key(*args)
  302. ### it might be nice at some point to "roll back" the independent ProgID
  303. ### to an earlier version if one exists, and just blowing away the
  304. ### specified version of the ProgID (and its corresponding CLSID)
  305. ### another time, though...
  306. ### NOTE: ATL simply blows away the above three keys without the
  307. ### potential checks that I describe. Assuming that defines the
  308. ### "standard" then we have no additional changes necessary.
  309. def GetRegisteredServerOption(clsid, optionName):
  310. """Given a CLSID for a server and option name, return the option value
  311. """
  312. keyNameRoot = "CLSID\\%s\\%s" % (str(clsid), str(optionName))
  313. return _get_string(keyNameRoot)
  314. def _get(ob, attr, default=None):
  315. try:
  316. return getattr(ob, attr)
  317. except AttributeError:
  318. pass
  319. # look down sub-classes
  320. try:
  321. bases = ob.__bases__
  322. except AttributeError:
  323. # ob is not a class - no probs.
  324. return default
  325. for base in bases:
  326. val = _get(base, attr, None)
  327. if val is not None:
  328. return val
  329. return default
  330. def RegisterClasses(*classes, **flags):
  331. quiet = 'quiet' in flags and flags['quiet']
  332. debugging = 'debug' in flags and flags['debug']
  333. for cls in classes:
  334. clsid = cls._reg_clsid_
  335. progID = _get(cls, '_reg_progid_')
  336. desc = _get(cls, '_reg_desc_', progID)
  337. spec = _get(cls, '_reg_class_spec_')
  338. verProgID = _get(cls, '_reg_verprogid_')
  339. defIcon = _get(cls, '_reg_icon_')
  340. threadingModel = _get(cls, '_reg_threading_', 'both')
  341. catids = _get(cls, '_reg_catids_', [])
  342. options = _get(cls, '_reg_options_', {})
  343. policySpec = _get(cls, '_reg_policy_spec_')
  344. clsctx = _get(cls, '_reg_clsctx_')
  345. tlb_filename = _get(cls, '_reg_typelib_filename_')
  346. # default to being a COM category only when not frozen.
  347. addPyComCat = not _get(cls, '_reg_disable_pycomcat_', pythoncom.frozen!=0)
  348. addnPath = None
  349. if debugging:
  350. # If the class has a debugging dispatcher specified, use it, otherwise
  351. # use our default dispatcher.
  352. dispatcherSpec = _get(cls, '_reg_debug_dispatcher_spec_')
  353. if dispatcherSpec is None:
  354. dispatcherSpec = "win32com.server.dispatcher.DefaultDebugDispatcher"
  355. # And remember the debugging flag as servers may wish to use it at runtime.
  356. debuggingDesc = "(for debugging)"
  357. options['Debugging'] = "1"
  358. else:
  359. dispatcherSpec = _get(cls, '_reg_dispatcher_spec_')
  360. debuggingDesc = ""
  361. options['Debugging'] = "0"
  362. if spec is None:
  363. moduleName = cls.__module__
  364. if moduleName == '__main__':
  365. # Use argv[0] to determine the module name.
  366. try:
  367. # Use the win32api to find the case-sensitive name
  368. moduleName = os.path.splitext(win32api.FindFiles(sys.argv[0])[0][8])[0]
  369. except (IndexError, win32api.error):
  370. # Can't find the script file - the user must explicitely set the _reg_... attribute.
  371. raise TypeError("Can't locate the script hosting the COM object - please set _reg_class_spec_ in your object")
  372. spec = moduleName + "." + cls.__name__
  373. # Frozen apps don't need their directory on sys.path
  374. if not pythoncom.frozen:
  375. scriptDir = os.path.split(sys.argv[0])[0]
  376. if not scriptDir: scriptDir = "."
  377. addnPath = win32api.GetFullPathName(scriptDir)
  378. RegisterServer(clsid, spec, desc, progID, verProgID, defIcon,
  379. threadingModel, policySpec, catids, options,
  380. addPyComCat, dispatcherSpec, clsctx, addnPath)
  381. if not quiet:
  382. print('Registered:', progID or spec, debuggingDesc)
  383. # Register the typelibrary
  384. if tlb_filename:
  385. tlb_filename = os.path.abspath(tlb_filename)
  386. typelib = pythoncom.LoadTypeLib(tlb_filename)
  387. pythoncom.RegisterTypeLib(typelib, tlb_filename)
  388. if not quiet:
  389. print('Registered type library:', tlb_filename)
  390. extra = flags.get('finalize_register')
  391. if extra:
  392. extra()
  393. def UnregisterClasses(*classes, **flags):
  394. quiet = 'quiet' in flags and flags['quiet']
  395. for cls in classes:
  396. clsid = cls._reg_clsid_
  397. progID = _get(cls, '_reg_progid_')
  398. verProgID = _get(cls, '_reg_verprogid_')
  399. customKeys = _get(cls, '_reg_remove_keys_')
  400. unregister_typelib = _get(cls, '_reg_typelib_filename_') is not None
  401. UnregisterServer(clsid, progID, verProgID, customKeys)
  402. if not quiet:
  403. print('Unregistered:', progID or str(clsid))
  404. if unregister_typelib:
  405. tlb_guid = _get(cls, "_typelib_guid_")
  406. if tlb_guid is None:
  407. # I guess I could load the typelib, but they need the GUID anyway.
  408. print("Have typelib filename, but no GUID - can't unregister")
  409. else:
  410. major, minor = _get(cls, "_typelib_version_", (1,0))
  411. lcid = _get(cls, "_typelib_lcid_", 0)
  412. try:
  413. pythoncom.UnRegisterTypeLib(tlb_guid, major, minor, lcid)
  414. if not quiet:
  415. print('Unregistered type library')
  416. except pythoncom.com_error:
  417. pass
  418. extra = flags.get('finalize_unregister')
  419. if extra:
  420. extra()
  421. #
  422. # Unregister info is for installers or external uninstallers.
  423. # The WISE installer, for example firstly registers the COM server,
  424. # then queries for the Unregister info, appending it to its
  425. # install log. Uninstalling the package will the uninstall the server
  426. def UnregisterInfoClasses(*classes, **flags):
  427. ret = []
  428. for cls in classes:
  429. clsid = cls._reg_clsid_
  430. progID = _get(cls, '_reg_progid_')
  431. verProgID = _get(cls, '_reg_verprogid_')
  432. customKeys = _get(cls, '_reg_remove_keys_')
  433. ret = ret + GetUnregisterServerKeys(clsid, progID, verProgID, customKeys)
  434. return ret
  435. # Attempt to 're-execute' our current process with elevation.
  436. def ReExecuteElevated(flags):
  437. from win32com.shell.shell import ShellExecuteEx
  438. from win32com.shell import shellcon
  439. import win32process, win32event
  440. import winxpgui # we've already checked we are running XP above
  441. import tempfile
  442. if not flags['quiet']:
  443. print("Requesting elevation and retrying...")
  444. new_params = " ".join(['"' + a + '"' for a in sys.argv])
  445. # If we aren't already in unattended mode, we want our sub-process to
  446. # be.
  447. if not flags['unattended']:
  448. new_params += " --unattended"
  449. # specifying the parent means the dialog is centered over our window,
  450. # which is a good usability clue.
  451. # hwnd is unlikely on the command-line, but flags may come from elsewhere
  452. hwnd = flags.get('hwnd', None)
  453. if hwnd is None:
  454. try:
  455. hwnd = winxpgui.GetConsoleWindow()
  456. except winxpgui.error:
  457. hwnd = 0
  458. # Redirect output so we give the user some clue what went wrong. This
  459. # also means we need to use COMSPEC. However, the "current directory"
  460. # appears to end up ignored - so we execute things via a temp batch file.
  461. tempbase = tempfile.mktemp("pycomserverreg")
  462. outfile = tempbase + ".out"
  463. batfile = tempbase + ".bat"
  464. # If registering from pythonwin, need to run python console instead since
  465. # pythonwin will just open script for editting
  466. current_exe = os.path.split(sys.executable)[1].lower()
  467. exe_to_run = None
  468. if current_exe == 'pythonwin.exe':
  469. exe_to_run = os.path.join(sys.prefix, 'python.exe')
  470. elif current_exe == 'pythonwin_d.exe':
  471. exe_to_run = os.path.join(sys.prefix, 'python_d.exe')
  472. if not exe_to_run or not os.path.exists(exe_to_run):
  473. exe_to_run = sys.executable
  474. try:
  475. batf = open(batfile, "w")
  476. try:
  477. cwd = os.getcwd()
  478. print("@echo off", file=batf)
  479. # nothing is 'inherited' by the elevated process, including the
  480. # environment. I wonder if we need to set more?
  481. print("set PYTHONPATH=%s" % os.environ.get('PYTHONPATH', ''), file=batf)
  482. # may be on a different drive - select that before attempting to CD.
  483. print(os.path.splitdrive(cwd)[0], file=batf)
  484. print('cd "%s"' % os.getcwd(), file=batf)
  485. print('%s %s > "%s" 2>&1' % (win32api.GetShortPathName(exe_to_run), new_params, outfile), file=batf)
  486. finally:
  487. batf.close()
  488. executable = os.environ.get('COMSPEC', 'cmd.exe')
  489. rc = ShellExecuteEx(hwnd=hwnd,
  490. fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
  491. lpVerb="runas",
  492. lpFile=executable,
  493. lpParameters='/C "%s"' % batfile,
  494. nShow=win32con.SW_SHOW)
  495. hproc = rc['hProcess']
  496. win32event.WaitForSingleObject(hproc, win32event.INFINITE)
  497. exit_code = win32process.GetExitCodeProcess(hproc)
  498. outf = open(outfile)
  499. try:
  500. output = outf.read()
  501. finally:
  502. outf.close()
  503. if exit_code:
  504. # Even if quiet you get to see this message.
  505. print("Error: registration failed (exit code %s)." % exit_code)
  506. # if we are quiet then the output if likely to already be nearly
  507. # empty, so always print it.
  508. print(output, end=' ')
  509. finally:
  510. for f in (outfile, batfile):
  511. try:
  512. os.unlink(f)
  513. except os.error as exc:
  514. print("Failed to remove tempfile '%s': %s" % (f, exc))
  515. def UseCommandLine(*classes, **flags):
  516. unregisterInfo = '--unregister_info' in sys.argv
  517. unregister = '--unregister' in sys.argv
  518. flags['quiet'] = flags.get('quiet',0) or '--quiet' in sys.argv
  519. flags['debug'] = flags.get('debug',0) or '--debug' in sys.argv
  520. flags['unattended'] = flags.get('unattended',0) or '--unattended' in sys.argv
  521. if unregisterInfo:
  522. return UnregisterInfoClasses(*classes, **flags)
  523. try:
  524. if unregister:
  525. UnregisterClasses(*classes, **flags)
  526. else:
  527. RegisterClasses(*classes, **flags)
  528. except win32api.error as exc:
  529. # If we are on xp+ and have "access denied", retry using
  530. # ShellExecuteEx with 'runas' verb to force elevation (vista) and/or
  531. # admin login dialog (vista/xp)
  532. if flags['unattended'] or exc.winerror != winerror.ERROR_ACCESS_DENIED \
  533. or sys.getwindowsversion()[0] < 5:
  534. raise
  535. ReExecuteElevated(flags)
  536. def RegisterPyComCategory():
  537. """ Register the Python COM Server component category.
  538. """
  539. regCat = _cat_registrar()
  540. regCat.RegisterCategories( [ (CATID_PythonCOMServer,
  541. 0x0409,
  542. "Python COM Server") ] )
  543. if not pythoncom.frozen:
  544. try:
  545. win32api.RegQueryValue(win32con.HKEY_CLASSES_ROOT,
  546. 'Component Categories\\%s' % CATID_PythonCOMServer)
  547. except win32api.error:
  548. try:
  549. RegisterPyComCategory()
  550. except pythoncom.error: # Error with the COM category manager - oh well.
  551. pass