描述:
我自己写一个dll程序,然而我的这个dll又要用到其他的dll(比如说msado15.dll)
在我的dll中要不要调用CoInitialize(NULL)函数。
解决方案1:
CoInitialize并不装载com库,这个函数只是用来初始化当前线程使用什么样的套间。当使用这个函数以后,线程就和一个套间建立了对应关系。
线程的套间模式决定了该线程如何调用com对象,是否需要列集等
你可以看一下有关列集的资料,使用不同套间之间对象接口是通过列集来完成的。关于列集的实现,很多书上都有较详细的说明。
CoInitialize()并不会干扰客户和服务之间的通信,套所作的事情只是让线程注册一个套间,而线程运行过程中就必然在此套间中,就象我们每个活着的人,都一定属于某个国家一样。
需要强调的是,套间是com中用来解决并发调用冲突的很有效的办法
需要,每一个动态库都需要调用这个函数。你可以看看你在新建动态库得时候VC自动为你创建得DLLMain函数。
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("YPGISPRINTDLL.DLL Initializing!\n");
CoInitialize(NULL);
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(YpgisPrintDLLDLL, hInstance))
return 0;
// Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
// an MFC Regular DLL (such as an ActiveX Control)
// instead of an MFC application, then you will want to
// remove this line from DllMain and put it in a separate
// function exported from this Extension DLL. The Regular DLL
// that uses this Extension DLL should then explicitly call that
// function to initialize this Extension DLL. Otherwise,
// the CDynLinkLibrary object will not be attached to the
// Regular DLL's resource chain, and serious problems will
// result.
new CDynLinkLibrary(YpgisPrintDLLDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("YPGISPRINTDLL.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(YpgisPrintDLLDLL);
CoUninitialize();
}
return 1; // ok
}