Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to import Python COM server into Ironpython programs?

What I mean here is Python class script (*.py) which is registered as COM server using win32com.server.register.UseCommandLine(), and then be called from Ironpython using Activator.CreateInstance(Type.GetTypeFromProgID('python.progid')).

I have tried several times but always failed. From the traceback, it shows that Ironpython tries to execute the script rather than importing it, which of course failed. As far as I know, as a COM object, the script should be run by Python, not Ironpython. Ironpython will use the output instead. However, using the same Activator.CreateInstance() command, it is able to import the COM server from C#. In C# I am able to call, pass, and retrieve value from the method within the COM class.

Here is the py script:

    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)

and here is the command from Ironpython (with the error):

    >>>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, I'm new to IronPython and Python. I use the 2.7 version for both.

Thanks for the help.

share|improve this question
    
Seems like you need to quote the script –  Eric Brown Nov 1 '13 at 21:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.