我的程序是这样的
TreturnDbname=function():String;stdcall;procedure TForm1.Button1Click(Sender: TObject);
var
   returnDbname : TReturnDbname;
   AHandle: THandle;
begin    CoInitialize(nil);
    AHandle:=LoadLibrary('CreatePro.dll');
    try
       if AHandle<>0 then
          @returnDbname:=GetProcAddress(AHandle,'returnDbName');
          if not(@returnDbname=nil) then
          begin              dbname:=returnDbname();
              showmessage('dbname='+dbname);
          end;
    finally
        freeLibrary(AHandle);
    end;
end;
我把freeLibrary(AHandle);注释掉程序执行完了关闭窗口时,但是整个project1却没有关闭依然在内存里运行着;而加上freeLibrary(AHandle);在程序运行完毕连showmessage('dbname='+dbname);也执行对了点击showmessage出现的窗口后就出现这样的错误:Access violation at address 00403F7E in module 'project1.exe'.Read of address 0168B792C.请问这是什么原因啊,该怎么解决,谢谢!

解决方案 »

  1.   

    uses ShareMem;
    或将string用PChar替换
      

  2.   

    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.
      

  3.   

    呵一会儿功夫就有人给出答案了.
    确实是STRING类型引起的错误.
    在DLL中如果要使用字符串类型.最好还是使用字符串指针PCHAR类型为好.
    下面是我的测试代码.......type
      TFun=function (var source:pchar):byte;stdcall;
    var
       Fun:tfun;
       dllhandle:thandle;
       p:pchar;
    begin
       dllhandle:=loadlibrary('StrTestDll.dll');
       try
          if dllhandle>32 then
            begin
              @fun:=getprocaddress(dllhandle,'TEST');
              if not (@fun=nil) then
                 begin
                  fun(p);
                  showmessage(strpas(p));
                 end;
            end;
       finally
         freelibrary(dllhandle);
       end;
    end;