HRESULT hr = E_FAIL; //COM 组件初始化
CoInitializeEx(NULL,COINIT_MULTITHREADED); //显示调用LoadLibrary,为了加载注册DLL,一般这个工作是由SETUP.exe做的。 typedef HRESULT (_stdcall * PFREG)();
HINSTANCE hInst = LoadLibrary("Test.dll"); //调用DLL中的DllRegisterServer(),将DLL注册进相应的注册表文件;
PFREG pRegister =(PFREG)::GetProcAddress(hInst,("DllRegisterServer"));
hr=(*pRegister)(); //获得相应的COM对象工厂的接口(IClassFactory) //This function(CoGetClassObject) provides a pointer to an interface on a class //object associated with a specified class identifier. Call CoGetClassObject directly //when you want to create multiple objects through a class object whose class //identifier is already in the system registry. Most class objects implement the //IClassFactory interface. You would then call IClassFactory::CreateInstance to //create an uninitialized object.
IClassFactory * pIFactory =NULL;
hr = CoGetClassObject(CLSID_TEST,
CLSCTX_INPROC_SERVER, NULL,
IID_IClassFactory, (void**)&pIFactory); //Find in 注册表==>LoadLibrary(加载DLL资源)==>(调用DLL的函数)DllGetClassObject,注意它不会调用函数DllRegisterServer //进行DLL的注册 //注意通常的情况是COM对象都会继承IUnknown,也可能会继承其他的接口(ILook...) //通过工厂获得COM对象通用接口(IUnknown)
IUnknown * pIUnknown=NULL;
hr = pIFactory->CreateInstance(NULL,IID_IUnknown,(void**)&pIUnknown);
hr = pIFactory->Release(); //通过工厂获得COM对象一个接口(ILook)
ILook * pILook =NULL; //Returns a pointer to a specified interface on an object to which a client //currently holds an interface pointer.
hr =->QueryInterface(IID_IWindowx,(void**)&pILook); //调用COM对象接口方法
HWND hWnd = pILook->CreateWindowx(NULL,0,0);
CoUninitialize();