// StopwatchClient.cpp : // Defines the entry point for the console application. // // std::out etc #include #include "timers_i.c" #include "Stopwatch.h" #include "StopwatchClassFactory.h" // NOTE: The following line will vary depending on // where the Timers.dll is located. #define TIMERSDLL "C:\\CIS630\\Chapter2\\Part3\\Timers\\Debug\\Timers.dll" typedef HRESULT (__stdcall *DLLGETCLASSOBJECT)(REFCLSID rclsid, REFIID riid, LPVOID* ppv); typedef HRESULT (__stdcall *DLLCANUNLOADNOW)(void); // A handle to the Timers DLL HINSTANCE hinstDll; // Instantiates a stopwatch object and returns it by reference HRESULT CreateInstance(REFCLSID rclsid, REFIID riid, void** ppv) { HRESULT hr = E_FAIL; DLLGETCLASSOBJECT DllGetClassObject; IClassFactory* pClassFactory; hinstDll = LoadLibrary(TIMERSDLL); if (hinstDll == NULL) std::cout << "Unable to load \"" TIMERSDLL "\"" << std::endl; else { DllGetClassObject = (DLLGETCLASSOBJECT) GetProcAddress(hinstDll, "DllGetClassObject"); if (DllGetClassObject != NULL) { // Request the Stopwatch Class Factory hr = DllGetClassObject(rclsid, IID_IClassFactory, (void**) &pClassFactory); if(SUCCEEDED(hr)) { hr = pClassFactory->CreateInstance( NULL, riid, ppv ); pClassFactory->Release(); } pClassFactory = NULL; } } return hr; } void UnloadDLL() { DLLCANUNLOADNOW DllCanUnloadNow; DllCanUnloadNow = (DLLCANUNLOADNOW) GetProcAddress(hinstDll, "DllCanUnloadNow"); if (DllCanUnloadNow != NULL) { // Request the Stopwatch Class Factory if ( (DllCanUnloadNow()) == S_OK ) FreeLibrary(hinstDll); } } void UseStopwatch(IStopwatch* const pStopwatch) { float nElapsedTime; pStopwatch->Start(); pStopwatch->ElapsedTime( &nElapsedTime ); std::cout << "The overhead time is " << nElapsedTime << std::endl; } int main(int argc, char* argv[]) { HRESULT hr; IUnknown* pIUnknown = NULL; IUnknown* pIUnknown2 = NULL; IStopwatch* pStopwatch = NULL; hr = CreateInstance( CLSID_Stopwatch, IID_IUnknown, (void**) &pIUnknown); if ( !SUCCEEDED(hr) ) std::cout << "ERROR: Unable to create Stopwatch!!"; else { pIUnknown2 = pIUnknown; // AddRef() not called because lifetime of pIUnknown wraps // pIUnknown2... hr = pIUnknown2->QueryInterface( IID_IStopwatch, (void**) &pStopwatch); if ( !SUCCEEDED(hr) ) std::cout << "ERROR: Unable to retrieve Stopwatch interface!!"; else { UseStopwatch(pStopwatch); pStopwatch->Release(); pStopwatch = NULL; } pIUnknown->Release(); } UnloadDLL(); return 0; }