delphi编写的com,其中调用了vc编写的dll动态连接库,到了后期出现了内存问题,有时会有程序写内存地址的错误,有时会出现变量值莫名其妙的发生了变化。因为对delphi和vc都不很熟悉,所以来此求救。问题如果解决另外还有分值奉上。

解决方案 »

  1.   

    是不是stdcall 和 farcall的区别?
      

  2.   

    是stdcall方式,代码很多,大概是这样:   function test():Integer; 
         ………………
         a:=func1();  //返回值为0(integer)
         b:=func2(…………);  //func2调用了vc的dll中的方法
         showmessage(a);   //在这里,a的值已经变成999
         ………………
       end;   function func2(acct_nbr:String; bill_charge:Integer; real_pay:Integer;
            flag:integer; a_ser_no:String; a_date:String):Integer;
        var
            AdoRs: OleVariant;
            acctNum:PChar;
            billCharge:Integer;
            realPay:Integer;
            flagType:Integer;
            staffId:Integer;
            staffAreaId:Integer;
            aSerNo:PChar;
            aDate:PChar;
            returnVal:Integer;
        begin
            AdoRS:=RunSql('select bt_staff_area_id, bt_staff_id ……');        GetMem(acctNum, 17);        strpcopy(acctNum, acct_nbr);
            aSerNo :=  PChar(a_ser_no);
            aDate :=  PChar(a_date);        billCharge:=bill_charge;    
            realPay:=real_pay;          
            flagType:=flag;                     staffId:=strToInt(trim(AdoRS.fields['bt_staff_id'].value));         
            staffAreaId:=strToInt(AdoRS.fields['bt_staff_area_id'].value); //*=999
            if AdoRs.state<>stClosed then AdoRs.Close;        returnVal:=writeoff(acctNum, billCharge, realPay,
                 flagType, staffId, staffAreaId, aSerNo, aDate);//vc的dll中的方法        if returnVal<>0 then
                fucn2:=1        //返回值
            else
                func2:=0;        freeMem(acctNum);
        end;
    end.定义:
    FUNCTION writeoff(acct_nbr:PChar; bill_charge:Integer;
        real_pay:Integer; flag:integer; staff_id:integer;
        staff_area_id:integer; aser_no:PChar; adate:PChar ):integer;
        stdcall; external 'a.dll' name 'writeoff';拜托各位了,我实在是没办法了。
      

  3.   

    定义:
    FUNCTION writeoff(acct_nbr:PChar; bill_charge:Integer;
        real_pay:Integer; flag:integer; staff_id:integer;
        staff_area_id:integer; aser_no:PChar; adate:PChar ):integer;
        stdcall; external 'a.dll' name 'writeoff';
    可能有问题;
    看来你在调用银行卡之类的东西的API;
    staff_id:integer;
        staff_area_id:integer;这两个中可能有个需要是返回的值;
    也就是需要VAR staff_area_id:integer
      

  4.   

    这个函数只返回是否成功,其他参数都是由我传入,不返回。
    这个a.dll在其他程序中也有使用,只不过是vc调用的,没有出现这种问题。
    我觉得是由于vc在分配内存时错误的使用了我在delphi中已经定义的变量所用的内存空间造成的,但我没有办法解决。
      

  5.   

    添加了var测试,仍然有错误。
    我用一个字符串保存了a,在a数据错误后使用字符串转回。间接解决了这个问题。
    但还是想知道原因。
    联系我请发邮件[email protected]
    感谢各位的帮助。