客户端为啥没有调用CoFreeUnusedLibraries()
客户端为什么没有调用CoFreeUnusedLibraries()?
组件被卸载,客户端要调用CoFreeUnusedLibraries,但是我在网上看到这段程序并没有调用函数CoFreeUnusedLibraries(),这是为什么?
------解决方案--------------------
调用 CoLoadLibrary 这个才需要用CoFreeUnusedLibraries,一般创建组件不需要
------解决方案--------------------
当组件的使用计数为0的时候,就会释放资源的
------解决方案--------------------
最后调用CoUninitialize的时候就会释放了
组件被卸载,客户端要调用CoFreeUnusedLibraries,但是我在网上看到这段程序并没有调用函数CoFreeUnusedLibraries(),这是为什么?
#include <windows.h>
#include <objbase.h>
#include <stdio.h>
#include "../IExample/IExample.h"
int main(int argc, char **argv)
{
IExample *exampleObj;
IClassFactory *classFactory;
HRESULT hr;
// We must initialize OLE before we do anything with COM objects. NOTE:
// some COM components, such as the IE browser engine, require that you
// use OleInitialize() instead. But our COM component doesn't require this
if (!CoInitialize(0))
{
// Get IExample.DLL's IClassFactory
if ((hr = CoGetClassObject(&CLSID_IExample, CLSCTX_INPROC_SERVER, 0, &IID_IClassFactory, &classFactory)))
MessageBox(0, "Can't get IClassFactory", "CoGetClassObject error", MB_OK|MB_ICONEXCLAMATION);
else
{
// Create an IExample object
if ((hr = classFactory->lpVtbl->CreateInstance(classFactory, 0, &IID_IExample, &exampleObj)))
{
classFactory->lpVtbl->Release(classFactory);
MessageBox(0, "Can't create IExample object", "CreateInstance error", MB_OK|MB_ICONEXCLAMATION);
}
else
{
char buffer[80];
// Release the IClassFactory. We don't need it now that we have the one
// IExample we want
classFactory->lpVtbl->Release(classFactory);
// Call SetString to set the text "Hello world!"
exampleObj->lpVtbl->SetString(exampleObj, "Hello world!");
// Retrieve the string to make sure we get "Hello world!"
exampleObj->lpVtbl->GetString(exampleObj, buffer, sizeof(buffer));
printf("string = %s (should be Hello World!)\n", buffer);
// Release the IExample now that we're done with it
exampleObj->lpVtbl->Release(exampleObj);
}
}
// When finally done with OLE, free it
CoUninitialize();
}
else
MessageBox(0, "Can't initialize COM", "CoInitialize error", MB_OK|MB_ICONEXCLAMATION);
return(0);
}
------解决方案--------------------
调用 CoLoadLibrary 这个才需要用CoFreeUnusedLibraries,一般创建组件不需要
------解决方案--------------------
当组件的使用计数为0的时候,就会释放资源的
------解决方案--------------------
最后调用CoUninitialize的时候就会释放了