register.py 22 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.iteritems():
  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, (code, fn, msg):
  38. if code != winerror.ERROR_FILE_NOT_FOUND:
  39. raise win32api.error(code, fn, msg)
  40. def recurse_delete_key(path, base=win32con.HKEY_CLASSES_ROOT):
  41. """Recursively delete registry keys.
  42. This is needed since you can't blast a key when subkeys exist.
  43. """
  44. try:
  45. h = win32api.RegOpenKey(base, path)
  46. except win32api.error, (code, fn, msg):
  47. if code != winerror.ERROR_FILE_NOT_FOUND:
  48. raise win32api.error(code, fn, msg)
  49. else:
  50. # parent key found and opened successfully. do some work, making sure
  51. # to always close the thing (error or no).
  52. try:
  53. # remove all of the subkeys
  54. while 1:
  55. try:
  56. subkeyname = win32api.RegEnumKey(h, 0)
  57. except win32api.error, (code, fn, msg):
  58. if code != winerror.ERROR_NO_MORE_ITEMS:
  59. raise win32api.error(code, fn, msg)
  60. break
  61. recurse_delete_key(path + '\\' + subkeyname, base)
  62. # remove the parent key
  63. _remove_key(path, base)
  64. finally:
  65. win32api.RegCloseKey(h)
  66. def _cat_registrar():
  67. return pythoncom.CoCreateInstance(
  68. pythoncom.CLSID_StdComponentCategoriesMgr,
  69. None,
  70. pythoncom.CLSCTX_INPROC_SERVER,
  71. pythoncom.IID_ICatRegister
  72. )
  73. def _find_localserver_exe(mustfind):
  74. if not sys.platform.startswith("win32"):
  75. return sys.executable
  76. if pythoncom.__file__.find("_d") < 0:
  77. exeBaseName = "pythonw.exe"
  78. else:
  79. exeBaseName = "pythonw_d.exe"
  80. # First see if in the same directory as this .EXE
  81. exeName = os.path.join( os.path.split(sys.executable)[0], exeBaseName )
  82. if not os.path.exists(exeName):
  83. # See if in our sys.prefix directory
  84. exeName = os.path.join( sys.prefix, exeBaseName )
  85. if not os.path.exists(exeName):
  86. # See if in our sys.prefix/pcbuild directory (for developers)
  87. if "64 bit" in sys.version:
  88. exeName = os.path.join( sys.prefix, "PCbuild", "amd64", exeBaseName )
  89. else:
  90. exeName = os.path.join( sys.prefix, "PCbuild", exeBaseName )
  91. if not os.path.exists(exeName):
  92. # See if the registry has some info.
  93. try:
  94. key = "SOFTWARE\\Python\\PythonCore\\%s\\InstallPath" % sys.winver
  95. path = win32api.RegQueryValue( win32con.HKEY_LOCAL_MACHINE, key )
  96. exeName = os.path.join( path, exeBaseName )
  97. except (AttributeError,win32api.error):
  98. pass
  99. if not os.path.exists(exeName):
  100. if mustfind:
  101. raise RuntimeError("Can not locate the program '%s'" % exeBaseName)
  102. return None
  103. return exeName
  104. def _find_localserver_module():
  105. import win32com.server
  106. path = win32com.server.__path__[0]
  107. baseName = "localserver"
  108. pyfile = os.path.join(path, baseName + ".py")
  109. try:
  110. os.stat(pyfile)
  111. except os.error:
  112. # See if we have a compiled extension
  113. if __debug__:
  114. ext = ".pyc"
  115. else:
  116. ext = ".pyo"
  117. pyfile = os.path.join(path, baseName + ext)
  118. try:
  119. os.stat(pyfile)
  120. except os.error:
  121. raise RuntimeError("Can not locate the Python module 'win32com.server.%s'" % baseName)
  122. return pyfile
  123. def RegisterServer(clsid,
  124. pythonInstString=None,
  125. desc=None,
  126. progID=None, verProgID=None,
  127. defIcon=None,
  128. threadingModel="both",
  129. policy=None,
  130. catids=[], other={},
  131. addPyComCat=None,
  132. dispatcher = None,
  133. clsctx = None,
  134. addnPath = None,
  135. ):
  136. """Registers a Python object as a COM Server. This enters almost all necessary
  137. information in the system registry, allowing COM to use the object.
  138. clsid -- The (unique) CLSID of the server.
  139. pythonInstString -- A string holding the instance name that will be created
  140. whenever COM requests a new object.
  141. desc -- The description of the COM object.
  142. progID -- The user name of this object (eg, Word.Document)
  143. verProgId -- The user name of this version's implementation (eg Word.6.Document)
  144. defIcon -- The default icon for the object.
  145. threadingModel -- The threading model this object supports.
  146. policy -- The policy to use when creating this object.
  147. catids -- A list of category ID's this object belongs in.
  148. other -- A dictionary of extra items to be registered.
  149. addPyComCat -- A flag indicating if the object should be added to the list
  150. of Python servers installed on the machine. If None (the default)
  151. then it will be registered when running from python source, but
  152. not registered if running in a frozen environment.
  153. dispatcher -- The dispatcher to use when creating this object.
  154. clsctx -- One of the CLSCTX_* constants.
  155. addnPath -- An additional path the COM framework will add to sys.path
  156. before attempting to create the object.
  157. """
  158. ### backwards-compat check
  159. ### Certain policies do not require a "class name", just the policy itself.
  160. if not pythonInstString and not policy:
  161. raise TypeError('You must specify either the Python Class or Python Policy which implement the COM object.')
  162. keyNameRoot = "CLSID\\%s" % str(clsid)
  163. _set_string(keyNameRoot, desc)
  164. # Also register as an "Application" so DCOM etc all see us.
  165. _set_string("AppID\\%s" % clsid, progID)
  166. # Depending on contexts requested, register the specified server type.
  167. # Set default clsctx.
  168. if not clsctx:
  169. clsctx = pythoncom.CLSCTX_INPROC_SERVER | pythoncom.CLSCTX_LOCAL_SERVER
  170. # And if we are frozen, ignore the ones that don't make sense in this
  171. # context.
  172. if pythoncom.frozen:
  173. assert sys.frozen, "pythoncom is frozen, but sys.frozen is not set - don't know the context!"
  174. if sys.frozen == "dll":
  175. clsctx = clsctx & pythoncom.CLSCTX_INPROC_SERVER
  176. else:
  177. clsctx = clsctx & pythoncom.CLSCTX_LOCAL_SERVER
  178. # Now setup based on the clsctx left over.
  179. if clsctx & pythoncom.CLSCTX_INPROC_SERVER:
  180. # get the module to use for registration.
  181. # nod to Gordon's installer - if sys.frozen and sys.frozendllhandle
  182. # exist, then we are being registered via a DLL - use this DLL as the
  183. # file name.
  184. if pythoncom.frozen:
  185. if hasattr(sys, "frozendllhandle"):
  186. dllName = win32api.GetModuleFileName(sys.frozendllhandle)
  187. else:
  188. raise RuntimeError("We appear to have a frozen DLL, but I don't know the DLL to use")
  189. else:
  190. # Normal case - running from .py file, so register pythoncom's DLL.
  191. # Although now we prefer a 'loader' DLL if it exists to avoid some
  192. # manifest issues (the 'loader' DLL has a manifest, but pythoncom does not)
  193. pythoncom_dir = os.path.dirname(pythoncom.__file__)
  194. if pythoncom.__file__.find("_d") < 0:
  195. suffix = ""
  196. else:
  197. suffix = "_d"
  198. loadername = "pythoncomloader%d%d%s.dll" % (sys.version_info[0], sys.version_info[1], suffix)
  199. if os.path.isfile(os.path.join(pythoncom_dir, loadername)):
  200. dllName = loadername
  201. else:
  202. # just use pythoncom.
  203. dllName = os.path.basename(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.iteritems():
  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 >> batf, "@echo off"
  479. # nothing is 'inherited' by the elevated process, including the
  480. # environment. I wonder if we need to set more?
  481. print >> batf, "set PYTHONPATH=%s" % os.environ.get('PYTHONPATH', '')
  482. # may be on a different drive - select that before attempting to CD.
  483. print >> batf, os.path.splitdrive(cwd)[0]
  484. print >> batf, 'cd "%s"' % os.getcwd()
  485. print >> batf, '%s %s > "%s" 2>&1' % (win32api.GetShortPathName(exe_to_run), new_params, outfile)
  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,
  509. finally:
  510. for f in (outfile, batfile):
  511. try:
  512. os.unlink(f)
  513. except os.error, 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, 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