【发布时间】:2021-09-09 04:37:21
【问题描述】:
一个可以帮助回答我之前提出的另一个问题的一般性问题。我相信 COM 对象只返回一个指向创建对象的指针。所以当我尝试初始化一个 COM 对象两次时,我是在我的应用程序中创建两个 COM 对象,还是创建两个指向同一个对象的不同指针?或者指针指向它们各自的对象的对象可能有两个副本?
// Pointer to WebViewController
static wil::com_ptr<ICoreWebView2Controller> webviewController;
// Pointer to WebView window
static wil::com_ptr<ICoreWebView2> webviewWindow;
// Locate the browser and set up the environment for WebView
CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hWnd, url](HRESULT result, ICoreWebView2Environment* env) -> HRESULT {
// Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
env->CreateCoreWebView2Controller(hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[hWnd, url](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT {
_com_error err4(result);
MessageBoxW(hWnd, L"Controller setup", 0, MB_OK);
MessageBoxW(hWnd, err4.ErrorMessage(), 0, MB_OK);
if (controller != nullptr) {
webviewController = controller;
webviewController->get_CoreWebView2(&webviewWindow);
}
// Add a few settings for the webview
// The demo step is redundant since the values are the default settings
ICoreWebView2Settings* Settings;
webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Settings->put_IsWebMessageEnabled(TRUE);
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewController->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
webviewWindow->Navigate(url);
if (GetWindow(hWnd, GW_CHILD) != NULL) {
MessageBoxW(hWnd, L"not a NULL child window", 0, MB_OK);
};
// Step 4 - Navigation events
// Step 5 - Scripting
// Step 6 - Communication between host and web content
return S_OK;
}).Get());
return S_OK;
}).Get());
我打过电话
CoInitialize(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); 前面的代码只有一次。我的问题是当我运行这个块两次时会发生什么?
【问题讨论】:
-
您可以通过两次调用函数并比较返回的两个指针来凭经验确定这一点。
-
@jon 这不是比较 COM 对象的身份的方式。所有比较操作都要求您首先查询规范的
IUnknown,然后才能以有意义的方式比较指针是否相等。 -
@JonathanPotter - 这只是 IUnknown 的 COM 要求:docs.microsoft.com/en-us/windows/win32/com/…
-
@jon 你把“你觉得合理的东西”与基本的 COM 规则混淆了。我已经解释了基本的 COM 规则是什么,Simon 提供了文档链接。
-
我只能说我很想看到可以在同一个地址实现两个不同对象的示例代码。