我的程序需要每隔一定时间枚举正在运行的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
这是什么原因呢?有办法解决吗?分不够再加
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
这是什么原因呢?有办法解决吗?分不够再加
在Service的Propertise里有一个Log On TAB,里面可以设置这个Service的启动用户。
你换成Administrator试试。
也就是说,服务的登录身份为“本地系统帐户(允许服务与桌面交互)”,因为我的程序有隐藏窗口,需要处理WINDOWS消息循环,所以只能这样。
Starting an Interactive Client Process in C++