It's possible to register and use a class without privileges. The class has to be registered in the current user instead of all users.
The option is not provided, so you'll have to register it yourself by writing the keys in HKCUSOFTWAREClasses
.
Here's a working example:
import os, sys, win32api, win32con, win32com.server.register
class HelloWorld(object):
_reg_progid_ = "Python.TestServer"
_reg_clsid_ = "{7CC9F362-486D-11D1-BB48-0000E838A65F}"
_reg_desc_ = "Python Test COM Server"
_public_methods_ = ['Hello']
def Hello(self):
return "Hello!"
def RegisterClass(cls):
file = os.path.abspath(sys.modules[cls.__module__].__file__)
folder = os.path.dirname(file)
module = os.path.splitext(os.path.basename(file))[0]
python = win32com.server.register._find_localserver_exe(1)
python = win32api.GetShortPathName(python)
server = win32com.server.register._find_localserver_module()
command = '%s "%s" %s' % (python, server, cls._reg_clsid_)
typename = module + "." + cls.__name__
def write(path, value):
win32api.RegSetValue(win32con.HKEY_CURRENT_USER, path, win32con.REG_SZ, value)
write("SOFTWARE\Classes" + cls._reg_progid_ + '\CLSID', cls._reg_clsid_)
write("SOFTWARE\Classes\AppID" + cls._reg_clsid_, cls._reg_progid_)
write("SOFTWARE\Classes\CLSID" + cls._reg_clsid_, cls._reg_desc_)
write("SOFTWARE\Classes\CLSID" + cls._reg_clsid_ + '\LocalServer32', command)
write("SOFTWARE\Classes\CLSID" + cls._reg_clsid_ + '\ProgID', cls._reg_progid_)
write("SOFTWARE\Classes\CLSID" + cls._reg_clsid_ + '\PythonCOMPath', folder)
write("SOFTWARE\Classes\CLSID" + cls._reg_clsid_ + '\PythonCOM', typename)
write("SOFTWARE\Classes\CLSID" + cls._reg_clsid_ + '\Debugging', "0")
print("Registered %s" % cls.__name__)
if __name__ == '__main__':
RegisterClass(HelloWorld)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…