COM- .
, , EXE COM DLL, EXE DLL. , , , .
interface ILoadedObject
{
HRESULT GiveObject(IUnknown *pObj);
};
DLL , EXE , , EXE .
IUnknown: , Release , QueryInterface IUnknown .
, EXE , ; COM EXE, Windows. OLE; .
, , EXE , . EXE :
interface IComponent;
interface IEnvironment : IUnknown
{
HRESULT CreateInstance(REFCLSID clsid, IComponent **ppNew);
}
:
interface IComponent : IUnknown
{
HRESULT SetEnvironment(IEnvironment *pEnv);
}
, , EXE , CreateInstance :
HRESULT Env::CreateInstance(REFCLSID clsid, IComponent **ppNew)
{
HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
__uuidof(IComponent), (void **)&ppNew);
if (FAILED(hr))
return hr;
(*ppNew)->SetEnvironment(this);
return S_OK;
}
, , "" . ( ) . ( ), EXE :
, :
HRESULT Comp::SetEnvironment(IEnvironment *e)
{
m_env = e;
return S_OK;
}
ComPtr<IComponent> button;
m_env->CreateInstance(CLSID_Button, &button);
Therefore, whenever a component is created, the environment (defined in the EXE) can precisely control how the implementation of the component is implemented. Each creation takes place through an exe.
This is sometimes called "dependency injection" or "control inversion."