Cook Computing

I got this error recently

February 2, 2002 Written by Charles Cook

I got this error recently when experimenting with .NET/COM interop:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

Never having seen this before when making COM calls it fooled me into thinking it was some .NET interop related problem. The actual problem was passing the wrong interface IID in a QI call - _HelloWorld instead of IHelloWorld in this code:


CComPtr<IHelloWorld> spHelloWorld;
hr = varDisp.pdispVal->QueryInterface(__uuidof(_HelloWorld), (void**)&spHelloWorld);

I've made this mistake before but only remember getting access violation type errors. Anyway this unsafe QI call is unnecessary these days. The IUnknown struct definition in unknwn.h provides this safe templatized call:


template<class Q>
HRESULT STDMETHODCALLTYPE QueryInterface(Q** pp)
{
  return QueryInterface(__uuidof(Q), (void **)pp);
}

... so the QI in the code above should have been:


hr = varDisp.pdispVal->QueryInterface(&spHelloWorld);