heresma
heresma

Reputation: 293

Runtime error 216 on IE when using BHO

I am working on a browser helper object written in Delphi, and when the BHO is installed and I close IE, I get the error "runtime error 216 at < address >". I suspect this could be because of the 253 disID (onquit) case on the following code:

function TIEM.Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
      Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult;
type
  POleVariant=^OleVariant;
var
  dps:TDispParams absolute Params;
  bHasParams:Boolean;
  pDispIDs:PDispIDList;
  iDispIDsSize:Integer;
begin
  Result:=DISP_E_MEMBERNOTFOUND;
  pDispIDs:=nil;
  iDispIDsSize:=0;
  bHasParams:=(dps.cArgs>0);
  if(bHasParams)then
  begin
    iDispIDsSize:=dps.cArgs*SizeOf(TDispID);
    GetMem(pDispIDs,iDispIDsSize);
  end;
  try
    if(bHasParams)then BuildPositionalDispIDs(pDispIDs,dps);
    case DispID of
      104:begin
          Result:=S_OK;
        end;
      250:begin
          DoBeforeNavigate2(IDispatch(dps.rgvarg^[pDispIDs^[0]].dispVal),
            POleVariant(dps.rgvarg^[pDispIDs^[1]].pvarVal)^,
            POleVariant(dps.rgvarg^[pDispIDs^[2]].pvarVal)^,
            POleVariant(dps.rgvarg^[pDispIDs^[3]].pvarVal)^,
            POleVariant(dps.rgvarg^[pDispIDs^[4]].pvarVal)^,
            POleVariant(dps.rgvarg^[pDispIDs^[5]].pvarVal)^,
            dps.rgvarg^[pDispIDs^[6]].pbool^);
          Result:=S_OK;
        end;
      252:
        begin
          DoNavigateComplete2(IDispatch(dps.rgvarg^[pDispIds^[0]].dispval), POleVariant(dps.rgvarg^[pDispIds^[1]].pvarval)^);
          Result := S_OK;
        end;
      259:
        begin
          DoDocumentComplete(IDispatch(dps.rgvarg^[pDispIds^[0]].dispval), POleVariant(dps.rgvarg^[pDispIds^[1]].pvarval)^);
          Result := S_OK;
        end;
      253:
        begin
          Result := S_OK;
        end;
    else
      Result := DISP_E_MEMBERNOTFOUND;
    end;
  finally
    if(bHasParams)then
      FreeMem(pDispIDs,iDispIDsSize);
  end;
end;

But I am not sure and I couldn't find any info about it. I am using a library I got from an example on Hack China to create the BHO, and I found some project on Google Code that uses IConnectionPoint.Unadvise(Integer) on the 253 case. I tried that, but still get the same runtime error 216. I've also tried adding an exception handler to the above code, but it didn't catch anything.

I added:

finalization
  exit;

And now I don't see the runtime error. I didn't know the BHO would need that.

Upvotes: 2

Views: 813

Answers (2)

heresma
heresma

Reputation: 293

I added:

finalization
  exit;

And now I don't see the runtime error. I didn't know the BHO would need that.

Upvotes: 1

Marjan Venema
Marjan Venema

Reputation: 19356

A 216 error when exiting your app means your are triggering an Access Violation in the finalization code of your project after the sysutils unit has already been finalized.

So, check all your finalization sections for use of invalid pointers. In your search include the finalization sections of all components you use in the project.

To debug finalization sections, you can put a breakpoint on the "end" statement in the dpr and when the debugger breaks on that, use F7 to step into the finalization code, then use F7 and F8 to step through all the finalization sections. It will be a tedious process, but it will bring you to the exact statement causing the Access Violation.

Upvotes: 4

Related Questions