怎样通过DLL返回IInterface(100分)

  • 主题发起人 主题发起人 zhrrqy
  • 开始时间 开始时间
Z

zhrrqy

Unregistered / Unconfirmed
GUEST, unregistred user!
我想通过dll返回一个IInterface的接口,怎么做?
 
沙发,,帮顶
 
type<br>&nbsp; itest = interface <br>&nbsp; &nbsp; procedure abc();<br>&nbsp; end;<br><br>ttest = class(itest)<br>&nbsp; procedure abc();<br>end;<br><br>procedure ttest.abc();<br>begin<br>&nbsp; showmessage('aaa');<br>end;<br><br>function aa: itest;<br>var a: itest;<br>begin<br>&nbsp; a := ttest.create();<br>&nbsp; result := aa; <br>end;<br><br>exports <br>&nbsp; aa;<br><br>// DLL导出函数区别大小写, 如果这样直接导出不行, 用包的方式编译就肯定可以导出.<br>// BTW: 用C#做这个就简单多了
 
刚写了个测试程序,不知是不是你想要的。<br>function exportinterface:ITest;返回ITest接口<br><br>dll部分:<br>//---project2.dpr---//<br>library Project2;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; Unit2 in 'Unit2.pas';<br><br>{$R *.res}<br>exports<br>&nbsp; exportinterface; //导出函数;<br>begin<br>end.<br><br>//--unit2.pas----//:<br>unit Unit2;<br>interface<br>uses<br>&nbsp; windows;<br>type<br>&nbsp; ITest=interface(IInterface)<br>&nbsp; &nbsp; procedure showmsg;<br>&nbsp; end;<br><br>&nbsp; TTest=class(TInterfacedObject,ITest)<br>&nbsp; private<br>&nbsp; &nbsp; FMsg:string;<br>&nbsp; public<br>&nbsp; &nbsp; procedure showmsg;<br>&nbsp; &nbsp; constructor create;<br>&nbsp; end;<br><br>function exportinterface:ITest;stdcall;<br><br>var<br>&nbsp; Test:ITest;<br><br>implementation<br><br>{ Test }<br>function exportinterface:ITest;<br>begin<br>&nbsp; if Test=nil then<br>&nbsp; &nbsp; Test:=TTest.Create;<br>&nbsp; result:=Test;<br>end;<br><br>constructor TTest.create;<br>begin<br>&nbsp; inherited;<br>&nbsp; FMSg:='dll import interface';<br>end;<br><br>procedure TTest.showmsg;<br>begin<br>&nbsp; messagebox(0,pchar(FMsg),pchar('Test'),MB_OK);<br>end;<br><br>end.<br><br>主程序unit1.pas:<br>函数声明:<br>function exportinterface:ITest;stdcall; external 'project2.dll';<br>调用:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; CallTest:ITest;<br>begin<br>&nbsp; CallTest:=exportinterface;<br>&nbsp; CallTest.showmsg;<br>end;
 
后退
顶部