这样声明函数 
function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):Integer;stdcall;external 'VEHDRVEXT.DLL';
  这样调用:var
  vi:integer;
  vcon,vserverice:string;
  //voutput:array[0..999] of char;
  voutput: PChar;//string;//
begin
    //vi:='110';
   // FillChar(vi,   SizeOf(vi),   ord('   '));
    voutput:='1122345678';
    Username :='Xubo';
    Password :='123456';
    Username := trim(medt1.Text);
    // parambyname('password').AsString :=  trim(edit2.Text);
    Password :=  trim(medt2.Text);
    try
      begin
        //GetMem(vi,2048);
        GetMem(voutput,2048);
        vi:=GetInformation('amag@amag123','checkLogin',voutput);
       end;
     finally
       begin
        //FreeMem(vi);
        FreeMem(voutput);
       end;
    end;但是 为什么 voutput总是得到乱码 呢??

解决方案 »

  1.   

     GetMem(voutput,2048); 
     因为你刚刚申请的内存没有初始化,里边的值都是随机的。
     而函数 
     vi:=GetInformation('amag@amag123','checkLogin',voutput); 
     不知道这个函数的实现,猜想voutput的值已经被填充了不过只是开头的几个字节而已
    所以你跟踪调试的时候看起来好像总是乱码。
    可以showmessage(voutput)出来看看。如果函数是以#0结尾的字符串填充的话
      

  2.   

    showmessage(voutput) 出来 还是乱码啊?我把vi 也像voutput 那样做就是把我 屏蔽的 vi 打开 
    vi 就能得到 正常的值 但是执行到 FreeMem(vi); 的时候出现 invalid pointer opertion
    这个指针错误  有办法解决吗?
      

  3.   

    new(voutput);
    .....
    vi:=GetInformation('amag@amag123','checkLogin',voutput);
    showmessage(voutput);//voutput的内容
    showmessage(inttostr(length(voutput)));//voutput的长度....
    dispose(voutput);
      

  4.   


    dll
    library VEHDRVEXT;uses
      SysUtils,
      Classes;{$R *.res}
    function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):Integer;stdcall;
    begin
      OutPut:='hello world';
      Result:=1;
    end;
      exports GetInformation;
    begin
    end.调用unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      FGetInformation = function (Const Con:PChar;Const Service:PChar; var OutPut:PChar):Integer;stdcall;  TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation  function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):Integer;stdcall;external 'VEHDRVEXT.DLL';
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      vi:Integer;
      voutput: PChar;
      str:string;
      hHandle:THandle;
      //GetInformation:FGetInformation;
      Con:PChar;
    begin
      str:='amag@amag123';
      GetMem(con,2048);
      strcopy(con,PChar(str));
      {
      hHandle:=LoadLibrary('VEHDRVEXT.dll');
      if hHandle>0 then
      begin
        @GetInformation:=GetProcAddress(hHandle,'GetInformation') ;
        if @GetInformation<>nil then
        begin
          vi:=GetInformation(con,'checkLogin',voutput);
          showmessage(voutput);
        end;
      end;
      FreeLibrary(hHandle);
      FreeMem(con);
      }  vi:=GetInformation(con,'checkLogin',voutput);
      showmessage(voutput);
      FreeMem(con);
    end;end.
      

  5.   

    修正一下
    new(voutput); 
    ..... 
    vi:=GetInformation(pchar('amag@amag123'),pchar('checkLogin'),voutput); 
    showmessage(voutput);//voutput的内容 
    showmessage(inttostr(length(voutput)));//voutput的长度 .... 
    dispose(voutput);
      

  6.   

    还是不行的啊
    各位老大 
    用 yunqianyi1974 的 没有值  在执行到dispose(voutput); 的时候出现 
    invalid pointer opertion 错误 所以不对用hongqi162 专家的 voutput:nil 好像没调用成功 还是怎么回事
    不给 voutput 分GetMem(voutput,2048);好像就这样dll 是别人写的 他里面有很多 函数所以
    他这样写的
    library VEHDRVEXT;uses
      ShareMem,
      GetInfoServer in '..\unit\GetInfoServer.pas',
      SaveInfoServer in '..\unit\SaveInfoServer.pas',
      ExportInter in '..\unit\ExportInter.pas',
      Config in '..\unit\Config.pas',
      Globalvar in '..\unit\Globalvar.pas',
      Picconvert in '..\unit\Picconvert.pas';exports
       InitMem,
       UnInitMem,
       GetInformation,
       SaveInformation;
    {$R *.res}beginend.
    写的人有个 exe 他都调用成功了 的(汗 但没源文件) 所以我怀疑 是 我调用的问题 
    但有找不出来 
    各位 大大 在给点提示啊  谢个了!
      

  7.   

    我 刚才又给 GetMem(voutput,2048); 分了空间也FreeMem(voutput);了
    没有出现 invalid pointer opertion 的指针错误 
    但是 voutput 还是乱码 好像没调用成功似的   但 没什么错误提示 快帮帮忙啊  各位大大 这个问题卡 我好些天了 
      

  8.   

    var
      voutput:pchar;
    //new(voutput); 
    ..... 
    vi:=GetInformation(pchar('amag@amag123'),pchar('checkLogin'),voutput); 
    showmessage(voutput);//voutput的内容 
    showmessage(inttostr(length(voutput)));//voutput的长度 .... 
    //dispose(voutput); 
    这样行不行
      

  9.   

    var
      voutput:pchar;
    //new(voutput); 
    ..... 
    vi:=GetInformation(pchar('amag@amag123'),pchar('checkLogin'),voutput); 
    showmessage(voutput);//voutput的内容 
    showmessage(inttostr(length(voutput)));//voutput的长度 .... 
    //dispose(voutput); 
    这样行不行
      

  10.   

    或者这样
    var 
      voutput:pchar; 
    new(voutput); 
    ..... 
    vi:=GetInformation(pchar('amag@amag123'),pchar('checkLogin'),voutput); 
    showmessage(voutput);//voutput的内容 
    showmessage(inttostr(length(voutput)));//voutput的长度 .... 
    voutput:=nil;
    dispose(voutput); 
      

  11.   

    还有个奇怪的问题 
    动态库都一样 但是 我把 函数这样 一变
    function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):PChar;stdcall;external 'VEHDRVEXT.DLL'; 把vi:PChar 调用出来的 vi =‘2:登录成功’是我要的答案 
    但是在 dispose(vi)的时候出现 invalid pointer opertion 的指针错误 这是为什么出现错误啊  有没办法解决呢?各位大大 ??
    都来帮小弟 转转啊!
      

  12.   

    function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):PChar;stdcall;external 'VEHDRVEXT.DLL'; 

    function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):Integer;stdcall;external 'VEHDRVEXT.DLL'; 
    不一样的你声明有问题当然不好用了
      

  13.   

    但是 为什么要的 voutput 千呼万唤 不出来呢?vi:=nil; 
    dispose(vi);  加了vi:=nil; 到是不出 invalid pointer opertion 的指针错误
    了 但是 vi 始终 都是 nil了 为什么 位换了声明 vi能得到 想要的东西呢? 照理 该是调用 dll不成功啊
     
      

  14.   

    接了啊 现在是这样的申明
    function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):PChar;stdcall;external 'VEHDRVEXT.DLL'; 最后 vi 值 就是我想要的 加了vi:=nil; 
    dispose(vi);  也不出 指针错误了 .就暂时先这样用吧  但是我很 困惑的是 我看了dll
    里面 函数 明明是 function GetInformation(Const Con:PChar;Const Service:PChar; var OutPut:PChar):Integer;stdcall; 这样的 
    怪 事 .
    在次谢谢各位 大大啊  特别是1974 .