Debugging error on using TAdvWebbrowser TCoreWebView2HistoryChangedEventHandler

Hi, when using an TAdvWebbrowser in combination with TCoreWebView2HistoryChangedEventHandler the Delphi debugger gives us a Critical error message.

It seems to be related to the ExecuteJavascript function we have in our code:

procedure TFormITREPOS_Protserver_Inloggen.FormCreate(Sender: TObject);
var
w: ICoreWebView2;
c: ICoreWebView2Controller;
e: EventRegistrationToken;
begin
c := ICoreWebView2Controller(AdvWebBrowserLogin.NativeBrowser);
if c.get_CoreWebView2(w) = S_OK then
begin
w.add_HistoryChanged(TCoreWebView2HistoryChangedEventHandler.Create, @e);
end;
end;

{ TCoreWebView2HistoryChangedEventHandler }

function TCoreWebView2HistoryChangedEventHandler.Invoke(sender: ICoreWebView2;
args: IInterface): HRESULT;
var
s: string;
cText: string;
begin
Result := S_OK;
if sender.get_Source(@s) = S_OK then
begin
cText := PChar(s);
FormITREPOS_Protserver_Inloggen.AdvWebBrowserLogin.ExecuteJavaScript('window.sessionStorage.getItem("default_oauth")',
procedure(const AValue: string)
begin
if AValue<>'null' then
begin
FormITREPOS_Protserver_Inloggen.InlogInfo.DefaultToken := AValue;
end;
end);
end;
end;

After this ExecuteJavaScript has been executed the debugger (sometimes) gives an error. When closing the form or application. (Only when debugging)
Debug Output:
Crirical error detected c0000374

Any idea what is causing this error and how we can resolve this?

This is the first time we encounter this kind of issue. Can you try to call the code inside the Invoke from a separate thread instead?

function TCoreWebView2HistoryChangedEventHandler.Invoke(Sender: ICoreWebView2;
  args: IInterface): HRESULT;
var
  s: string;
  cText: string;
  aTask: ITask;
begin
  Result := S_OK;
  if Sender.get_Source(@s) = S_OK then
  begin
    cText := PChar(s);
    aTask := TTask.Create(
      procedure
      begin
        FormITREPOS_Protserver_Inloggen.AdvWebBrowserLogin.ExecuteJavaScript
          ('window.sessionStorage.getItem("default_oauth")',
          procedure(const AValue: string)
          begin
            if AValue <> 'null' then
            begin
              FormITREPOS_Protserver_Inloggen.InlogInfo.DefaultToken := AValue;
            end;
          end);
      end);
    aTask.Start;
  end;
end;

Hi @Pieter_Scheldeman, unfortunately your suggestion doesn't resolve the issue.

In addition: when just running the application (without debugging), we experience random crashes.

Very strange issue. Can you throw together a small sample that we can try to reproduce?

Hi @Pieter_Scheldeman we wil setup a small sample for you to reproduce. Since the sample will connect to our servers and we don't want to expose any addresses etc., is it possible to send the sample to you some secure way in stead of uploading it here (public)?

You can send it via a private message directly to me, or to support@tmssoftware.com

Hi,

Thank you for the sample, I was able to reproduce the issue here after 4 tries, and I'm currently investigating it.

Please change your code to:

var
s: PWideChar;
cText: string;
begin
  Result := S_OK;
  if sender.get_Source(@s) = S_OK then
  begin
    cText := s;

When changing the pointer, I was no longer able to reproduce the issue.

Hi @Pieter_Scheldeman, thanks for the quick response. We will change the code and hopefully also have no more issues :)

:crossed_fingers: let's hope :slight_smile:

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.