现在来说一下调用过程:【本地调试通过,环境是XP,delphi7.0,vs2005】
    首先在vs2005中创建一Class Library项目,添加2个cs文件,代码分别为:
 声明一个接口
 1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4 namespace beep_Class
namespace beep_Class
5 {
{
6 public interface IGO
    public interface IGO
7 {
    {
8 string GO();
        string GO();
9 }
    }
10 }
}
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4
 namespace beep_Class
namespace beep_Class5
 {
{6
 public interface IGO
    public interface IGO7
 {
    {8
 string GO();
        string GO();9
 }
    }10
 }
}实现该接口
 1 using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
2 namespace beep_Class
namespace beep_Class
3 {
{   
4 [ClassInterface(ClassInterfaceType.None)]
    [ClassInterface(ClassInterfaceType.None)]
5 public class Class1:IGO
    public class Class1:IGO
6 {
    {
7
8 public string GO()
        public string GO()
9 {
        {
10 return "aaaaabbbb";
            return "aaaaabbbb";
11 }
        }
12 }
    }    
13 }
}
 using System.Runtime.InteropServices;
using System.Runtime.InteropServices;2
 namespace beep_Class
namespace beep_Class3
 {
{   4
 [ClassInterface(ClassInterfaceType.None)]
    [ClassInterface(ClassInterfaceType.None)]5
 public class Class1:IGO
    public class Class1:IGO6
 {
    {7

8
 public string GO()
        public string GO()9
 {
        {10
 return "aaaaabbbb";
            return "aaaaabbbb";11
 }
        }12
 }
    }    13
 }
}
然后在生成类库之前设置一下该项目的属性,如下图所示:
注意红线标示的部分。
然后对此编译成功的DLL【beep_Class.dll】进行处理,打开vs2005自带的命令行工具。输入 tlbexp beep_Class.dll
生成 beep_Class.tlb文件。
下一步是打开delphi7,新建一个Application,在Form上增加一Button。然后选择Project下的,import type library,把刚才生成的Tlb文件【beep_Class.tlb】添加进来,然后点击 CreateUnit就ok了
delphi中的Button事件代码如下:
 1 procedure TForm1.Button1Click(Sender: TObject);
procedure TForm1.Button1Click(Sender: TObject);
2 var
var
3 co:Class1;
  co:Class1;
4 a:string;
  a:string;
5 begin
begin
6 co:= CoClass1.Create;
  co:= CoClass1.Create;
7 a:=co.GO();
  a:=co.GO();
8 showmessage(a);
  showmessage(a);
9 end;
end;
10 end.
end.
 procedure TForm1.Button1Click(Sender: TObject);
procedure TForm1.Button1Click(Sender: TObject);2
 var
var3
 co:Class1;
  co:Class1;4
 a:string;
  a:string;5
 begin
begin6
 co:= CoClass1.Create;
  co:= CoClass1.Create;7
 a:=co.GO();
  a:=co.GO();8
 showmessage(a);
  showmessage(a);9
 end;
end;10
 end.
end.
编译通过,运行结果如下图:
注意:运行的时候要把Beep_Class.dll放在程序目录中。
 
                     
                    
                 
                    
                 
 
        

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号