如何传递 IWebBrowser2::Navigate2 个参数?

How to pass IWebBrowser2::Navigate2 arguments?

我想在 window 上实现 Internet Explorer webview 控件。 我找到了 答案,关于如何做到这一点。

有一个问题:Navigate2的回答方法和我的headers不一样。在发布者的代码中,它似乎只有一个参数,也许其他参数是默认的,但我有 5 个参数,这是我见过的最愚蠢的东西 - VARIANT 类型变量(另外,在发布者的代码中它是 _variant_t 这对我来说是未定义的)。

可能我永远不会理解 sEiFe 逻辑,为什么要制作而不是 Navigate2(wchar_t *,...) 很酷的东西 VARIANT *(我知道 Navigate 方法),但是谁能提供一个例子调用该方法。

完整代码

#include <Windows.h>
#include <Ole2.h>
#include "resource.h"
#include <iostream>
#include <atlbase.h> //activex
#include <atlwin.h> //windows
#include <atlcom.h>
#include "exdisp.h"
#include <comutil.h>

#pragma comment(lib, "comsuppw.lib")

//This will load the browser dll then library and will generate headers
//All the declarations will be in the namespace SHDocVw
//#import "shdocvw.dll"
using namespace std;

class CMyDialog : public CAxDialogImpl<CMyDialog>
{
public:
enum { IDD = IDD_DIALOG1 };

BEGIN_MSG_MAP(CMyDialog)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnBnCancel)
COMMAND_HANDLER(IDOK, BN_CLICKED, OnBnOk)
END_MSG_MAP()
CComPtr<IWebBrowser2>  ctrl;
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Do some initialization code
HRESULT hr;
//IDC_EXPLORER_TEST is the ID of your control
GetDlgControl(IDC_EXPLORER_TEST, __uuidof(ctrl), (void**)&ctrl);

VARIANT address;
address.vt = VT_BSTR;
address.bstrVal = SysAllocString(L"google.com");

VARIANT empty;
empty.vt = VT_EMPTY;

hr = ctrl->Navigate2(&address, &empty, &empty, &empty, &empty);

SysFreeString(address.bstrVal);

/* 

Also fails

_variant_t a = SysAllocString(L"google.com");
VARIANT f;
f.vt = VT_I2;
f.iVal = navBrowserBar;
_variant_t fr = SysAllocString(L"_self");
_variant_t h = SysAllocString(L" ");

hr = ctrl->Navigate2(&a, &f, &fr, &h, &h);
*/
  
LRESULT res = CAxDialogImpl<CMyDialog>::OnInitDialog(uMsg, wParam, lParam, bHandled);
return 0;
}
public:
LRESULT OnBnCancel(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
EndDialog(IDCANCEL);
return 0;
}
LRESULT OnBnOk(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
EndDialog(IDOK);
return 0;
}
};
CComModule _Module;

int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int)
{
CMyDialog dlg;
dlg.DoModal();

return 0;
}

Returns 由于 0x0 读取违规,方法调用出现异常。

您似乎未能检查 GetDlgControl 是否成功。失败时,ctrl 有一个未指定的值,可能为空。

当然,这留下了为什么它会失败的问题,但那是另一个问题。