第一种情况如下,无论是否在DLL中改变li的值,都一切正常。
function login(var li:string):WordBool;Far;External 'mylogin.dll';
var
  li:string;
begin
  Application.Initialize;
  initsystem;
  ///////li:='10';
  if login(li) then
  begin
    Application.CreateForm(TfrmMain, frmMain);
    Application.run;
  end
  else
    Application.Terminate;
end.第二种情况如下:在DLL中改变li的值,关闭主窗体时报无效的指针错误,有时也报AV错误。
function login(var li:string):WordBool;Far;External 'mylogin.dll';
var
  li:string;
begin
  Application.Initialize;
  initsystem;
  li:='10';
  if login(li) then
  begin
    Application.CreateForm(TfrmMain, frmMain);
    Application.run;
  end
  else
    Application.Terminate;
end.
请高手指点,另外,如果li为integer型,就一点错误也没有。DLL中部分代码如下:function login(var li:string):WordBool;
begin
  iclick:=0;   //计数器,最多输入三次口令;
  result:=false;
  frmLogin:=TfrmLogin.create(application);
  try
    if frmLogin.showmodal=mrOK then
      with frmLogin do
      begin
        li:=edit1.Text;
        result:=true;
      end;
  finally
    frmLogin.Close;
    frmLogin.free;
  end;
end;
procedure TfrmLogin.LbSpeedButton1Click(Sender: TObject);
begin
  ModalResult:=mrOK;
end;
procedure TfrmLogin.LbSpeedButton2Click(Sender: TObject);
begin
  modalresult:=mrCancel;
end;

解决方案 »

  1.   

    第二种: li:='10';li的指针被Delphi编译器优化指向一个常量地址。所以。
      

  2.   

    请将li的类型由string改为PChar类型。
      

  3.   

    顶,明天上班去试试对了,还想问个,DLL怎么调试?只能BUILD啊?
      

  4.   

    run->parameter
    设置你的调用程序,或者%windir%\system32\rundll.exe
      

  5.   

    li没用的话,就会被优化,直接地跳过,你F7看看,是不是有执行。
      

  6.   

    各位
    用PCHAR后是不报错了
    我在主程序中赋了初值
    在DLL中进行修改
    但是发现在回到主程序时变量还是初值,没有修改?怎么回事?谢谢
      

  7.   

    TO: YiOnLine(Yi==小精,牛大少) OK,多谢,解决了,是什么原因啊?
      

  8.   

    学做第一个DLL时就应该知道:
    library Project1;{ 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;{$R *.res}begin
    end.