【发布时间】:2015-03-18 13:08:24
【问题描述】:
在过去的几周里,我一直在 ABI 层研究 WRL,并遇到了这个问题。
我在 IDL 中定义了如下接口:
namespace Async{
[uuid(f469e110-7ef5-41df-a237-9ddef9aed55c), version(1.0)]
interface IDownloader : IInspectable
{
HRESULT GetFeed([in] HSTRING url,[out, retval] Windows.Web.Syndication.SyndicationFeed ** feed);
[propget]HRESULT Feed([out, retval]Windows.Web.Syndication.SyndicationFeed ** feed);
}
[version(1.0), activatable(1.0)]
runtimeclass Downloader
{
[default] interface IDownloader;
}
}
我在头文件中这样定义的:
#pragma once
#include "Async_h.h"
namespace ABI {
namespace Async {
class Downloader : public Microsoft::WRL::RuntimeClass<ABI::Async::IDownloader>
{
InspectableClass(L"Async.Downloader", BaseTrust);
public:
Downloader();
STDMETHOD(GetFeed)(HSTRING url, ABI::Windows::Web::Syndication::ISyndicationFeed ** feed);
STDMETHOD(get_Feed)(ABI::Windows::Web::Syndication::ISyndicationFeed ** feed);
private:
//Microsoft::WRL::ComPtr<ABI::Windows::Foundation::Uri> feedUrl;
Microsoft::WRL::ComPtr<ABI::Windows::Web::Syndication::ISyndicationFeed> m_feed;
};
ActivatableClass(Downloader);
}
}
在我的 cpp 文件中,我实现了以下功能:
STDMETHODIMP Downloader::GetFeed(HSTRING url, ISyndicationFeed** feed)
{
HRESULT hr;
RoInitializeWrapper ro(RO_INIT_MULTITHREADED);
ComPtr<IUriRuntimeClass> uri;
ComPtr<IUriRuntimeClassFactory> uriFactory;
hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);
hr = uriFactory->CreateUri(url, uri.GetAddressOf());
ComPtr<ISyndicationClient> client;
ComPtr<IInspectable> inspectable;
RoActivateInstance(HStringReference(RuntimeClass_Windows_Web_Syndication_SyndicationClient).Get(), &inspectable);
hr = inspectable.As(&client);
Event timerCompleted(CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, WRITE_OWNER | EVENT_ALL_ACCESS));
auto callback = Callback<IAsyncOperationWithProgressCompletedHandler<SyndicationFeed*,RetrievalProgress>>([&](IAsyncOperationWithProgress<SyndicationFeed*,RetrievalProgress> *op, AsyncStatus status) ->HRESULT
{
auto error = GetLastError();
if (status == AsyncStatus::Completed)
{
hr = op->GetResults(m_feed.GetAddressOf());
*feed = m_feed.Get();
}
return S_OK;
});
ComPtr<IAsyncOperationWithProgress<SyndicationFeed*,RetrievalProgress>> operation;
hr = client->RetrieveFeedAsync(uri.Get(), operation.GetAddressOf());
operation->put_Completed(callback.Get());
return S_OK;
}
STDMETHODIMP Downloader::get_Feed(ISyndicationFeed** feed)
{
*feed = m_feed.Get();
return S_OK;
}
该属性按预期工作,它应该是投影到 c++/cx 的。但是,在 GetFeed 方法中,当我尝试将 feed 参数设置为检索到的 feed 时,我遇到了访问冲突。显然我知道内存不好,但我理解 COM 属性的方式,它们本质上是函数调用,而属性方法和 GetFeed 方法在减去检索部分后做的事情完全相同。
这是我的问题:
- COM 属性方法和常规接口方法在预计返回值方面有什么区别?
- 为什么属性方法的参数初始化为 nullptr 而 GetFeed 方法的参数在 IDL 中的描述完全一样?
- 如果属性方法中的 out 参数被初始化,COM 运行时的哪个部分正在为我执行此操作并且是可控的? IE 有没有办法让我可以写入的内存传递给我?
我知道我可能会设计它,但这不是重点。我只是想了解它是如何工作的。
谢谢。
【问题讨论】:
-
至少对于 C++ 来说,COM 属性和 IDL 中定义的 COM 方法之间没有明显区别。只有特定的消费者,例如 VB,才会出现差异。我不确定为什么“get_Feed”的参数会为空。
-
我注意到在我们自己古老的 COM 代码中,我们实际上会检查空参数并返回“E_POINTER”,尽管我不知道为什么会发生这种情况。
-
是的,我也不认为有区别。我认为这是投影中的东西,但这只是一个猜测。这东西的文档太少了。
-
我不确定它是否会有所帮助,但您能告诉我们什么语言/程序正在使用您的 COM 组件吗?
-
到目前为止,我只在 c++/cx 中尝试过。 GetFeed 方法预计为:
SyndicationFeed^ Downloader::GetFeed(String^ url),属性预计为SyndicationFeed^ Downloader::Feed
标签: c++ windows-runtime wrl