调用IInternetSecurityManager::GetZoneMappings总是不成功?

timelight2000 2018-08-23 10:58:30
调用IInternetSecurityManager::GetZoneMappings ,HRESULT为S_OK,但获得的接口总是为空,明明用C#绕着圈的调用都成功了。
调用SetZoneMapping是成功的,所以问题八成还是在这个IEnumString接口上。
新手,看不懂MSDN上没有例子的说明。
到底该怎么调用呢?

#include "pch.h"
#include <iostream>
#include <urlmon.h>
#include <tchar.h>
#include "cmdline.h"
#include <vector>
#include "atlstr.h"
using namespace std;

int main(int argc, char *argv[])
{
::CoInitialize(NULL);
IEnumString ** enumstring = NULL;
HRESULT hResult = S_OK;
IInternetSecurityManager *pSecurityManager = NULL;
hResult = CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER, IID_IInternetSecurityManager,
(void **)&pSecurityManager);
if (SUCCEEDED(hResult))
{
hResult = pSecurityManager->GetZoneMappings(URLZONE_INTRANET, enumstring, 0);
ULONG pceltFetched = 0;
LPOLESTR *site = NULL;
while ((*enumstring)->Next(1, site, &pceltFetched) != 0)
{
USES_CONVERSION;
LPCSTR szText = OLE2CA(*site);
cout << szText << endl;
pSecurityManager->Release();
}
}
::CoUninitialize();
}
...全文
137 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgl7903 2018-08-23
  • 打赏
  • 举报
回复
IEnumString * enumstring = NULL;
hResult = pSecurityManager->GetZoneMappings(URLZONE_INTRANET, &enumstring, 0);
timelight2000 2018-08-23
  • 打赏
  • 举报
回复
终于调试通过了。刚学着用VC,写的代码还真是漏洞百出呢。
不但是字符串数组初始化错了,判断条件也不对,还提前释放了使用的组件,最后还把宽字符串当字符串输出了。
最终代码:

#include "pch.h"
#include <iostream>
#include <urlmon.h>
#include <tchar.h>
#include "cmdline.h"
#include <vector>
#include "atlstr.h"
using namespace std;
int main(int argc, char *argv[])
{
::CoInitialize(NULL);
IEnumString * enumstring = NULL;
HRESULT hResult = S_OK;
IInternetSecurityManager *pSecurityManager = NULL;
hResult = CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER, IID_IInternetSecurityManager,
(void **)&pSecurityManager);
if (SUCCEEDED(hResult))
{
hResult = pSecurityManager->GetZoneMappings(URLZONE_INTRANET, &enumstring, 0);
ULONG celt=1;
ULONG pceltFetched = 0;
LPOLESTR *site = new wchar_t *[1];
while (hResult==S_OK)
{
hResult = enumstring->Next(celt,site, &pceltFetched);
if(hResult != S_OK)
break;
wcout << site[0] << endl;
}
pSecurityManager->Release();
}
::CoUninitialize();
}
timelight2000 2018-08-23
  • 打赏
  • 举报
回复
我检查了一下(*enumstring)->Next(1, site, &pceltFetched)的返回值,发现报 E_INVALIDARG One or more arguments are invalid.
所以,问题在于如何正确调用IEnumString接口,获得返回的字符串数组。
timelight2000 2018-08-23
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903 的回复:
IEnumString * enumstring = NULL;
hResult = pSecurityManager->GetZoneMappings(URLZONE_INTRANET, &enumstring, 0);

我开始就是按这种方式调用的,与现在我用的方式没有区别。
关键是返回的这个enumstring怎么处理得到字符串数组啊。

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧