首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

从delphi移栽一段调用DLL的代码出错,求解

2013-01-20 
从delphi移植一段调用DLL的代码出错,求解unit MaininterfaceusesWindows, Messages, SysUtils, Variants,

从delphi移植一段调用DLL的代码出错,求解
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, XPMan;
const
  DLL_Name = 'btLock56.dll' ;
  // 读客人卡
  function Read_Guest_Card(
                         AReaderType,
                         ACardType,
                         APort,
                         ASector,
                         ABlock: Byte;
                         AHotelPwd: Pchar;
                         ClientID: Longword;
                     var ALockNo,
                         AStartSuite,
                         AEndSuite,
                         ACardID,
                         ASubCardID: integer;
                         ATime: Pchar
                         ):integer; stdcall; External DLL_Name;

type
  TMainForm = class(TForm)
    StatusBar1: TStatusBar;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Edit2: TEdit;
    Edit1: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    GroupBox3: TGroupBox;
    Label11: TLabel;
    cbPort: TComboBox;
    Label9: TLabel;
    cbReader: TComboBox;
    edBlock: TEdit;
    Label13: TLabel;
    Label14: TLabel;
    edSector: TEdit;


    Label7: TLabel;
    Edit7: TEdit;
    Edit8: TEdit;
    Label15: TLabel;
    XPManifest1: TXPManifest;
    BtnReadCard: TButton;
    Button1: TButton;
    Label16: TLabel;
    cbCardType: TComboBox;
    lblClientID: TLabel;
    edtClientID: TEdit;
    procedure BtnReadCardClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  FPortNo:integer;     // 通信端口
  FReaderType,         // 读写器类型
  FCardType,           // 卡类型:  $41: 表示EM4150卡, $44: 表示EM4469卡.
  FSector,             // 一般情况下,EM4150卡默认为3; EM4469卡默认为5;
                       //  请客户根据酒店提供, 门锁软件 --> 系统设置 -->调试锁 -->设置卡窗口下的卡地址。
  FBlock:Byte;         // 未用到 ;   客人卡默认0块

implementation

{$R *.dfm}
// 读客人卡信息
procedure TMainForm.BtnReadCardClick(Sender: TObject);
var
  Time:pchar;
  tmpTime:string;
  HotelPwd:array[0..8] of char;
  rt,LockNo,CardID,SubCardID,StartSuite, EndSuite:integer;
  tmpClientID: Longword;
begin
   LockNo:=0;
   CardID:=0;
   SubCardID:=0;
   StartSuite:=0;
   EndSuite:=0;
   GetMem(Time,11);
   // 酒店密码
   StrPCopy(HotelPwd, Edit2.Text);
   tmpClientID := StrToInt64(edtClientID.Text);
   // 读客人卡
   rt:=Read_Guest_Card(FReaderType,FCardType, FPortNo,FSector,FBlock,@HotelPwd,
                       tmpClientID,LockNo,StartSuite,EndSuite,CardID,SubCardID,Time);
   Edit3.text:=inttostr(LockNo);
   Edit4.text:=inttostr(CardID);
   Edit5.text:=inttostr(SubCardID);
   tmpTime:=strpas(Time);
   FreeMem(Time);
   Edit6.Text:=tmpTime;
   Edit7.Text:=inttostr(Startsuite);
   Edit8.Text:=inttostr(EndSuite);
end;

end.

这是delphi的DEMO,是好使的,但是下面我移植到BCB里出现了错误
[解决办法]
int (_stdcall *Read_Guest_Card)( unsigned char AReaderType, unsigned char ACardType, unsigned char APort, unsigned char ASector, unsigned char ABlock, unsigned char * AHotelPwd, unsigned int ClientID, 

估计是这部分的var传参你没注意,
int & ALockNo, int & AStartSuite, int & AEndSuite, int & ACardID, int & ASubCardID, unsigned char *ATime);



如果还不行,就试试 int * ALockNo 这样。

另外,建议typedef 来写DLL的函数声明,你这代码的可读性真心不是很好。
[解决办法]
好长啊,
  for(int i=0;i<9;i++) //这个i<8才对,要么把定义AHotelPwd[9]
  {
    AHotelPwd[i]='\0';
  }
ATime[100] //看题意只需要ATime[11+1]



[解决办法]
unsigned char [100];
直接这样省事,没必要new,还要delete

热点排行