procedure TMainForm.mmiHelpClick(Sender: TObject);
Type
  TCommuConnect_ErrorCodes=Function(x:byte):String;stdcall;
var
  Commu_Code:byte;
  Th:THandle;
  Tf:TCommuConnect_ErrorCodes;
  Tp:TFarproc;
begin
     comBioMed :=CreateOLEObject('BioMed.Instruments');
     Commu_Code :=comBioMed.SetSetting(USB_Communicate);
try
     Th:=LoadLibrary(PChar('ErrorHandling.dll'));
     if Th>0 then
        @Tf:=GetProcAddress(Th,PChar('CommuConnect_ErrorCodes'));
        if Assigned(Tf) then
           begin
           Label1.Caption:=Tf(Commu_Code);
           end;
finally
     FreeLibrary(Th);//单步执行到这是报上述错误
end;
end;
此程序是完成返回一个字符串功能,报‘Access Vilolation at address.....’错误,但字符还能正常返回来显示。

解决方案 »

  1.   

    { 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. }To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. 
      

  2.   


    在DLL中使用String类型:
    1、在你的DLL项目源文件中第一个引用ShareMem单元;
    2、你必须在你的应用程序项目源文件中第一个引用SHAREMEM单元;
      

  3.   

    问题可能出在这里:
      TCommuConnect_ErrorCodes=Function(x:byte):String;stdcall;改成:
      TCommuConnect_ErrorCodes=Function(x:byte):PChar;stdcall;  //返回值类型改为PChar试一下吧