I want to write some unit tests for my COM object using googletest. Unfortunately I get an exception when CreateInstance is called: 0xC0000005: Access violation reading location 0x00000000.
The code basically looks like this:
MyClass.h
class ATL_NO_VTABLE CMyClass
: public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CMyClass, &CLSID_MyClass>,
public IConnectionPointImpl<CMyClass, &IID_IMyClassListener>,
public IConnectionPointContainerImpl<CMyClass>,
public IDispatchImpl<MyComClass, &IID_MyComClass, &LIBID_MyLib, 1, 0>
{
...
}
Test.cpp
#include "stdafx.h"
#include "gtest/gtest.h"
#include "MyClass.h"
TEST(MyClassTest, IsCreated)
{
HRESULT hr(E_FAIL);
CComPtr<MyComClass> lMyObject;
hr = CMyClass::CreateInstance(&lMyObject);
EXPECT_EQ(S_OK, hr);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I am able to call CreateInstance without any problems in some existing (non-test) projects. I don’t really understand COM/ATL so I’m having trouble figuring out why I’m getting this weird exception.
@Roman had the right idea. I put the following above main() in my Test.cpp and everything started working: