library Cartoon;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }uses
  SysUtils,
  Classes,
  ActiveX,
  IniFiles,
  ADODB;{$R *.RES}
function funbalanceqry(cardID:string;password:string):Integer;stdcall;
var
  myinifile:Tinifile;
  ADOQuery : TAdoQuery;
  filename:string;
//  value:TStrings;
  Provider,Persist,User,Initial,Data:string;
begin
  ADOQuery:=TAdoQuery.Create(nil);
  if not FileExists('dbconfig.ini') then
  begin
    result:=100001;
    exit;
  end
  else
  try
    myinifile:=Tinifile.create('dbconfig.ini');
    Filename:=ExtractFilePath(Paramstr(0))+'dbconfig.ini';
    myinifile:=Tinifile.Create(filename);
   // myinifile.readsectionvalues(dbconfig,value);    Provider:='Provider'+trim(myinifile.ReadString('config','Provider',''));
    Persist:='Persist Security Info'+trim(myinifile.ReadString('config','Persist Security Info',''));
    User:='User ID'+trim(myinifile.ReadString('config','User ID',''));
    Initial:='Initial Catalog'+trim(myinifile.ReadString('config','Initial Catalog',''));
    Data:='Data Source'+trim(myinifile.ReadString('config','Data Source',''));
    myinifile.free;    ADOQuery.ConnectionString:=Provider+Persist+User+Initial+Data;
    ADOQuery.Active:=true;
    except
      result:=100001;
      exit;
    end;
  try
  with ADOQuery do
  begin
    close;
    sql.Text:='select * from tcards where iCardID='''+cardid+'''';
    execsql;
    if RecordCount=0 then result:=100002;
    exit;
  end;
  with ADOQuery do
  begin
    close;
    sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+'''';
    execsql;
    if RecordCount=0 then result:=100003;
    exit;
  end;
  with ADOQuery do
  begin
    close;
    sql.Text:='select mBalance from tcards where iCardID='''+cardid+''' and cPassword='''+password+'''';
    open;
    result:=fieldbyname('mBalance').asinteger;
    exit;
  end;
  except
    result:=8;
  end;
end;function FunConsume(cardID:string;password:string;Balance:float;SystemID:integer;BillID:string;PersonnelID:integer):Integer;stdcall;
var
  myinifile:Tinifile;
  ADOQuery : TAdoQuery;
  filename:string;
//  value:tstrings;
  Provider,Persist,User,Initial,Data:string;
begin
  ADOQuery:=TAdoQuery.Create(nil);
  if not FileExists('dbconfig.ini') then
  begin
    result:=100001;
    exit;
  end
  else
  try
    myinifile:=Tinifile.create('dbconfig.ini');
    Filename:=ExtractFilePath(Paramstr(0))+'dbconfig.ini';
    myinifile:=Tinifile.Create(filename);
    //myinifile.readsectionvalues(dbconfig,value);
    Provider:='Provider'+trim(myinifile.ReadString('config','Provider',''));
    Persist:='Persist Security Info'+trim(myinifile.ReadString('config','Persist Security Info',''));
    User:='User ID'+trim(myinifile.ReadString('config','User ID',''));
    Initial:='Initial Catalog'+trim(myinifile.ReadString('config','Initial Catalog',''));
    Data:='Data Source'+trim(myinifile.ReadString('config','Data Source',''));
    myinifile.free;
    ADOQuery.ConnectionString:=Provider+Persist+User+Initial+Data;
//    ADOQuery.ConnectionString:=value;
    ADOQuery.Active:=true;
    except
      result:=100001;
      exit;
    end;
  try
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+'''';
      execsql;
      if RecordCount=0 then result:=100002;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+'''';
      execsql;
      if RecordCount=0 then result:=100003;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+''' and iCardState=2';
      execsql;
      if RecordCount>0 then result:=2;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+''' and iCardState=3';
      execsql;
      if RecordCount>0 then result:=3;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+''' and iCardState=4';
      execsql;
      if RecordCount>0 then result:=4;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+''' and iCardState=5';
      execsql;
      if RecordCount>0 then result:=5;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+''' and iMakeYears+dSendDate>getdate()';
      execsql;
      if RecordCount>0 then result:=6;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select mOverdraftLimit from tMemberTypes a,tMember b,tCards c where c.iCardID='''+cardid+''' and c.cPassword='''+password+''' and c.imemberid=b.cmemberid and b.iMemberTypesID=a.iMemberTypesID';
      execsql;
      if RecordCount>0 then result:=7;
      exit;
    end;
    with ADOQuery do
    begin
      close;
      sql.Text:='select * from tcards where iCardID='''+cardid+''' and cPassword='''+password+''' and iCardState=1';
      execsql;
      if RecordCount>0 then
      begin
        close;
        sql.Text:='insert into tCardBills values(cardID,3,Balance,gerdate(),PersonnelID,1,BillID,)';
        execsql;
      end;
    end;
  except
    result:=8
    exit;
  end;
  result:=0;
end;exports
  FunBalanceQry, FunConsume;
begin
  CoInitialize(nil);
end.

解决方案 »

  1.   

    这是帮别人做的校园一卡通的部分功能,第一个函数是输入卡号和密码来查询余额,第2个函数是输入卡号,密码,金额,子系统号,单号和操作员号来检查卡状态,没问题在写入帐目表中。数据库连接字符串写在INI文件中。
    因为我是第1次写DLL,所以心里没底,请大家帮忙看看,很急的,看看程序有什么毛病,什么地方可以精简,什么地方应该改进,谢谢大家了。。
      

  2.   

    1. 名字不对,cartoon?拼音也不对,容易让人误解;
    2. 建议避免对外使用string类型的参数,而采用pchar;其他参数也最好模仿windows API的风格;
    3. 没试过,你自己在外面(Delphi、VC)调用行么?貌似框架没问题;
    4. 把borland帮你加的那段废话(关于string与borlandmm的)删了吧。
      

  3.   

    你的dll用到 string 作参数:
    >>function funbalanceqry(cardID:string;password:string):Integer;stdcall;
    那必须在dll与exe中都
    uses ShareMem;你看你的代码一开始的注释,已经写的很清楚:
    >>if your DLL exports any procedures or
      functions that pass strings as parameters or function results
    解决方法
    >>To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters.
      

  4.   

    恩,我把名字改了。用了PCHAR有WARRNING说是不安全类型啊。。还有,金额的类型我用MONEY和FLOAT都不对,应该用什么呢?麻烦 RamjetZhang(王小桃) 大虾了。。
      

  5.   

    aiirii(ari-爱的眼睛)是我没注意,呵呵。麻烦帮忙在看看吧。谢谢。。
      

  6.   

    把String 改成pchar,那个warrning 不用理会!