// StopwatchClient.cpp : // Defines the entry point for the console application. // // std::out etc #include #include "Timers_i.c" #include "Stopwatch.h" // NOTE: The following line will vary depending on // where the Timers.dll is located. #define TIMERSDLL "C:\\CIS630\\Chapter2\\Part2\\Timers\\Debug\\Timers.dll" typedef HRESULT (__stdcall *DLLGETCLASSOBJECT)(REFCLSID rclsid, REFIID riid, LPVOID* ppv); // Instantiates a stopwatch object and returns it by reference HRESULT CreateInstance(REFCLSID rclsid, REFIID riid, void** ppv) { HRESULT hr = E_FAIL; HINSTANCE hinstDll; DLLGETCLASSOBJECT DllGetClassObject; hinstDll = LoadLibrary(TIMERSDLL); if (hinstDll == NULL) std::cout << "Unable to load \"" TIMERSDLL "\"" << std::endl; else { DllGetClassObject = (DLLGETCLASSOBJECT) GetProcAddress(hinstDll, "DllGetClassObject"); if (DllGetClassObject != NULL) hr = DllGetClassObject(rclsid, riid, ppv); } return hr; } 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; // 3rd parameter of call to CreateInstance() must be passed by // reference and also cast 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... // 2nd parameter of QueryInterface() must be passed by reference // and cast 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(); } return 0; }