In-proc 线程模型
Rate 2007-11-29 05:42:40
COMDll2 中有 2 个类
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_ApartmObj3, CApartmObj3)
OBJECT_ENTRY(CLSID_FeeObj, CFeeObj)
END_OBJECT_MAP()
CApartmObj3 线程模型: Apartment
CFeeObj 线程模型: Free
我打印如下一些线程ID:
CApartmObj3()
{
char Data[64];
memset(Data,0,sizeof(Data));
sprintf(Data,"CApartmObj3\tthread:0x%0x\tthis:0x%x\t",::GetCurrentThreadId(),(void*)this);
std::string strLog = Data;
strLog += "CApartmObj3()";
g_LogFile.WriteLog(strLog);
}
STDMETHODIMP CApartmObj3::QueryInfo2(BSTR *pName)
{
// TODO: Add your implementation code here
char Data[64];
memset(Data,0,sizeof(Data));
sprintf(Data,"CApartmObj3\tthread:0x%0x\tthis:0x%x\t",::GetCurrentThreadId(),(void*)this);
std::string strLog = Data;
strLog += "QueryInfo2()";
g_LogFile.WriteLog(strLog);
*pName = _bstr_t("CApartmObj3::QueryInfo2 test........").copy();
return S_OK;
}
CFeeObj()
{
char Data[64];
memset(Data,0,sizeof(Data));
sprintf(Data,"CFeeObj\tthread:0x%0x\tthis:0x%x\t",::GetCurrentThreadId(),(void*)this);
std::string strLog = Data;
strLog += "CFeeObj()";
g_LogFile.WriteLog(strLog);
}
STDMETHODIMP CFeeObj::QueryInfo2(BSTR *pName)
{
// TODO: Add your implementation code here
char Data[64];
memset(Data,0,sizeof(Data));
sprintf(Data,"CFeeObj\tthread:0x%0x\tthis:0x%x\t",::GetCurrentThreadId(),(void*)this);
std::string strLog = Data;
strLog += "QueryInfo2()";
g_LogFile.WriteLog(strLog);
*pName = _bstr_t("CFeeObj::QueryInfo2 test........").copy();
return S_OK;
}
我在测试中调用:
void CCOMTestDlg::OnButton1()
{
CoInitialize(NULL);
CLSID clsid;
HRESULT hr;
CLSIDFromProgID(OLESTR("COMDll2.ApartmObj3"), &clsid);
hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, __uuidof(IUnknown), reinterpret_cast<void**>(&pIUnknown2));
IApartmObj3 *pApartmObj3;
pIUnknown2->QueryInterface(__uuidof(IApartmObj3),(void**)&pApartmObj3);
pIUnknown2->Release();
BSTR Name2;
pApartmObj3->QueryInfo2(&Name2);
_bstr_t bstrName22(Name2,false);
MessageBox(bstrName22.operator char*());
//
IUnknown* pIUnknown3;
CLSIDFromProgID(OLESTR("COMDll2.FeeObj"), &clsid);
hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, __uuidof(IUnknown), reinterpret_cast<void**>(&pIUnknown3));
IFeeObj *pFeeObj;
pIUnknown3->QueryInterface(__uuidof(IFeeObj),(void**)&pFeeObj);
pIUnknown3->Release();
BSTR Name23;
pFeeObj->QueryInfo2(&Name23);
_bstr_t bstrName23(Name23,false);
MessageBox(bstrName23.operator char*());
pApartmObj3->Release();
pFeeObj->Release();
CoUninitialize();
}
最后日志如下:
[2007-11-29 17:03:36] main thread:0x814
[2007-11-29 17:03:36] CApartmObj3 thread:0x814 this:0x18b1940 CApartmObj3()
[2007-11-29 17:03:36] CApartmObj3 thread:0x814 this:0x18b1940 QueryInfo2()
[2007-11-29 17:03:36] CFeeObj thread:0x218 this:0x18b1750 CFeeObj()
[2007-11-29 17:03:36] CFeeObj thread:0x7c0 this:0x18b1750 QueryInfo2()
//
[2007-11-29 17:41:40] main thread:0x610
[2007-11-29 17:41:40] CApartmObj3 thread:0x610 this:0x18b1940 CApartmObj3()
[2007-11-29 17:41:40] CApartmObj3 thread:0x610 this:0x18b1940 QueryInfo2()
[2007-11-29 17:41:40] CFeeObj thread:0x818 this:0x18b1750 CFeeObj()
[2007-11-29 17:41:40] CFeeObj thread:0x850 this:0x18b1750 QueryInfo2()
main thread : 是 DllMain-> DLL_PROCESS_ATTACH 记录下来的
发现 CFeeObj 在 0x218 线程中创建, QueryInfo2() 在 0x7c0 被调用,这是为什么?