Delphi 调用C++动态库问题!100分!(100分)

  • 主题发起人 主题发起人 xucuiping
  • 开始时间 开始时间
X

xucuiping

Unregistered / Unconfirmed
GUEST, unregistred user!
--------------dll.h---------------<br>#ifdef DYNAMICDLL_EXPORTS<br> #define DLL_API _declspec(dllexport)<br>#else<br> #define DLL_API _declspec(dllimport)<br>#endif<br><br>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br><br><br>struct test1<br>{<br> &nbsp; char *pchDevip;<br> &nbsp; &nbsp;int nTimeout; <br>};<br><br>struct test2<br>{<br> &nbsp; &nbsp;int nTimeout;<br> &nbsp; &nbsp;int nPortnum; <br>};<br><br>class &nbsp;CXCP<br>{<br>public:<br> &nbsp; int getProgress()<br> &nbsp; { <br> &nbsp; &nbsp; &nbsp; return 100;<br> &nbsp; }<br>};<br><br>extern &quot;C&quot;<br>{<br> DLL_API void getDeviceEntity(CXCP **pDevEntity,test1ostTftp,test2 <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ostSnmp);<br>}<br><br>-------------dll.cpp-----------------<br>#include &quot;CTest.h&quot;<br><br>void getDeviceEntity(CXCP **pDevEntity,test1ostTftp,test2 ostSnmp);<br>{<br> &nbsp; &nbsp; *pDevEntity = new CXCP; <br>}<br><br><br>----------------Delphi------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, StdCtrls;<br><br>type<br><br> &nbsp;STtest1 = record<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pchdevip: Pchar;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nTimeout: integer;<br> &nbsp;end;<br> &nbsp;//--------------------------------<br><br> &nbsp;STtest2 = record<br> &nbsp;nTimeout: integer;<br> &nbsp;nPortnum: integer;<br> &nbsp;end;<br> &nbsp;//--------------------------------<br><br> &nbsp;TXCP = class(Tobject)<br> &nbsp;public<br> function getProgress():integer; [:(!] //只声明不定义,实现在dll中。可是编译总提<br> &nbsp;private &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//示“Unsatisfied forward or external <br> &nbsp;end; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //declaration: 'TXCP.getprogress'”<br> &nbsp;<br> &nbsp;PTXCP = ^TXCP;<br> &nbsp;//--------------------------------<br><br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br><br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br>procedure getDeviceEntity(var poDeviceEntity: PTDeviceEntity;oTftp: STTFTPPARA;oSnmp:STSNMPPATA);cdecl;external'E:/Delphi/C_Dll/dll/DynamicDLL.dll';<br>var<br> &nbsp;Form1: TForm1;<br><br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp; poXCP: PTXCP;<br> &nbsp; otest1: STtest1;<br> &nbsp; otest2: STtest2;<br>begin<br> &nbsp;getDeviceEntity(poXCP,otest1,otest2);<br> &nbsp;showmessage(inttostr(poXCP.getProgress()));<br>end;<br><br><br>end.
 
那位大虾帮帮忙?
 
建议用动态调用的方式<br>loadlibary,freelibary,getprocaddress等函数<br>另外要注意的是c++和delphi的调用惯例的问题
 

Similar threads

I
回复
0
查看
719
import
I
I
回复
0
查看
658
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
后退
顶部