2

是否可以将 Python COM 服务器导入 Ironpython 程序?

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

我尝试了几次,但总是失败。从追溯来看,它表明 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 的命令(有错误):

    >>>from System import Type, Activator
    >>>o = Activator.CreateInstance(Type.GetTypeFromProgID('TestCom.DisplayText'))
    pythoncom error: PythonCOM Server - The 'win32com.server.policy' module could not be loaded.

    Traceback (most recent call last):
      File "C:\Python27\Lib\site-Packages\win32com\__init__.py", line 5, in <module>

        import win32api, sys, os
    ImportError: No module named win32api
    pythoncom error: CPyFactory::CreateInstance failed to create instance. (80004005)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    EnvironmentError: System.Runtime.InteropServices.COMException (0x80004005): Creating an instance of the COM component with CLSID{B02B1819-2954-4E47-8E19-570652B38DF2} from the IClassFactory failed due to the following error: 80004005.
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
       at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
       at Microsoft.Scripting.Interpreter.FuncCallInstruction`6.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
       at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
       at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
       at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
       at IronPython.Hosting.PythonCommandLine.<>c__DisplayClass1.<RunOneInteraction>b__0()

顺便说一句,我是 IronPython 和 Python 的新手。我对两者都使用 2.7 版本。

谢谢您的帮助。

4

0 回答 0