如何将Python com服务器导入Ironpython

2024-10-08 20:16:34 发布

您现在位置:Python中文网/ 问答频道 /正文

可以将Python COM服务器导入Ironpython程序吗?在

我这里的意思是Python类脚本(*.py),它使用win32com.server.register.UseCommandLine()注册为COM服务器,然后使用Activator.CreateInstance(Type.GetTypeFromProgID('python.progid'))从Ironpython调用。在

我试了好几次,但总是失败。从回溯中可以看出,Ironpython尝试执行脚本而不是导入脚本,这当然失败了。据我所知,作为一个COM对象,脚本应该由Python运行,而不是Ironpython。Ironpython将使用输出。但是,使用相同的Activator.CreateInstance()命令,它可以从C#导入COM服务器。在C中,我可以调用、传递和检索COM类中的方法的值。在

下面是py脚本:

    class TestCom:
        _reg_progid_ =  "TestCom.DisplayText"
        _reg_clsid_ = "{B02B1819-2954-4E47-8E19-570652B38DF2}"

        def __init__(self):
            self.DisplayThis = "Hello from python world"
            self.aText = ""

        def DeliverText(self, aText):
            "say hello"
            if aText <> "": self.DisplayThis = aText
            return self.DisplayThis

    if __name__=='__main__':
        import sys
        sys.path.append(r'C:\Python27')
        sys.path.append(r'C:\Python27\Lib')
        sys.path.append(r'C:\Python27\Lib\site-Packages')
        print "Registering COM server..."
        import win32com.server.register
        win32com.server.register.UseCommandLine(TestCom)

下面是来自Ironpython的命令(有错误):

^{pr2}$

顺便说一句,我不熟悉IronPython和Python。我都使用2.7版本。在

谢谢你的帮助。在


Tags: pathself服务器脚本comregisterserversys

热门问题