好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python调用C#Comdll组件的过程详解

下面小编就为大家带来一篇Python调用C# Com dll组件实战教程。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
 
namespace ComToPython
{
  [Guid("350779B9-8AB5-4951-83DA-4CBC4AD860F4")]
  public interface IMyClass
  {
    void Initialize();
    void Dispose();
    int Add(int x, int y);
  }
 
  [ClassInterface(ClassInterfaceType.None)]
  [Guid("16D9A0AD-66B3-4A8A-B6C4-67C9ED0F4BE4")]
  [ProgId("ComToPython.Application")]
  public class ComToPython: IMyClass
  {
    public void Initialize()
    {
      // nothing to do 
    }
 
    public void Dispose()
    {
      // nothing to do 
    }
 
    public int Add(int x, int y)
    {
      return x + y;
    }
  }
} 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
a=1
b=2
print "方法一:"
from win32com.client import Dispatch
dll = Dispatch("ComToPython.Application")
result = dll.Add(a, b)
print "a + b = " + str(result)

print "方法二:"
import comtypes.client
dll = comtypes.client.CreateObject('ComToPython.Application')
result = dll.Add(a, b)
print "a + b = " + str(result) 

运行代码,执行结果如下:

以上就是Python调用C# COM Dll整个过程了

以上就是Python调用C# Com dll组件的过程详解的详细内容,更多请关注Gxl网其它相关文章!

查看更多关于Python调用C#Comdll组件的过程详解的详细内容...

  阅读:39次