Re: Stack around the variable 'v' was corrupted
I don't think you are using pEnum->Next properly. The first parameter in
Next specifies the number of items to return, not an index into the item to
return.
So, the first time through, you are returning 1 item, the next time, two
items, then next time after that, three items, etc...
In all of those cases though, you are only passing in a pointer to 1
variant, not a pointer to an array of Variants.
So, you should NEVER pass a value greater than 1 as the first parameter of
Next.
I think the logic is supposed to be something like this:
CComVariant v; // Note, this isn't an array of variants, just 1 variant,
so we pass "1" into pEnum->Next
while (pEnum->Next(1, &v, &fetched) == S_OK)
{
.... Do stuff, one at a time.
}
Leo.
"Jeffrey Walton" <noloader@gmail.com> wrote in message
news:b83b628a-eb54-4ec0-b3e8-0418b5feed3e@13g2000yql.googlegroups.com...
Hi All,
I'm enumerating commands in Visual Studio in an attempt to determine
if my command is already installed (otherwise I call AddNamedCommand).
Obviously, I'm smashing the stack. Unfortunately, I don't see where/
why.
I use ATLASSERT like mad when debugging, so none of the pointers below
are bad. I also check _all_ return values, so I don't believe its a
matter of carelessness. There is *one* cast I do not like, but I don't
how to get rid of it.
The stack is smashed at the top of the loop, on the third iteration
(or at least that is where VS complains). m_pDevEnv is the DTE2 passed
in OnConnect.
Thanks in advance,
Jeff
CComPtr<Commands> pCommands;
CComPtr<IEnumVARIANT> pEnum;
hr = m_pDevEnv->get_Commands( &pCommands );
ATLASSERT( SUCCEEDED(hr) );
if( FAILED(hr) ) {...}
LONG count = 0;
pCommands->get_Count( &count );
ATLASSERT( SUCCEEDED(hr) );
if( FAILED(hr) ) {...}
hr = pCommands->_NewEnum( (IUnknown **)&pEnum );
ATLASSERT( SUCCEEDED(hr) );
if( FAILED(hr) ) {...}
ULONG fetched;
for( INT i = 1; i <= count; i++ )
{
CComVariant v;
hr = pEnum->Next( i, &v, &fetched );
ATLASSERT( SUCCEEDED(hr) );
if( S_FALSE == hr ) { break; }
if( FAILED(hr) ) { ... }
ATLASSERT( v.vt == VT_DISPATCH );
CComPtr<IDispatch> pDispatch = v.pdispVal;
ATLASSERT( NULL != pDispatch );
CComQIPtr<Command> pCommand = pDispatch;
ATLASSERT( NULL != pCommand );
}