经验交流,从网上下载的好东西望大家分享
源代码在线查看: component.cpp
// component.cpp
#include
#include "component\component.h" // Generated by MIDL
#include "registry.h" // Add This!!!
// {10000002-0000-0000-0000-000000000001}
const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
long g_cComponents = 0;
long g_cServerLocks = 0;
interface INoAggregationUnknown
{
virtual HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv)=0;
virtual ULONG __stdcall AddRef_NoAggregation()=0;
virtual ULONG __stdcall Release_NoAggregation()=0;
};
class CInsideCOM : public ISum, public INoAggregationUnknown
{
public:
// IUnknown
ULONG __stdcall AddRef();
ULONG __stdcall Release();
HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
// INoAggregationUnknown
ULONG __stdcall AddRef_NoAggregation();
ULONG __stdcall Release_NoAggregation();
HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv);
// ISum
HRESULT __stdcall Sum(int x, int y, int* retval);
CInsideCOM(IUnknown* pUnknownOuter);
~CInsideCOM() { cout
private:
ULONG m_cRef;
IUnknown* m_pUnknownOuter;
};
CInsideCOM::CInsideCOM(IUnknown* pUnknownOuter) : m_cRef(1)
{
g_cComponents++;
if(pUnknownOuter != NULL)
m_pUnknownOuter = pUnknownOuter;
else
m_pUnknownOuter = (IUnknown*)(INoAggregationUnknown*)this;
}
HRESULT CInsideCOM::QueryInterface_NoAggregation(REFIID riid, void** ppv)
{
if(riid == IID_IUnknown)
{
cout *ppv = (INoAggregationUnknown*)this;
}
else if(riid == IID_ISum)
{
cout *ppv = (ISum*)this;
}
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
((IUnknown*)(*ppv))->AddRef();
return S_OK;
}
ULONG CInsideCOM::AddRef_NoAggregation()
{
return ++m_cRef;
}
ULONG CInsideCOM::Release_NoAggregation()
{
if(--m_cRef != 0)
return m_cRef;
delete this;
return 0;
}
ULONG CInsideCOM::AddRef()
{
return m_pUnknownOuter->AddRef();
}
ULONG CInsideCOM::Release()
{
return m_pUnknownOuter->Release();
}
HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
{
return m_pUnknownOuter->QueryInterface(riid, ppv);
}
HRESULT CInsideCOM::Sum(int x, int y, int* retval)
{
cout *retval = x + y;
return S_OK;
}
class CFactory : public IClassFactory
{
public:
// IUnknown
ULONG __stdcall AddRef();
ULONG __stdcall Release();
HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
// IClassFactory
HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
HRESULT __stdcall LockServer(BOOL bLock);
CFactory() : m_cRef(1) { }
~CFactory() { }
private:
ULONG m_cRef;
};
ULONG CFactory::AddRef()
{
cout return ++m_cRef;
}
ULONG CFactory::Release()
{
cout if(--m_cRef != 0)
return m_cRef;
delete this;
return 0;
}
HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
{
if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
{
cout *ppv = (IClassFactory *)this;
}
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
{
if(pUnknownOuter != NULL && riid != IID_IUnknown)
return CLASS_E_NOAGGREGATION;
CInsideCOM *pInsideCOM = new CInsideCOM(pUnknownOuter);
if(pInsideCOM == NULL)
return E_OUTOFMEMORY;
HRESULT hr = pInsideCOM->QueryInterface_NoAggregation(riid, ppv);
pInsideCOM->Release_NoAggregation();
return hr;
}
HRESULT CFactory::LockServer(BOOL bLock)
{
if(bLock)
g_cServerLocks++;
else
g_cServerLocks--;
return S_OK;
}
HRESULT __stdcall DllCanUnloadNow()
{
cout if(g_cServerLocks == 0 && g_cComponents == 0)
return S_OK;
else
return S_FALSE;
}
HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
{
cout
if(clsid != CLSID_InsideCOM)
return CLASS_E_CLASSNOTAVAILABLE;
CFactory* pFactory = new CFactory;
if(pFactory == NULL)
return E_OUTOFMEMORY;
// QueryInterface probably for IClassFactory
HRESULT hr = pFactory->QueryInterface(iid, ppv);
pFactory->Release();
return hr;
}
HRESULT __stdcall DllRegisterServer()
{
return RegisterServer("component.dll", CLSID_InsideCOM, "Inside COM Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
}
HRESULT __stdcall DllUnregisterServer()
{
return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
}
|
相关资源 |
|
-
经验交流,从网上下载的好东西望大家分享
-
51通用延时子程序,我从网上看到的,的确不错,和大家分享一下.
-
也是我从网上找到的源码
供大家分享 希望有所新发现
-
arm编译器中文手册
极其罕见的好东西
与大家分享
-
51单片机 电压表 课程设计赶快下载啊 好东西跟大家分享
-
这是我从网上下载的代码
-
学习jsp的资料,适合初学者,从网上下载的,觉得好,所以
-
一个使用回填翻译控制流语句的JAVA演示例子,是本人从网上下载的,非常好,值得学习编译原理的朋友参考学习.
|