procedure Secret16Encrypt(lpInBuffer:PChar; lpKey:PChar);
var
  Secret16Encrypt: function(lpInBuffer:PChar; lpKey:PChar; lpOutBuffer:PChar): Boolean;  stdcall;
  OutputStr: array [0..16] of Char;
begin
  FillChar(OutputStr, Sizeof(OutputStr), #0);
  Initialize;
  if hlib <> 0 then begin
    @Secret16Encrypt := GetProcAddress(hlib, 'Secret16Encrypt');
    if @Secret16Encrypt <> nil then begin
      if Secret16Encrypt(lpInBuffer, lpKey, OutputStr) then
        Return := OutputStr
      else
        Return := '函数调用错误!'
      end
    else
      ShowMessage('加载功能模块出错!');    FreeLibrary(hlib);
  end
  else
    ShowMessage('无法加载DLL!');
end;上面这段代码是我从人家的一个DEMO里面拷贝出来的,它功能是就是输入一个字符串和一个Key然后得到加密结果,但是它没有通过函数返回加果,而是通过函数参数返回结果,lpOutBuffer就是返回结果,这样的效果应该在DLL怎么定义呢?
不用VAR应该怎么实现?

解决方案 »

  1.   

    PChar就是一个指针,指向返回结果的内存首地址,所以和var是一回事。
      

  2.   

    lpOutBuffer就是把指针传递到函数里面,定义没什么特殊的。
    function Secret16Encrypt(lpInBuffer:PChar; lpKey:PChar; lpOutBuffer:PChar): Boolean;
      

  3.   

    pchar是一个指向字符的指针,在dll中都是这样作的,delphi里的dll返回string不通用。用指针才能兼营microsoft的c,c++等。
      

  4.   

    hlib定义的是DLL名字。
    可能大伙还没有看明白我的意思吧,我的意思是最后函数返回加密后的值是返回给lpOutBuffer,这样是怎么样做到的?
      

  5.   

    我的全部调用代码如下这是DLL代码:
    library AppDll;uses
      SysUtils,Windows,Classes,Dialogs;{$R *.res}function AddStr(lpInputStr:PChar;lpOutputStr:PChar):Boolean;stdcall;
    begin
      try
        lpOutputStr:=PChar(StrPas(lpInputStr)+'abcdef');
        Result:=True;
      except
        Result:=False;
      end;  
    end;exports
      AddStr;
      
    begin
    end.主程序代码:
    var
      hlib: HMODULE;implementation{$R *.dfm}procedure Initialize;
    begin
      hlib:= LoadLibrary('AppDLL.dll');
    end;Procedure Str(lpInputStr:PChar);
    var
      AddStr: function(lpInputStr:PChar;lpOutputStr:PChar): Boolean;  stdcall;
      DllStr:array[0..30] of Char;
    begin
      FillChar(DllStr, SizeOf(DllStr), #0);
      Initialize;
      if hlib <> 0 then begin
        @AddStr := GetProcAddress(hLib, 'AddStr');
        if @AddStr <> nil then begin
          if AddStr(lpInputStr, DllStr) then
            Form1.Edit1.Text:= DLLStr
          else
            Form1.Edit1.Text:= '函数调用错误!'
        end
        else
          ShowMessage('加载功能模块出错!');    FreeLibrary(hLib);
      end
      else
        ShowMessage('无法加载DLL!');
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Str(PChar('123456'));
    end;