【问题标题】:Access violation reading a cookie with Delphi embedded Chromium (CEF1)使用 Delphi 嵌入式 Chromium (CEF1) 读取 cookie 的访问冲突
【发布时间】:2013-02-01 08:47:56
【问题描述】:

我在我的应用程序中使用 Delphi Embedding Chromium (CEF1),但无法读取 URL 的 cookie 数据。

我找到了this code(包括在下面),但是在 XE3 上,当我使用它时,我遇到了一个异常:

if WaitForSingleObject(vis.pEvent.Handle, INFINITE) = WAIT_OBJECT_0 then begin

例外是

项目 guiclient.exe 引发异常类 EAccessViolation 消息“地址 00000000 的访问冲突”。读取地址 00000000

暗示其中一个对象未​​正确创建或初始化。

我使用的代码(从上面的论坛链接复制)是:

  TCustomVisitor = class (TCefCookieVisitorOwn)
    private
      fcookie: PCefCookie;
      function visit(const name, value, domain, path: ustring; secure, httponly,
            hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;
            count, total: Integer; out deleteCookie: Boolean): Boolean; override;
    public
      pEvent: TEvent;
      function getCookies: PCefCookie;
      constructor Create; override;
  end;

constructor TCustomVisitor.Create;
begin
  inherited;
  pEvent := TEvent.Create(nil, False, False, 'ev.chromium');
  new(fcookie);
end;

function TCustomVisitor.getCookies;
begin
  Result := fcookie;
end;

function TCustomVisitor.visit(const name, value, domain, path: ustring; secure, httponly,
            hasExpires: Boolean; const creation, lastAccess, expires: TDateTime;
            count, total: Integer; out deleteCookie: Boolean): Boolean;
begin
    fcookie.name := CefString(name);
    fcookie.value := CefString(value);
    fcookie.domain := CefString(domain);
    fcookie.path := CefString(path);
    fcookie.secure := secure;
    fcookie.httponly := httponly;
    fcookie.has_expires := hasExpires;
    //fcookie.creation := DateTimeToCefTime(creation);
    //fcookie.last_access := DateTimeToCefTime(lastAccess);
    //fcookie.expires  := DateTimeToCefTime(expires);

    SetEvent(pEvent.Handle);
end;


procedure TfrmAuth.bt_okClick(Sender: TObject);
var
  vis: TCustomVisitor;
  cname, cvalue: uString;
  ccookie: PCefCookie;
begin
  if crm.Browser<>nil then begin
    vis := TCustomVisitor.Create;
    try
      CefVisitUrlCookies(ed_url.Text, true, vis);

      // !!! This line causes the access violation
      if WaitForSingleObject(vis.pEvent.Handle, INFINITE) = WAIT_OBJECT_0 then begin
        ccookie := vis.getCookies;
        cname := CefString(@ccookie.name);
        cvalue := CefString(@ccookie.value);
      end;
    finally
      vis.Free;
    end;
  end;
end;

【问题讨论】:

  • 你得到的错误是什么?错误信息很有价值 - 请张贴!此外,可能值得包含您的代码,而不是论坛链接。最好(a)在问题本身中包含所有信息,而不是将来可能会消失的链接; (b) 查看您的实际代码,以防原因是转换示例代码以适应您的应用时出错。
  • 我没有报告错误,因为它不是“人为”错误,是十六进制代码(我认为)。我认为链接中的代码有问题。
  • 那么,十六进制代码是什么? (ICE,内部编译器错误?或者它是否编译并且代码是 HRESULT 吗?或者......它是什么?我不知道,你还没有说。)请包括所有信息! 基于猜测,我们无法为您提供帮助。
  • 这是错误:“项目 guiclient.exe 引发异常类 EAccessViolation 并带有消息‘地址 00000000 的访问冲突’。读取地址 00000000”。这对你有帮助吗?
  • TLama,我不确定,他实际上还没有发布他的实际代码。 Martin,复制/粘贴代码很容易出错...,如果需要帮助,需要write a better question。我已将您的问题编辑得更清楚,但请添加您自己的真实代码以及任何其他有用信息。

标签: delphi cookies tchromium chromium-embedded


【解决方案1】:

这很简单。 CefVisitUrlCookies 将您的对象作为引用计数接口,并在返回时释放它,并且对象会自行销毁,因此您在尝试访问炸对象时会遇到访问冲突。 为避免此问题,您可能希望在局部变量中存储对对象的引用,或显式调用 _addref:

vis._AddRef();
CefVisitUrlCookies(ed_url.Text, true, vis);
WaitForSingleObject(...)
vis._Release();

ivis: ICefCookieVisitor;
...
ivis := vis;
CefVisitUrlCookies(ed_url.Text, true, ivis);
WaitForSingleObject(...)
ivis := nil;

并且不要忘记删除免费通话。您永远不应该使用引用计数显式释放接口对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多