如何将Python com服务器导入Ironpython

时间:2013-11-01 03:40:28

标签: python com ironpython

是否可以将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的命令(带错误):

    >>>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()

BTW,我是IronPython和Python的新手。我使用2.7版本。

感谢您的帮助。

0 个答案:

没有答案