C#生成COM组件,Delphi调用

C#程序:

1)文件--》新建类库项目

2)Properties-->AssemblyInfo.cs 设置ComVisible为true

3)项目--》属性

4)属性--》生成  为COM互操作注册

5)代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace MyFirstCOM
{
    [ComVisible(true)]
    public interface IWelcome
    {
        string Greeting(string name);
    }

    [ComVisible(true)]
    public interface IMath
    {
        int add(int val1, int val2);
        int sub(int val1, int val2);
    }
    [ComVisible(true)]
    public class FirstCOM : IWelcome, IMath
    {
        public int add(int val1, int val2)
        {
            return val1 + val2;
        }

        public string Greeting(string name)
        {
            return "Hello " + name;
        }

        public int sub(int val1, int val2)
        {
            return val1 - val2;
        }
    }
}

6)注册  打开Command

Delphi调用:

1)新项目

Componets--》Import Componets

2)delphi代码

unit Unit27;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,MyFirstCOM_TLB;

type
  TForm27 = class(TForm)
    btn: TButton;
    procedure btnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form27: TForm27;

implementation

{$R *.dfm}

procedure TForm27.btnClick(Sender: TObject);
var
  com : _FirstCOM;
begin
  com := CoFirstCOM.Create;
  ShowMessage(IntToStr((com as IMath).add(10,20)));

end;

end.

3)MyFirstCOM_TLB.pas代码如下 

unit MyFirstCOM_TLB;

// ************************************************************************ //
// WARNING                                                                    
// -------                                                                    
// The types declared in this file were generated from data read from a       
// Type Library. If this type library is explicitly or indirectly (via        
// another type library referring to this type library) re-imported, or the   
// 'Refresh' command of the Type Library Editor activated while editing the   
// Type Library, the contents of this file will be regenerated and all        
// manual modifications will be lost.                                         
// ************************************************************************ //

// $Rev: 34747 $
// File generated on 2019-03-11 13:10:55 from Type Library described below.

// ************************************************************************  //
// Type Lib: C:\Users\jay\Source\Repos\MyFirstCOM\MyFirstCOM\bin\Debug\MyFirstCOM.tlb (1)
// LIBID: {05E7FAE0-E4D7-4624-920D-5F2338D27749}
// LCID: 0
// Helpfile: 
// HelpString: 
// DepndLst: 
//   (1) v2.0 stdole, (C:\Windows\SysWOW64\stdole2.tlb)
//   (2) v2.4 mscorlib, (C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
{$ALIGN 4}
interface

uses Windows, ActiveX, Classes, Graphics, mscorlib_TLB, OleServer, StdVCL, Variants;
  


// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:        
//   Type Libraries     : LIBID_xxxx                                      
//   CoClasses          : CLASS_xxxx                                      
//   DISPInterfaces     : DIID_xxxx                                       
//   Non-DISP interfaces: IID_xxxx                                        
// *********************************************************************//
const
  // TypeLibrary Major and minor versions
  MyFirstCOMMajorVersion = 1;
  MyFirstCOMMinorVersion = 0;

  LIBID_MyFirstCOM: TGUID = '{05E7FAE0-E4D7-4624-920D-5F2338D27749}';

  IID_IWelcome: TGUID = '{E7112B29-AEEF-39E0-B454-0EE3A3A43361}';
  IID_IMath: TGUID = '{10E2E218-E904-327F-AA40-F5E47AE9D071}';
  IID__FirstCOM: TGUID = '{EF6CAED7-63D7-3ADE-AC3E-3EA664C9C972}';
  CLASS_FirstCOM: TGUID = '{86268596-D3D5-3E4C-BE79-349680844D0A}';
type

// *********************************************************************//
// Forward declaration of types defined in TypeLibrary                    
// *********************************************************************//
  IWelcome = interface;
  IWelcomeDisp = dispinterface;
  IMath = interface;
  IMathDisp = dispinterface;
  _FirstCOM = interface;
  _FirstCOMDisp = dispinterface;

// *********************************************************************//
// Declaration of CoClasses defined in Type Library                       
// (NOTE: Here we map each CoClass to its Default Interface)              
// *********************************************************************//
  FirstCOM = _FirstCOM;


// *********************************************************************//
// Interface: IWelcome
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {E7112B29-AEEF-39E0-B454-0EE3A3A43361}
// *********************************************************************//
  IWelcome = interface(IDispatch)
    ['{E7112B29-AEEF-39E0-B454-0EE3A3A43361}']
    function Greeting(const name: WideString): WideString; safecall;
  end;

// *********************************************************************//
// DispIntf:  IWelcomeDisp
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {E7112B29-AEEF-39E0-B454-0EE3A3A43361}
// *********************************************************************//
  IWelcomeDisp = dispinterface
    ['{E7112B29-AEEF-39E0-B454-0EE3A3A43361}']
    function Greeting(const name: WideString): WideString; dispid 1610743808;
  end;

// *********************************************************************//
// Interface: IMath
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {10E2E218-E904-327F-AA40-F5E47AE9D071}
// *********************************************************************//
  IMath = interface(IDispatch)
    ['{10E2E218-E904-327F-AA40-F5E47AE9D071}']
    function add(val1: Integer; val2: Integer): Integer; safecall;
    function sub(val1: Integer; val2: Integer): Integer; safecall;
  end;

// *********************************************************************//
// DispIntf:  IMathDisp
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {10E2E218-E904-327F-AA40-F5E47AE9D071}
// *********************************************************************//
  IMathDisp = dispinterface
    ['{10E2E218-E904-327F-AA40-F5E47AE9D071}']
    function add(val1: Integer; val2: Integer): Integer; dispid 1610743808;
    function sub(val1: Integer; val2: Integer): Integer; dispid 1610743809;
  end;

// *********************************************************************//
// Interface: _FirstCOM
// Flags:     (4432) Hidden Dual OleAutomation Dispatchable
// GUID:      {EF6CAED7-63D7-3ADE-AC3E-3EA664C9C972}
// *********************************************************************//
  _FirstCOM = interface(IDispatch)
    ['{EF6CAED7-63D7-3ADE-AC3E-3EA664C9C972}']
  end;

// *********************************************************************//
// DispIntf:  _FirstCOMDisp
// Flags:     (4432) Hidden Dual OleAutomation Dispatchable
// GUID:      {EF6CAED7-63D7-3ADE-AC3E-3EA664C9C972}
// *********************************************************************//
  _FirstCOMDisp = dispinterface
    ['{EF6CAED7-63D7-3ADE-AC3E-3EA664C9C972}']
  end;

// *********************************************************************//
// The Class CoFirstCOM provides a Create and CreateRemote method to          
// create instances of the default interface _FirstCOM exposed by              
// the CoClass FirstCOM. The functions are intended to be used by             
// clients wishing to automate the CoClass objects exposed by the         
// server of this typelibrary.                                            
// *********************************************************************//
  CoFirstCOM = class
    class function Create: _FirstCOM;
    class function CreateRemote(const MachineName: string): _FirstCOM;
  end;

implementation

uses ComObj;

class function CoFirstCOM.Create: _FirstCOM;
begin
  Result := CreateComObject(CLASS_FirstCOM) as _FirstCOM;
end;

class function CoFirstCOM.CreateRemote(const MachineName: string): _FirstCOM;
begin
  Result := CreateRemoteComObject(MachineName, CLASS_FirstCOM) as _FirstCOM;
end;

end.

4)测试结果

猜你喜欢

转载自blog.csdn.net/zang141588761/article/details/88392032
今日推荐