C++ 调用FindConnectionPoint时写入内存时发生访问冲突
我正在尝试订阅MBN活动。这是我的密码:C++ 调用FindConnectionPoint时写入内存时发生访问冲突,c++,winapi,com,mobile-broadband-api,connection-points,C++,Winapi,Com,Mobile Broadband Api,Connection Points,我正在尝试订阅MBN活动。这是我的密码: void subscribeToMbnEvents() { dwError = CoInitializeEx(NULL, COINIT_MULTITHREADED); SAFEARRAY* mbnInterfaces; CComPtr<IMbnInterfaceManager> intMgr = NULL; dwError = CoCreateInstance(CLSID_MbnInterfaceManag
void subscribeToMbnEvents()
{
dwError = CoInitializeEx(NULL, COINIT_MULTITHREADED);
SAFEARRAY* mbnInterfaces;
CComPtr<IMbnInterfaceManager> intMgr = NULL;
dwError = CoCreateInstance(CLSID_MbnInterfaceManager, NULL, CLSCTX_ALL, IID_IMbnInterfaceManager, (void**)&intMgr);
if (dwError != ERROR_SUCCESS)
{
CoUninitialize();
std::cout << getTimeStamp() << " failed to initialize IMbnInterfaceManager \n";
}
dwError = intMgr->GetInterfaces(&mbnInterfaces);
if (dwError != ERROR_SUCCESS)
{
CoUninitialize();
std::cout << getTimeStamp() << " failed to get MBN Interfaces \n";
}
if (dwError == ERROR_SUCCESS)
{
LONG indexOfFirstMBNInterface;
dwError = SafeArrayGetLBound(mbnInterfaces, 1, &indexOfFirstMBNInterface);
if (dwError != ERROR_SUCCESS)
{
std::cout << getTimeStamp() << " failed to get first index of MBN Interface \n";
}
CComPtr<IMbnInterface> MbnInt = NULL;
dwError = SafeArrayGetElement(mbnInterfaces, &indexOfFirstMBNInterface, (void*)(&MbnInt));
if (dwError != ERROR_SUCCESS)
{
std::cout << getTimeStamp() << " failed to get MBN Interface \n";
}
IConnectionPointContainer* icpc;
dwError = intMgr->QueryInterface(IID_IMbnInterfaceManager, (void**)&icpc);
if (dwError != ERROR_SUCCESS)
{
std::cout << "Error querying interface" << std::endl;
}
IConnectionPoint *icp;
dwError = icpc->FindConnectionPoint(IID_IMbnInterfaceEvents, &icp);
if (dwError != ERROR_SUCCESS)
{
std::cout << "Error finding connection point" << std::endl;
}
}
}
void subscribeToMbnEvents()
{
dwError=coinitializex(NULL,COINIT_多线程);
安全阵列*MBN接口;
CComPtr intMgr=NULL;
dwError=CoCreateInstance(CLSID_MbnInterfaceManager、NULL、CLSCTX_ALL、IID_imbinterface manager、(void**)和intMgr);
if(dwError!=错误\u成功)
{
coninitialize();
std::cout检索IConnectionPointContainer
的代码错误:
IConnectionPointContainer* icpc;
dwError = intMgr->QueryInterface(IID_IMbnInterfaceManager, (void**)&icpc);
// ^^^^^^^^^^^^^^^^^^^^^^^^ wrong interface ID
if (dwError != ERROR_SUCCESS)
{
std::cout << "Error querying interface" << std::endl;
}
使用宏更容易、更安全。它推断出与指针类型匹配的接口ID:
HRESULT hr = intMgr->QueryInterface(IID_PPV_ARGS(&icpc));
1它不是完全随机的。FindConnectionPoint
是IConnectionPointContainer
接口中的第二个条目,即v表中的第五个条目(考虑3个IUnknown
方法)。该方法占用了imbinterfacemanager
中的同一个位置。它的第一个参数是[out]参数,从而解释写入时的访问冲突。“一切正常(除了queryinterface调用”-如果QueryInterface
调用失败,它将返回一个NULL
指针。现在,当调用FindConnectionPoint
@IInspectable时,您正在解除对icpc
指针的引用。很抱歉,如果我的措辞有点错误,QueryInterface
似乎成功了(dwError等于调用后的ERROR\u SUCCESS)我还检查了nullptr,以确保它不是nullptr。你为什么不回答你的问题,这样它就不会再使用了?顺便说一句,你的错误处理是错误的。它与一个常量进行比较,而不是HRESULT
(错误\u成功
),而且无论如何也不应该进行平等性比较。请参阅入门资料。@i不可检测的对不起?我知道你是想帮忙,但如果你能阅读整个passus:“除了queryinterface调用之外,我在其他项目中也使用了这一精确的代码,工作正常”你会注意到这更多的是你的理解错误,而不是我“撒谎”那句话没有任何意义。我在“除了查询接口调用”之后停止了阅读。你住的地方不使用标点符号吗?
HRESULT hr = intMgr->QueryInterface(IID_PPV_ARGS(&icpc));