从.NET消费Python的COM服务器 [英] Consuming Python COM Server from .NET

查看:445
本文介绍了从.NET消费Python的COM服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用win32com扩展来实现的Python COM服务器。 然后从.NET中消耗服务器。 我用下面的例子来实现COM服务器,它运行没有问题,但是当我尝试使用它使用C#我FileNotFoundException异常,出现以下消息检索的组件的COM类工厂CLSID {676E38A6-7FA7-4BFF-9179 -AE959734DEBB}失败,原因是以下错误:8007007E 。我张贴的C#code作为well.I不知道如果我想的东西我会AP preciate任何帮助。

谢谢, 莎拉

 #PythonCOMServer.py

进口pythoncom
类PythonUtilities:
    _public_methods_ = ['SplitString']
    _reg_progid_ =PythonDemos.Utilities
    #切勿复制下面的ID

    #使用打印pythoncom.CreateGuid(),以使一个新的。
    _reg_clsid_ = pythoncom.CreateGuid()
    打印_reg_clsid_
    高清SplitString(个体经营,缬氨酸,项目=无):
        进口字符串
        如果项目=无:项目= STR(项目)
        返回string.split(STR(VAL)项)

#添加code,这样当该脚本被运行
#Python.exe,。它自行注册。

如果__name __ =='__ main__:
    打印注册COM服务器
    进口win32com.server.register
    win32com.server.register.UseCommandLine(PythonUtilities)


// C#的code
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Reflection;

命名空间ConsoleApplication2
{
    类节目
    {
        静态无效的主要(字串[] args)
        {

              键入pythonServer;
              对象pythonObject;
              pythonServer = Type.GetTypeFromProgID(PythonDemos.Utilities);
              pythonObject = Activator.CreateInstance(pythonServer);

        }
    }
}
 

解决方案

一个COM服务器只是一个软件(DLL或可执行文件),将接受远程过程调用(RPC)通过定义的协议。该协议的一部分说,服务器必须有一个唯一的ID,存储在Windows'注册表。 在我们的例子中,这意味着你有注册是不存在的服务器。因此,错误(组件未找到)。

所以,它应该是这样的(像往常一样,这是未经考验code!):

 进口pythoncom

一流的HelloWorld:
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    _reg_clsid_ ={B83DD222-7750-413D-A9AD-01B37021B24B}
    _reg_desc_ =Python的测试COM服务器
    _reg_progid_ =Python.TestServer
    _public_methods_ = ['你好']
    _public_attrs_ = ['softspace','noCalls']
    _readonly_attrs_ = ['noCalls']

    高清__init __(个体经营):
        self.softspace = 1
        self.noCalls = 0

    高清你好(个体经营,谁):
        self.noCalls = self.noCalls + 1
        #插入softspace数量的空格
        回归你好+,* self.softspace + STR(谁)

如果__name__ =='__main__':
    如果在sys.argv中'--register'[1:]或sys.argv中'--unregister'[1:]:
        进口win32com.server.register
        win32com.server.register.UseCommandLine(的HelloWorld)
    其他:
        #启动服务器。
        从win32com.server进口的LocalServer
        localserver.serve('B83DD222-7750-413D-A9AD-01B37021B24B)
 

那你应该在命令行中运行(假设脚本被称为HelloWorldCOM.py):

 HelloWorldCOM.py --register
HelloWorldCOM.py 

类的HelloWorld是服务器的实际实现。它公开一个方法(你好)和一对属性的,其中的两个是只读。 随着第一个命令,你注册服务器;与第二个,则运行它,然后将它变为可用从其他应用程序使用。

I wanted to implement python com server using win32com extensions. Then consume the server from within the .NET. I used the following example to implement the com server and it runs without a problem but when I try to consume it using C# I got FileNotFoundException with the following message "Retrieving the COM class factory for component with CLSID {676E38A6-7FA7-4BFF-9179-AE959734DEBB} failed due to the following error: 8007007e." . I posted the C# code as well.I wonder if I'm missing something I would appreciate any help.

Thanks, Sarah

#PythonCOMServer.py

import pythoncom
class PythonUtilities:
    _public_methods_ = [ 'SplitString' ]
    _reg_progid_ = "PythonDemos.Utilities"
    # NEVER copy the following ID

    # Use"print pythoncom.CreateGuid()" to make a new one.
    _reg_clsid_ = pythoncom.CreateGuid()
    print _reg_clsid_
    def SplitString(self, val, item=None):
        import string
        if item != None: item = str(item)
        return string.split(str(val), item)

# Add code so that when this script is run by
# Python.exe,.it self-registers.

if __name__=='__main__':        
    print 'Registering Com Server'
    import win32com.server.register
    win32com.server.register.UseCommandLine(PythonUtilities)


// the C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

              Type pythonServer;
              object pythonObject;
              pythonServer = Type.GetTypeFromProgID("PythonDemos.Utilities");
              pythonObject = Activator.CreateInstance(pythonServer);

        }
    }
}

解决方案

A COM server is just a piece of software (a DLL or an executable) that will accept remote procedure calls (RPC) through a defined protocol. Part of the protocol says that the server must have a unique ID, stored in the Windows' registry. In our case, this means that you have "registered" a server that is not existing. Thus the error (component not found).

So, it should be something like this (as usual, this is untested code!):

import pythoncom

class HelloWorld:
    _reg_clsctx_ = pythoncom.CLSCTX_LOCAL_SERVER
    _reg_clsid_ = "{B83DD222-7750-413D-A9AD-01B37021B24B}"
    _reg_desc_ = "Python Test COM Server"
    _reg_progid_ = "Python.TestServer"
    _public_methods_ = ['Hello']
    _public_attrs_ = ['softspace', 'noCalls']
    _readonly_attrs_ = ['noCalls']

    def __init__(self):
        self.softspace = 1
        self.noCalls = 0

    def Hello(self, who):
        self.noCalls = self.noCalls + 1
        # insert "softspace" number of spaces
        return "Hello" + " " * self.softspace + str(who)

if __name__ == '__main__':
    if '--register' in sys.argv[1:]  or '--unregister' in sys.argv[1:]:
        import win32com.server.register
        win32com.server.register.UseCommandLine(HelloWorld)
    else:
        # start the server.
        from win32com.server import localserver
        localserver.serve('B83DD222-7750-413D-A9AD-01B37021B24B')

Then you should run from the command line (assuming the script is called HelloWorldCOM.py):

HelloWorldCOM.py --register
HelloWorldCOM.py

Class HelloWorld is the actual implementation of the server. It expose one method (Hello) and a couple of attributes, one of the two is read-only. With the first command, you register the server; with the second one, you run it and then it becomes available to usage from other applications.

这篇关于从.NET消费Python的COM服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆