描述:
我的程序需要每隔一定时间枚举正在运行的IE实例,于是在WM_TIMER中执行如下代码:
CoInitialize(NULL);
SHDocVw::IShellWindowsPtr m_spSHWinds;
HRESULT ret=m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));
if(m_spSHWinds==NULL)
{
if(debug.Open(L"c:\\debug.txt",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate))
{
CString errCode;
errCode.Format(L"CoCreateInstance failed (%X)\n",ret);
debug.SeekToEnd();
debug.WriteString(errCode);
debug.Close();
}
}
该代码运行正常,但是后来我把程序改为了Win32服务程序(NT Service),于是CreateInstance每次都失败,c:\\debug.txt中的信息如下:
CoCreateInstance failed (80040154)
CoCreateInstance failed (8007000E)
CoCreateInstance failed (8007000E)
CoCreateInstance failed (8007000E)
...
在winerror.h中查到 80040154 对应REGDB_E_CLASSNOTREG,意思是Class not registered,而8007000E对应E_OUTOFMEMORY,意思是Ran out of memory
这是什么原因呢?有办法解决吗?分不够再加
解决方案1:
IShellWindowsPtr需要一个shell。在服务里面Shell是没有的,因为服务自己没有桌面。你可以在用户启动时运行一个程序来和服务交互。
解决方案2: 读MSDN中的文章
Starting an Interactive Client Process in C++