1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include <comutil.h> // for _bstr_t
#include <iostream>
#include <objbase.h> // Necessary for COM
#include <stdio.h>
#include <tchar.h>
#include "FormattedOutput.h"
#include "Menu.h"
#include "VideohubAPI_h.h"
class MyVideohubCallback : public IVideohubCallback
{
int m_refCount;
IVideohubState * m_state;
IVideohub * m_hub;
public:
MyVideohubCallback(IVideohub *hub) : m_hub(hub), m_refCount(0), m_state(NULL) {}
~MyVideohubCallback()
{
if (m_state!=NULL)
m_state->Release();
}
HRESULT STDMETHODCALLTYPE StateChanged()
{
if (m_state == NULL)
{
// it is the first we are called, get the IvideohubAttributes interface
// and print some details about the Videohub device
printDeviceInfo();
// we havent got an IVideohubState yet, get one
if(m_hub->GetState(&m_state)==S_OK)
{
dumpState(m_state);
}
}
else
{
// we already have an IVideohubState, update it
// and get an iterator over the changes
IVideohubStateChangeIterator *iter;
if (m_state->UpdateState(&iter)==S_OK)
{
dumpChanges(m_state, iter);
iter->Release();
}
}
// re-print the user menu
printMenu();
return S_OK;
}
HRESULT STDMETHODCALLTYPE DeviceDisconnected()
{
printf_s("******\tVideohub DISCONNECTED\t******\n");
return S_OK;
}
HRESULT STDMETHODCALLTYPE DeviceReconnected()
{
printf_s("******\tVideohub RECONNECTED\t******\n");
return S_OK;
}
HRESULT STDMETHODCALLTYPE NewDeviceConnected()
{
return S_OK;
}
// IUnknown interface methods
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv)
{
HRESULT result = E_NOINTERFACE;
*ppv = NULL;
if (iid == IID_IUnknown)
{
*ppv = this;
AddRef();
result = S_OK;
}
else if (iid == IID_IVideohubCallback)
{
*ppv = (IVideohubCallback*)this;
AddRef();
result = S_OK;
}
return result;
}
ULONG STDMETHODCALLTYPE AddRef(void)
{
m_hub->AddRef();
return InterlockedIncrement((LONG*)&m_refCount);
}
ULONG STDMETHODCALLTYPE Release(void)
{
long newRefValue;
m_hub->Release();
newRefValue = InterlockedDecrement((LONG*)&m_refCount);
if (newRefValue == 0)
{
delete this;
}
return newRefValue;
}
void printDeviceInfo()
{
IVideohubAttributes *attributes = NULL;
if (m_hub->QueryInterface(IID_IVideohubAttributes, (void **) &attributes)==S_OK)
{
// print some info about the device
dumpVideohubInfo(attributes);
// release the interface
attributes->Release();
}
else
{
printf("Error obtaining the Attributes interface\n");
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT result;
IVideohubDiscovery *videoHubDiscovery = NULL;
IVideohub *videohub;
MyVideohubCallback *cb;
// check the arguments
if (argc!=2)
{
printf_s("usage: DeviceState <videohub device address>");
return 1;
}
// initialise the device address
_bstr_t deviceAddress(argv[1]);
// Initialize COM on this thread
result = CoInitialize(NULL);
if (FAILED(result))
{
printf_s("Initialization of COM failed - result = %08x.\n", result);
return 1;
}
// Create an IVideohubDiscovery object
result = CoCreateInstance(CLSID_CVideohubDiscovery, NULL, CLSCTX_ALL, IID_IVideohubDiscovery, (void**)&videoHubDiscovery);
if (FAILED(result))
{
printf_s("A Videohub Discovery object could not be created.\n");
goto bail;
}
// get an IVideohub object for our device address
if(videoHubDiscovery->ConnectTo(deviceAddress, 10000, &videohub) == S_OK)
{
// instantiate an IVideohubCallback object to receive state change notifications
cb = new MyVideohubCallback(videohub);
cb->AddRef();
// register the callback object
videohub->AddVideohubCallback(cb);
// wait for user input
// this function returns when the user selects the Quit option
waitForUserInput(videohub);
// remove the callback object
videohub->RemoveVideohubCallback(cb);
// release the callback object
cb->Release();
// release the IVideohub object
videohub->Release();
}
else
{
printf_s("An IVideohub object could not be created for the given address\n");
}
// release the VideohubDiscovery object
videoHubDiscovery->Release();
bail:
// Uninitalize COM on this thread
CoUninitialize();
return 0;
}
|