如何将Python com服务器导入Ironpython

如何将Python com服务器导入Ironpython,python,com,ironpython,Python,Com,Ironpython,是否可以将Python COM服务器导入到Ironpython程序中 这里我指的是Python类脚本(*.py),它使用win32com.server.register.usecondline()注册为COM服务器,然后使用Activator.CreateInstance(Type.GetTypeFromProgID('Python.progid'))从Ironpython调用 我试了好几次,但总是失败。从回溯中可以看出,Ironpython试图执行脚本,而不是导入脚本,这当然失败了。据我所知,

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

这里我指的是Python类脚本(*.py),它使用
win32com.server.register.usecondline()
注册为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)
classtestcom:
_reg\u progid=“TestCom.DisplayText”
_reg_clsid_u382;=“{B02B1819-2954-4E47-8E19-570652B38DF2}”
定义初始化(自):
self.DisplayThis=“来自python世界的您好”
self.aText=“”
def DeliverText(自身、aText):
“打招呼”
如果aText“”:self.DisplayThis=aText
返回self.DisplayThis
如果“名称”=“\uuuuuuuu主要”:
导入系统
sys.path.append(r'C:\Python27')
append(r'C:\Python27\Lib')
append(r'C:\Python27\Lib\site Packages')
打印“正在注册COM服务器…”
导入win32com.server.register
win32com.server.register.UseCommandLine(TestCom)
下面是来自Ironpython的命令(带有错误):

>来自系统导入类型,激活器
>>>o=Activator.CreateInstance(Type.GetTypeFromProgID('TestCom.DisplayText'))
pythoncom错误:pythoncom服务器-无法加载“win32com.Server.policy”模块。
回溯(最近一次呼叫最后一次):
文件“C:\Python27\Lib\site Packages\win32com\\uuuu init\uuuuu.py”,第5行,在
导入win32api、sys、os
ImportError:没有名为win32api的模块
pythoncom错误:CPyFactory::CreateInstance未能创建实例。(80004005)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
环境错误:System.Runtime.InteropServices.COMException(0x80004005):由于以下错误,从IClassFactory创建CLSID为{B02B1819-2954-4E47-8E19-570652B38DF2}的COM组件实例失败:80004005。
在System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandleInternal&ctor、Boolean&bNeedSecurityCheck)
位于System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache)
位于System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly、布尔skipVisibilityChecks、布尔skipCheckThis、布尔fillCache)
位于System.Activator.CreateInstance(类型,布尔非公共)
位于System.Activator.CreateInstance(类型)
在Microsoft.Scripting.解释器.FuncCallInstruction`2.Run(解释器框架)
在Microsoft.Scripting.解释器.解释器.Run(解释器框架)
位于Microsoft.Scripting.explorer.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0,T1 arg1,T2 arg2,T3 arg3)
在System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](调用站点,T0 arg0,T1 arg1,T2 arg2)
在Microsoft.Scripting.解释器.FuncCallInstruction`6.Run(解释器框架)
在Microsoft.Scripting.解释器.解释器.Run(解释器框架)
位于Microsoft.Scripting.explorer.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0,T1 arg1,T2 arg2,T3 arg3)
在IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(解释器框架)处
在Microsoft.Scripting.解释器.解释器.Run(解释器框架)
位于Microsoft.Scripting.explorer.LightLambda.Run2[T0,T1,TRet](T0 arg0,T1 arg1)
在IronPython.Compiler.PythonScriptCode.RunWorker(codecontextctx)上
在IronPython.Compiler.PythonScriptCode.Run(作用域)
在IronPython.Hosting.PythonCommandLine.c_uudisplayClass1.b_uu0()上
顺便说一句,我不熟悉IronPython和Python。我使用2.7版本来处理这两个问题


感谢您的帮助。

您似乎需要引用脚本
    >>>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()