就是执行接口的成员函数本身没有问题(正确返回数据),
却在离开 加载dll 程序块 是出错(好像是那个时期出现的--F8 跟踪不到确切位置),
出错信息为:“Invalid Pointer Operation”另外,DLL 的工程单元,uses ShareMem 与否都有问题----------------------------------------------------------------------------
Code:
----------------------------------------------------------------------------// 接口文件定义
unit UMyIntf;interfacetype
  IMyIntf = interface
  ['{94A5FCF2-4B2D-4196-B335-7721E76083B2}']
    procedure TestProc(var tt: string);
    function  TestFunc: string;
  end; implementation
 end.
----------------------------------------------------------// 主程序加载dll部分
procedure LoadDll(dllName: string);
var
  hModule: THandle;
  pProc: function : Integer;
  sTest1, sTest2: string;
  Entry: IMyIntf;
begin
  hModule := LoadLibrary(dllName);
  if hModule <> 0 then
  begin
    try
      @pProc := GetProcAddress(hModule, 'GetThisModule');      if pProc <> nil then
      begin
        Entry := IMyIntf(pProc); Entry.TestProc(sTest1);  // 执行这2个函数后 会出现
sTest2 := Entry.TestFunc;//   “Invalid Pointer Operation”的运行时错误
       end;                    
    finally
      FreeLibrary(hModule);
    end;
  end;  ShowMessage(sTest1);
  ShowMessage(sTest2);
  // 执行到这里都没问题
  // 上面提到的 指针访问错误 好像是在出这个Proc的发生的,奇怪
end;--------------------------------------------------------------
// DLL 接口实现及 出口函数部分
unit UTestDllIntf;interfaceuses 
  UMyIntf, Classes;type
  TTestDll = class(TInterfacedObject, IMyIntf)
  public
    procedure TestProc(var tt: string);
    function  TestFunc: string; 
  end;implementationvar
  ThisModule: IMyIntf;{TTestDll}
procedure TTestDll.TestProc(var tt: string);
begin
  tt := 'Test Dll Proc';
end;function TTestDll.TestFunc: string;
begin
  Result := 'Test Dll Func';
end;function GetThisModule: Integer; stdcall; export;
begin
  Result := Integer(ThisModule);
end;exports
  GetThisModule name 'GetThisModule';initialization
  ThisModule := TTestDll.Create;
finalization
  ThisModule := nil;
end.

解决方案 »

  1.   

    // 以下重新 确切表达为
    Entry.TestProc(sTest1);  // 如果执行这2个函数, 稍候 会出现
    sTest2 := Entry.TestFunc;//   “Invalid Pointer Operation”的运行时错误
      

  2.   

    除了  paranoia190(190) ...
      

  3.   

    try
          @pProc := GetProcAddress(hModule, 'GetThisModule');      if @pProc <> nil then
          begin
            Entry := IMyIntf(pProc);        Entry.TestProc(sTest1);  // 执行这2个函数后 会出现
            sTest2 := Entry.TestFunc;//   “Invalid Pointer Operation”的运行时错误
          end;                    
        finally
          Entry := nil; {这里加上这句}
          FreeLibrary(hModule);
        end;
      

  4.   

    来晚了,就如楼上所说,如果不加那一句Entry := nil; {这里加上这句}的话,就会在End之后执行,而此时已经FreeLibrary掉了,所以会出错。因为Entry := Nil;这句会隐含调用:Entry._Release;