Unit1function SendMsgExEx(Mobiles:Pchar;SM_Content:String;UserCount:Integer;ServiceType:String;FeeCode:String):integer;
var
    ................
    sDestAddrs:Pchar;
    iRet:integer;
    sMobile:String;
begin
   ..........
   sDestAddrs:=StrAlloc(21*UserCount-1);
   sFeeAddr:=StrAlloc(21*UserCount-1);
   sFeeAddr:=Mobiles;
   sFeeCode:=Pchar(FeeCode);
   byUserNum:=UserCount;
   sDestAddrs:=Mobiles;
   try
      iRet:=SubmitAExEx(OrgTON,OrgNPI,OrgAddr,DestTON,DestNPI,DestAddr,
                        PRI,RD,RP,SRR,MR,DCS,PID,Schedule,Expire,Default_ID,UDHI,
                        UDLen,UserData,sServiceType,@byMsgID,byPKTotal,byPKNumber,
                        byMsgLevel,byFeeUserType,sSPID,sFeeType,sFeeAddr,sFeeCode,
                        byUserNum,sDestAddrs,@SM_ID,@FCS);
   Finally
        SendMsgExEx:=iRet;
        //StrDispose(sDestAddrs);  //当添加该句时,运行时总是地址报错
       //StrDispose(sFeeAddr);     //当添加该句时,运行时总是地址报错
   end;
end;
Unit2procedure TFrmTest.BtnSendClick(Sender: TObject);
   var vMobileList:TStringList;
       vPMobile:Pchar;
       Ret:Integer;
begin
       vMobileList:=TStringList.Create;
       if EdtMobile1.Text<>'' then
          vMobileList.Add(EdtMobile1.Text);
       if EdtMobile2.Text<>'' then
          vMobileList.Add(EdtMobile2.Text);       vPMobile:=StrAlloc(21*vMobileList.Count-1);
       if (vMobileList.Count > 0)  and (MemoMsg.Text <> '' ) then
       begin
           vPMobile:=FunctionUnit.PMobiles(vMobileList);
           Ret:=FunctionUnit.SendMsgExEx(vPMobile,Trim(MemoMsg.Text),vMobileList.Count,'ZC1','750');
           if Ret = 0 then
               ShowMessage('发送成功!  反馈代码:'+IntToStr(Ret))
           else
               ShowMessage('发送失败!  反馈代码:'+IntToStr(Ret));
       end
       else
           showMessage('请输入完整参数');
       StrDispose(vPMobile);
       vMobileList.Free;
end;当我执行BtnSendClick时,正常弹出对话框:'发送成功!  反馈代码.."
但是当我点击确定后就出现 'Invalid Pointer operation'.
应该是指针的问题.
当添加  StrDispose(sFeeAddr); 句时,运行时也总是地址报错 .怎么解决?

解决方案 »

  1.   

    sDestAddrs:=StrAlloc(21*UserCount-1);
       '''''''
       sDestAddrs:=Mobiles;
    你分配了两次内存,看清楚了;
      

  2.   

    vPMobile:=StrAlloc(21*vMobileList.Count-1);
    你分配了两次内存,看清楚了;
      

  3.   

    To : outer2000我按你说的改了.有时会出错有时不会.
    Unit1function SendMsgExEx(Mobiles:Pchar;SM_Content:String;UserCount:Integer;ServiceType:String;FeeCode:String):integer;
    var
        ................
        sDestAddrs:Pchar;
        iRet:integer;
        sMobile:String;
    begin
       ..........
       sFeeAddr:=Mobiles;
       sFeeCode:=Pchar(FeeCode);
       byUserNum:=UserCount;
       sDestAddrs:=Mobiles;
       try
          iRet:=SubmitAExEx(OrgTON,OrgNPI,OrgAddr,DestTON,DestNPI,DestAddr,
                            PRI,RD,RP,SRR,MR,DCS,PID,Schedule,Expire,Default_ID,UDHI,
                            UDLen,UserData,sServiceType,@byMsgID,byPKTotal,byPKNumber,
                            byMsgLevel,byFeeUserType,sSPID,sFeeType,sFeeAddr,sFeeCode,
                            byUserNum,sDestAddrs,@SM_ID,@FCS);
       Finally
            SendMsgExEx:=iRet;
            StrDispose(sDestAddrs
       end;
    end;
    Unit2procedure TFrmTest.BtnSendClick(Sender: TObject);
       var vMobileList:TStringList;
           vPMobile:Pchar;
           Ret:Integer;
    begin
           vMobileList:=TStringList.Create;
           if EdtMobile1.Text<>'' then
              vMobileList.Add(EdtMobile1.Text);
           if EdtMobile2.Text<>'' then
              vMobileList.Add(EdtMobile2.Text);
           if (vMobileList.Count > 0)  and (MemoMsg.Text <> '' ) then
           begin
               vPMobile:=FunctionUnit.PMobiles(vMobileList);
               Ret:=FunctionUnit.SendMsgExEx(vPMobile,Trim(MemoMsg.Text),vMobileList.Count,'ZC1','750');
               if Ret = 0 then
                   ShowMessage('发送成功!  反馈代码:'+IntToStr(Ret))
               else
                   ShowMessage('发送失败!  反馈代码:'+IntToStr(Ret));
           end
           else
               showMessage('请输入完整参数');
          vMobileList.Free;
    end;
      

  4.   

    To : outer2000我按你说的改了.有时会出错有时不会.
    Unit1function SendMsgExEx(Mobiles:Pchar;SM_Content:String;UserCount:Integer;ServiceType:String;FeeCode:String):integer;
    var
        ................
        sDestAddrs:Pchar;
        iRet:integer;
        sMobile:String;
    begin
       ..........
       sFeeAddr:=Mobiles;
       sFeeCode:=Pchar(FeeCode);
       byUserNum:=UserCount;
       sDestAddrs:=Mobiles;
       try
          iRet:=SubmitAExEx(OrgTON,OrgNPI,OrgAddr,DestTON,DestNPI,DestAddr,
                            PRI,RD,RP,SRR,MR,DCS,PID,Schedule,Expire,Default_ID,UDHI,
                            UDLen,UserData,sServiceType,@byMsgID,byPKTotal,byPKNumber,
                            byMsgLevel,byFeeUserType,sSPID,sFeeType,sFeeAddr,sFeeCode,
                            byUserNum,sDestAddrs,@SM_ID,@FCS);
       Finally
            SendMsgExEx:=iRet;
            StrDispose(sDestAddrs)
       end;
    end;
    Unit2procedure TFrmTest.BtnSendClick(Sender: TObject);
       var vMobileList:TStringList;
           vPMobile:Pchar;
           Ret:Integer;
    begin
           //其中vPMobile变量的值是来自  添加手机号码集函数的(Pchar型)
           vMobileList:=TStringList.Create;
           if EdtMobile1.Text<>'' then
              vMobileList.Add(EdtMobile1.Text);
           if EdtMobile2.Text<>'' then
              vMobileList.Add(EdtMobile2.Text);
           if (vMobileList.Count > 0)  and (MemoMsg.Text <> '' ) then
           begin
               vPMobile:=FunctionUnit.PMobiles(vMobileList);
               Ret:=FunctionUnit.SendMsgExEx(vPMobile,Trim(MemoMsg.Text),vMobileList.Count,'ZC1','750');
               if Ret = 0 then
                   ShowMessage('发送成功!  反馈代码:'+IntToStr(Ret))
               else
                   ShowMessage('发送失败!  反馈代码:'+IntToStr(Ret));
           end
           else
               showMessage('请输入完整参数');
          vMobileList.Free;
    end;再补充一点:{-------------------添加手机号码集-----------------}
    function PMobiles(MobileList:TStringList):Pchar;
        Var m,n,CountDest,MemCount,tempID:Integer;
            tempMobileAdd:String[11];
            tempMobileListAdd:Pchar;
    begin
        CountDest:=MobileList.Count;
        MemCount:=21*CountDest - 1;
        tempID:=0;
        tempMobileListAdd:=StrAlloc(MemCount);
        for m:=0 to CountDest-1 do
        begin
            tempMobileAdd:=MobileList.Strings[m];
            for n:=1 to 21 do
            begin
                if n<=11 then
                    tempMobileListAdd[tempID]:=tempMobileAdd[n]
                else
                   tempMobileListAdd[tempID]:=#0;
                 tempID:=tempID + 1;
            end;
        end;
        Result:=tempMobileListAdd;
    end;
      

  5.   

    总是  'Invalid Pointer operation' 
    就是在发送短信的时候出错的.
      

  6.   

    tempMobileListAdd:=StrAlloc(MemCount);///这里分配了?
        for m:=0 to CountDest-1 do
        begin
            tempMobileAdd:=MobileList.Strings[m];
            for n:=1 to 21 do
            begin
                if n<=11 then
                    tempMobileListAdd[tempID]:=tempMobileAdd[n]
                else
                   tempMobileListAdd[tempID]:=#0;
                 tempID:=tempID + 1;
            end;
        end;
        Result:=tempMobileListAdd;
    ///什么时候释放呢?
      

  7.   

    tempMobileListAdd:=StrAlloc(MemCount);///这里分配了?
        for m:=0 to CountDest-1 do
        begin
            tempMobileAdd:=MobileList.Strings[m];
            for n:=1 to 21 do
            begin
                if n<=11 then
                    tempMobileListAdd[tempID]:=tempMobileAdd[n]
                else
                   tempMobileListAdd[tempID]:=#0;
                 tempID:=tempID + 1;
            end;
        end;
        Result:=tempMobileListAdd;
        STRDISPOSE(TMP)
      

  8.   

    To: : outer2000(天外流星) 
     
    加了这句:StrDispose(tempMobileListAdd),在调用
    PMobiles(MobileList:TStringList):Pchar函数后内存已经别释放了.
    这时调用SendMsgExEx函数时发送手机的号码就是空的.我是在SendMsgExEx函数发送后就释放内存.在SendMsgExEx函数内部释放!
      

  9.   

    strcopy(result,tempMobileListAdd);
      

  10.   

    to  : outer2000(天外流星) 
    strcopy(result,tempMobileListAdd);
    StrDispose(tempMobileListAdd);我先测试!非常谢谢你!!
      

  11.   

    测试1:
       PMobiles(MobileList:TStringList):Pchar;
       begin
    ....
       strcopy(result,tempMobileListAdd);
       StrDispose(tempMobileListAdd);
    end;
    TFrmTest.BtnSendClick
    begin
       StrDispose(sDestAddrs);  
    end;
      出错语句:Ret:=FunctionUnit.SendMsgExEx(vPMobile,Trim   (MemoMsg.Text),vMobileList.Count,'ZC1','750');
    测试2:
      strcopy(result,tempMobileListAdd);
      出错语句:Ret:=FunctionUnit.SendMsgExEx(vPMobile,Trim   (MemoMsg.Text),vMobileList.Count,'ZC1','750');
      

  12.   

    to  : outer2000(天外流星)
    测试1:
       PMobiles(MobileList:TStringList):Pchar;
       begin
    ....
       strcopy(result,tempMobileListAdd);
       StrDispose(tempMobileListAdd);
    end;
    TFrmTest.BtnSendClick
    begin
       StrDispose(sDestAddrs);  
    end;
      出错语句:Ret:=FunctionUnit.SendMsgExEx(vPMobile,Trim   (MemoMsg.Text),vMobileList.Count,'ZC1','750');
    测试2:
      strcopy(result,tempMobileListAdd);
      出错语句:Ret:=FunctionUnit.SendMsgExEx(vPMobile,Trim   (MemoMsg.Text),vMobileList.Count,'ZC1','750');