C++ casting Windows IAction thorws an exception at dynamic_Cast
04:26 25 Sep 2018

I'm working on retrieving some info from Windows Task Scheduler. MSDN indicates there are several types of action. I want to deal with them separately. I tried:

IAction* pAction = NULL;
pActionCollection->get_Item(_variant_t(i), &pAction);
if (IExecAction* pExecAction = dynamic_cast(pAction)) { /*my work...*/ }
if (IComHandlerAction* pComHandlerAction = dynamic_cast(pAction)) { /*my work...*/ }
if (IEmailAction* pEmailAction = dynamic_cast(pAction)) { /*my work...*/ }
if (IShowMessageAction* pShowMessageAction = dynamic_cast(pAction)) { /*my work...*/ }

But this program throws exception at the first dynamic_cast.

Exception thrown at 0x00007FFB516365A5 (vcruntime140d.dll) in myProgram.exe: 0xC0000005: Access violation reading location 0x00000130BAFEDB04.

The definition in taskschd.h shows IExecAction is a derived class from IAction.

This works well:

if (IExecAction* pExecAction = ((IExecAction*)pAction)) { /*my work...*/ }

But what if I want to do some type checking? How could I use it properly?

c++ windows casting windows-task-scheduler