C语言转DELPHI问题!(200分) (50分)

  • 主题发起人 主题发起人 sms
  • 开始时间 开始时间
S

sms

Unregistered / Unconfirmed
GUEST, unregistred user!
在DLL中有C语言函数原型如下:<br>【函数原型】<br>int WINAPI SubmitA(unsigned char OrgTON, unsigned char OrgNPI, LPCTSTR OrgAddr,<br>&nbsp;unsigned long* SM_ID, unsigned char* FCS);<br>其中的unsigned long* SM_ID, unsigned char* FCS在DELPHI中引用时分别怎么表示?<br><br>
 
unsigned long* SM_ID &nbsp; &nbsp; &nbsp;var SM_ID: Cardinal;<br>unsigned char* FCS &nbsp; &nbsp; &nbsp; &nbsp;FCS: PByte;
 
在C中调用是SubmitA(2, 1,"13900000001",<br>&amp;SM_ID, &amp;FCS);<br>在DELPHI中调用时“&amp;SM_ID, &amp;FCS”怎么表示?
 
在C中调用是SubmitA(2, 1,"13900000001",<br>&amp;SM_ID, &amp;FCS<br><br>在DELPHI中为<br>SubmitA(2, 1,"13900000001",<br>@SM_ID,@FCS);<br><br>它在C中调用传入的是地址,最好你在DELPHI中声明时,都声明为指针类型的变量<br>
 
声名: SubmitA(OrgTON, OrgNPI: Byte; OrgAddr: string; var SM_ID: Cardinal; var FCS: Byte);<br>调用:<br>var<br>&nbsp; SM_ID: Cardinal;<br>&nbsp; FCS: Byte;<br>begin<br>&nbsp; SM_ID:=XXX;<br>&nbsp; FCS:=YYY;<br>&nbsp; SubmitA(2, 1, '13900000001', SM_ID, FCS);<br>end;
 
虽然说编译能通过,但调用不成功,我现将代码贴出来,请高手指点:<br>1、DLL中函数原型及示例:<br>【函数原型】<br>int WINAPI SubmitA(unsigned char OrgTON, unsigned char OrgNPI, LPCTSTR OrgAddr,<br>&nbsp;unsigned char DestTON, unsigned char DestNPI, LPCTSTR DestAddr, <br>unsigned char PRI, unsigned char RD, unsigned char RP, unsigned char SRR,<br>&nbsp;unsigned char MR, unsigned char DCS, unsigned char PID, LPCTSTR Schedule,<br>&nbsp;LPCTSTR Expire, unsigned long Default_ID, unsigned char UDHI, <br>unsigned long UDLen, LPCTSTR UserData, unsigned long* SM_ID, <br>unsigned char* FCS);<br>其中输出参数:<br>SM_ID 类型:unsigned long*<br>&nbsp; &nbsp; 意义:返回短消息ID<br>FCS &nbsp;类型:unsigned char*<br>&nbsp; &nbsp; 意义:返回SC端失败原因<br>【调用举例】<br>unsigned char PRI = 0, DCS = 8, RD = 0, RP = 0, SRR = 0, MR = 0, PID = 0;<br>unsigned long default_id = 0;<br>unsigned char UDHI = 0;<br>Cstring UserData = "你好,我已到达。";<br>unsigned long UDLen = &nbsp;UserData.GetLength();<br> unsigned long SM_ID;<br> unsigned char FCS;<br>int iRet = SubmitA(2, &nbsp;1, &nbsp;"1392700001", &nbsp;2, &nbsp;1, &nbsp;"1392700002", &nbsp;PRI, RD, RP, <br>&nbsp; SRR, MR, DCS, PID, &nbsp;"1998/01/01 08:00:00", &nbsp;"1998/01/01 20:30:00", &nbsp;default_id,<br>&nbsp; 0, &nbsp;UDLen, &nbsp;(LPCTSTR)UserData, &nbsp;&amp;SM_ID, &amp;FCS);<br>if (iRet != 0)<br>&nbsp; &nbsp; AfxMessageBox("短讯发送失败");<br><br>下面为DELPHI中代码:<br>var <br>&nbsp; SubmitA:function( OrgTON,OrgNPI:byte; Orgaddr:string;DestTON,DestNPI:byte; DestAddr:string;<br>&nbsp; &nbsp;PRI,RD,Rp,SRR,MR,DCS,PID:byte; Schedule,Expire:string; Defautlt_ID:longWord;<br>&nbsp; &nbsp;UDHI:byte; UDLen:LongWord; USerdata:string; SM_ID:cardinal; FCS:byte):Longword;stdcall;<br>procedure TfrmMain.BitBtn1Click(Sender: TObject);<br>var<br>&nbsp; PRI,DCS,RD,RP,SRR,MR,PID:byte;<br>&nbsp; default_id:longWord;<br>&nbsp; UDHI:byte;<br>&nbsp; Userdata:string;<br>&nbsp; UdLen:longWord;<br>&nbsp; SM_ID:cardinal;<br>&nbsp; FCS:byte;<br>&nbsp; iRet:Longword;<br>begin<br>&nbsp;PRI:=0;<br>&nbsp;DCS:=8;<br>&nbsp;RD:=0;<br>&nbsp;RP:=0;<br>&nbsp;SRR:=0;<br>&nbsp;MR:=0;<br>&nbsp;PID:=0;<br>&nbsp;default_id:=0;<br>&nbsp;UDHI:=0;<br>&nbsp;Userdata:='你好,测试';<br>&nbsp;Udlen:=length(Userdata);<br>&nbsp;iRet:=SubmitA(2,1,'555588',2,1,'13900000001',PRI,RD,RP,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SRR,MR,DCS,PID,'2002/08/05 08:00:00', &nbsp;'2002/08/05 20:30:00', &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default_id,UDHI,Udlen, Userdata, SM_ID,FCS);<br>&nbsp;if iret&lt;&gt;0 then<br>&nbsp; &nbsp;showmessage('短信发送失败');<br>end;<br><br>调用时产生Access violation at address 00000000.Read of adddress 00000000.<br>的错误信息,请大侠们指点<br><br><br><br>
 
关注,不会,给会的人多家点分
 
只要能解决,200分相送
 
function SubmitA( OrgTON,OrgNPI:byte; Orgaddr:string;DestTON,DestNPI:byte; DestAddr:string;<br>&nbsp; &nbsp;PRI,RD,Rp,SRR,MR,DCS,PID:byte; Schedule,Expire:string; Defautlt_ID:longWord;<br>&nbsp; &nbsp;UDHI:byte; UDLen:LongWord; USerdata:string; SM_ID:cardinal; FCS:byte):Longword;stdcall; external 'YourDllFileName.dll';<br><br>procedure TfrmMain.BitBtn1Click(Sender: TObject);<br>var<br>&nbsp; PRI,DCS,RD,RP,SRR,MR,PID:byte;<br>&nbsp; default_id:longWord;<br>&nbsp; UDHI:byte;<br>&nbsp; Userdata:string;<br>&nbsp; UdLen:longWord;<br>&nbsp; SM_ID:cardinal;<br>&nbsp; FCS:byte;<br>&nbsp; iRet:Longword;<br>begin<br>&nbsp;PRI:=0;<br>&nbsp;DCS:=8;<br>&nbsp;RD:=0;<br>&nbsp;RP:=0;<br>&nbsp;SRR:=0;<br>&nbsp;MR:=0;<br>&nbsp;PID:=0;<br>&nbsp;default_id:=0;<br>&nbsp;UDHI:=0;<br>&nbsp;Userdata:='你好,测试';<br>&nbsp;Udlen:=length(Userdata);<br>&nbsp;iRet:=SubmitA(2,1,'555588',2,1,'13900000001',PRI,RD,RP,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SRR,MR,DCS,PID,'2002/08/05 08:00:00', &nbsp;'2002/08/05 20:30:00', &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default_id,UDHI,Udlen, Userdata, SM_ID,FCS);<br>&nbsp;if iret&lt;&gt;0 then<br>&nbsp; &nbsp;showmessage('短信发送失败');<br>end;<br>
 
改为:<br>&nbsp; SubmitA:function( OrgTON,OrgNPI:byte; Orgaddr:PChar;DestTON,DestNPI:byte; DestAddr:PChar;<br>&nbsp; &nbsp;PRI,RD,Rp,SRR,MR,DCS,PID:byte; Schedule,Expire:string; Defautlt_ID:longWord;<br>&nbsp; &nbsp;UDHI:byte; UDLen:LongWord; USerdata:PChar; var SM_ID:cardinal; var FCS:byte):Longword;stdcall;
 
&nbsp;你那个错误信息可能是由于 SM_ID:cardinal; &nbsp;FCS:byte 前面没加Var 引起的<br>&nbsp; LPCTSTR &nbsp; 数据类型是Windows SDK所支持的,在DELPHI中可以直接声明,直接用<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var a:LPCTSTR;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a:='good luck';<br>&nbsp; LPCTSTR &nbsp; A 32-bit pointer to a constant character string that <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is portable for Unicode and DBCS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; 若声明成Pchar 可能也行,声明成string 可能有问题,不如直接声明就是了,<br>&nbsp; 返回值类型要用integer(32位有符号类型,可能返回-1等值)<br>所以函数声明改成:<br>Function &nbsp;SubmitA(OrgTON,OrgNPI:byte; &nbsp;OrgAddr LPCTSTR; DestTON, DestNPI:byte;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DestAddr LPCTSTR; PRI, RD, RP, SRR, MR,r DCS, PID:byte;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Schedule, Expire:LPCTSTR;Default_ID:Cardinal; UDHI:byte;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;UDLen:Cardinal;UserData :LPCTSTR;VAR SM_ID:Cardinal;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VAR &nbsp;FCS:byte):integer;stdcall;<br><br>在变量声明时要保证变量类型和函数声明的一致,要保证占用空间相同<br><br>如果还有问题,把调动约定改成Cdecl试试<br>
 
我也遇到这个问题,只不过这个厂家更恨,居然用<br>int send(... INT64 *msg_id...) // message ID<br>我试过 &nbsp; &nbsp; &nbsp;msg_id: PInt64; &nbsp; (type PInt64 = ^Int64)<br>调用时传入 &nbsp;i:=send(...@msg_id...) 编译时出错<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i:=send(...msg_id...) 编译可以通过,调用时报错,错误代码和sms兄的差不多<br>stdcall 和 cdecl 都试过都提示错误<br>我不知道该怎么定义,还请高手帮忙!我也给分<br>
 
后退
顶部