【发布时间】:2020-12-26 14:10:06
【问题描述】:
我想在我的窗口上实现 Internet Explorer webview 控件。 我找到了this 的答案,关于如何做到这一点。
有一个问题:答案中的Navigate2 方法与我的标题不同。在海报的代码中,它似乎只有一个参数,而其他参数可能是默认的,但我有 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;
}
由于0x0 读取冲突,在方法调用时返回异常。
【问题讨论】:
-
试试地址.bstrVal = ::SysAllocString(L"google.com"); ... ::SysFreeString
-
@wtom 我试过没有
SysFreeString它也返回异常。我应该试试SysFreeString吗? -
你用 SysAllocString 调用分配 adress.bstrVal 吗? docs.microsoft.com/en-us/cpp/atl-mfc-shared/…
-
我们需要您提供完整的代码。例如,您缺少
_variant_t,因为您没有包含相同的标题。 -
@wtom:从
<comdef.h>使用_bstr_t会容易得多。