一个简单的createdispatch的问题,请帮忙!
COleException e
------解决方案--------------------void CTestView::OnBatterylife()
{
COleDispatchDriver disp;
COleException *e = new COleException;
try {
// Create instance of Microsoft System Information Control
// by using ProgID.
if (disp.CreateDispatch("SYSINFO.Sysinfo.1", e))
{
//Call BatteryLifePercent.
short nBatteryLifePercent;
disp.InvokeHelper(0x03, DISPATCH_PROPERTYGET, VT_I2,
(void*)&nBatteryLifePercent, NULL);
if (nBatteryLifePercent == 255)
AfxMessageBox("Battery Life % unknown");
else
{
CString cStr;
cStr.Format("Battery Life is at %d%%",
nBatteryLifePercent);
AfxMessageBox(cStr);
}
}
else
throw e;
}
//Catch control-specific exceptions.
catch (COleDispatchException * e)
{
CString cStr;
if (!e->m_strSource.IsEmpty())
cStr = e->m_strSource + " - ";
if (!e->m_strDescription.IsEmpty())
cStr += e->m_strDescription;
else
cStr += "unknown error";
AfxMessageBox(cStr, MB_OK,
(e->m_strHelpFile.IsEmpty())? 0:e->m_dwHelpContext);
e->Delete();
}
//Catch all MFC exceptions, including COleExceptions.
// OS exceptions will not be caught.
catch (CException *e)
{
TRACE("%s(%d): OLE Execption caught: SCODE = %x",