Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.3 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include "msnotify.h"
  4. #include "webcheck.h"
  5. GUID CLSID_ConnectionAgent = { 0xE6CC6978,0x6B6E,0x11D0,0xBE,0xCA,0x00,0xC0,0x4F,0xD9,0x40,0xBE };
  6. int iState = 1;
  7. class CDialTest : public INotificationSink
  8. {
  9. public:
  10. CDialTest();
  11. ~CDialTest();
  12. // IUnknown members
  13. STDMETHODIMP QueryInterface(REFIID, void **);
  14. STDMETHODIMP_(ULONG) AddRef(void);
  15. STDMETHODIMP_(ULONG) Release(void);
  16. // INotificationSink members
  17. STDMETHODIMP OnNotification(
  18. INotification *pNotification,
  19. INotificationReport *pNotificationReport,
  20. DWORD dwReserved);
  21. HRESULT Dial(void);
  22. HRESULT OnConnected(INotificationReport *);
  23. HRESULT OnDisconnected(void);
  24. HRESULT SendNotification(NOTIFICATIONTYPE type);
  25. long m_cRef;
  26. };
  27. ///////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////////////////
  29. //
  30. // IUnknown members
  31. //
  32. STDMETHODIMP CDialTest::QueryInterface(REFIID riid, void ** ppv)
  33. {
  34. *ppv=NULL;
  35. // Validate requested interface
  36. if ((IID_IUnknown == riid) ||
  37. (IID_INotificationSink == riid))
  38. {
  39. *ppv=(INotificationSink*)this;
  40. } else {
  41. return E_NOINTERFACE;
  42. }
  43. ((LPUNKNOWN)*ppv)->AddRef();
  44. return NOERROR;
  45. }
  46. STDMETHODIMP_(ULONG) CDialTest::AddRef(void)
  47. {
  48. return ++m_cRef;
  49. }
  50. STDMETHODIMP_(ULONG) CDialTest::Release(void)
  51. {
  52. if( 0L != --m_cRef )
  53. return m_cRef;
  54. delete this;
  55. return 0L;
  56. }
  57. ///////////////////////////////////////////////////////////////////////////
  58. ///////////////////////////////////////////////////////////////////////////
  59. //
  60. // INotificationSink members
  61. //
  62. STDMETHODIMP CDialTest::OnNotification(
  63. INotification *pNotification,
  64. INotificationReport *pNotReport,
  65. DWORD dwReserved)
  66. {
  67. // Depending on the notification type, we call our other
  68. // existing members.
  69. NOTIFICATIONTYPE nt;
  70. HRESULT hr;
  71. hr = pNotification->GetNotificationInfo(&nt, NULL,NULL,NULL,0);
  72. if (FAILED(hr)) {
  73. printf("OnNotification failed to get notification type\n");
  74. return E_INVALIDARG;
  75. }
  76. if (IsEqualGUID(nt, NOTIFICATIONTYPE_INET_ONLINE)) {
  77. printf("OnNotification: INET_ONLINE\n");
  78. iState = 2;
  79. } else if (IsEqualGUID(nt, NOTIFICATIONTYPE_INET_OFFLINE)) {
  80. printf("OnNotification: INET_OFFLINE\n");
  81. } else if (IsEqualGUID(nt, NOTIFICATIONTYPE_BEGIN_REPORT)) {
  82. printf("OnNotification: BEGIN_REPORT\n");
  83. } else if (IsEqualGUID(nt, NOTIFICATIONTYPE_END_REPORT)) {
  84. printf("OnNotification: END_REPORT\n");
  85. switch(iState) {
  86. case 0:
  87. // trying to exit...
  88. break;
  89. case 1:
  90. // dial failed, we're done
  91. iState = 0;
  92. break;
  93. case 2:
  94. // connected successfully - now hang up
  95. printf("Sending hangup notification.\n");
  96. SendNotification(NOTIFICATIONTYPE_DISCONNECT_FROM_INTERNET);
  97. iState = 3;
  98. break;
  99. case 3:
  100. // hung up. We're done.
  101. iState = 0;
  102. break;
  103. default :
  104. break;
  105. } /* switch */
  106. } else printf("OnNotification unknown notification type\n");
  107. // Avoid bogus assert
  108. if (SUCCEEDED(hr)) hr = S_OK;
  109. return hr;
  110. }
  111. ///////////////////////////////////////////////////////////////////////////
  112. ///////////////////////////////////////////////////////////////////////////
  113. //
  114. // Other members
  115. //
  116. HRESULT CDialTest::SendNotification(NOTIFICATIONTYPE type)
  117. {
  118. HRESULT hr;
  119. INotificationMgr *pNotificationMgr = NULL;
  120. INotification *pNotification;
  121. // get notification manager
  122. hr = CoCreateInstance(CLSID_StdNotificationMgr, NULL, CLSCTX_INPROC_SERVER,
  123. IID_INotificationMgr, (void**)&pNotificationMgr);
  124. if(SUCCEEDED(hr)) {
  125. // create a notification
  126. hr = pNotificationMgr->CreateNotification(
  127. type,
  128. (NOTIFICATIONFLAGS)0,
  129. NULL,
  130. &pNotification,
  131. 0);
  132. }
  133. if(SUCCEEDED(hr)) {
  134. // deliver it
  135. hr = pNotificationMgr->DeliverNotification(
  136. pNotification,
  137. CLSID_ConnectionAgent,
  138. DM_NEED_COMPLETIONREPORT,
  139. (INotificationSink *)this,
  140. NULL,
  141. 0);
  142. pNotification->Release();
  143. }
  144. if(pNotificationMgr)
  145. pNotificationMgr->Release();
  146. return SUCCEEDED(hr);
  147. }
  148. CDialTest::CDialTest()
  149. {
  150. m_cRef = 0;
  151. }
  152. CDialTest::~CDialTest()
  153. {
  154. }
  155. HRESULT CDialTest::Dial(void)
  156. {
  157. printf("Sending dial notification.\n");
  158. return SendNotification(NOTIFICATIONTYPE_CONNECT_TO_INTERNET);
  159. }
  160. int __cdecl main()
  161. {
  162. BOOL fDone = FALSE;
  163. DWORD dwRes;
  164. MSG msg;
  165. CDialTest s;
  166. if(FAILED(CoInitialize(NULL)))
  167. return TRUE;
  168. s.Dial();
  169. while(iState && GetMessage(&msg, NULL, 0, 0)) {
  170. if(msg.hwnd != NULL) {
  171. TranslateMessage(&msg);
  172. DispatchMessage(&msg);
  173. }
  174. }
  175. CoUninitialize();
  176. return 0;
  177. }