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.

180 lines
5.8 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include "upnphost.h"
  4. #include <stdio.h>
  5. #include "resource.h"
  6. #include "CInternetGatewayDevice.h"
  7. #include "..\idl\obj\i386\InternetGatewayDevice_i.c"
  8. #include "CWANDevice.h"
  9. #include "CWANCommonDevice.h"
  10. CComModule _Module;
  11. BEGIN_OBJECT_MAP(ObjectMap)
  12. END_OBJECT_MAP()
  13. void Test()
  14. {
  15. HRESULT hr = S_OK;
  16. IUPnPRegistrar* pRegistrar;
  17. hr = CoCreateInstance(CLSID_UPnPRegistrar, NULL, CLSCTX_SERVER, IID_IUPnPRegistrar, reinterpret_cast<void**>(&pRegistrar));
  18. if(SUCCEEDED(hr))
  19. {
  20. HRSRC hrsrc = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDX_DESC_DOC), L"XML");
  21. if(hrsrc)
  22. {
  23. HGLOBAL hGlobal = LoadResource(GetModuleHandle(NULL), hrsrc);
  24. if(hGlobal)
  25. {
  26. void * pvData = LockResource(hGlobal);
  27. BSTR bstrData = NULL;
  28. if(pvData)
  29. {
  30. long nLength = SizeofResource(GetModuleHandle(NULL), hrsrc);
  31. wchar_t * sz = new wchar_t[nLength + 1];
  32. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, reinterpret_cast<char*>(pvData), nLength, sz, nLength);
  33. sz[nLength] = 0;
  34. bstrData = SysAllocString(sz);
  35. delete [] sz;
  36. }
  37. if(bstrData)
  38. {
  39. BSTR bstrId = NULL;
  40. BSTR bstrInitString = SysAllocString(L"Init");
  41. BSTR bstrPath = SysAllocString(L"C:\\upnp\\");
  42. IUPnPDeviceControl* pDevice;
  43. //hr = CoCreateInstance(CLSID_CInternetGatewayDevice, NULL, CLSCTX_INPROC, IID_IUPnPDeviceControl, reinterpret_cast<void**>(&pDevice));
  44. CComObject<CInternetGatewayDevice>* pDevice2;
  45. hr = CComObject<CInternetGatewayDevice>::CreateInstance(&pDevice2);
  46. if(SUCCEEDED(hr))
  47. {
  48. pDevice = pDevice2;
  49. pDevice->AddRef();
  50. }
  51. if(SUCCEEDED(hr))
  52. {
  53. wchar_t szFilename[1024];
  54. GetModuleFileName(NULL, szFilename, 1024);
  55. ITypeLib * pTlb = NULL;
  56. hr = LoadTypeLib(L"beacon.exe", &pTlb);
  57. pTlb->Release();
  58. if(FAILED(hr))
  59. {
  60. printf("type lib failed\n");
  61. }
  62. }
  63. if(SUCCEEDED(hr))
  64. {
  65. hr = pRegistrar->RegisterRunningDevice(
  66. bstrData,
  67. pDevice,
  68. bstrInitString,
  69. bstrPath,
  70. 10,
  71. &bstrId);
  72. if(FAILED(hr))
  73. {
  74. printf("rrd failed %x\n", hr);
  75. IErrorInfo* pErrorInfo;
  76. hr = GetErrorInfo(0, &pErrorInfo);
  77. if(S_OK == hr)
  78. {
  79. BSTR pDesc;
  80. hr = pErrorInfo->GetDescription(&pDesc);
  81. if(SUCCEEDED(hr))
  82. {
  83. wprintf(L"error desc %s\n", pDesc);
  84. SysFreeString(pDesc);
  85. }
  86. }
  87. hr = E_FAIL;
  88. }
  89. printf("press q to exit, u to update connection status\n");
  90. char buf[255];
  91. while(gets(buf))
  92. {
  93. if('q' == *buf)
  94. {
  95. break;
  96. }
  97. else if('u' == *buf)
  98. {
  99. CComObject<CWANDevice>* pWANDevice = pDevice2->m_pWANDevice;
  100. CComObject<CWANCommonDevice>* pWANCommonDevice = pWANDevice->m_pWANCommonDevice;
  101. pWANCommonDevice->UpdateConnectionStatus();
  102. }
  103. }
  104. if(SUCCEEDED(hr))
  105. {
  106. hr = pRegistrar->UnregisterDevice(bstrId, TRUE);
  107. if(FAILED(hr))
  108. {
  109. printf("rrd failed\n");
  110. }
  111. }
  112. }
  113. SysFreeString(bstrInitString);
  114. SysFreeString(bstrPath);
  115. if(SUCCEEDED(hr))
  116. {
  117. SysFreeString(bstrId);
  118. }
  119. SysFreeString(bstrData);
  120. }
  121. FreeResource(hGlobal);
  122. }
  123. }
  124. }
  125. }
  126. void TestDisconnect()
  127. {
  128. // CLanControl LanControl;
  129. //
  130. //
  131. // LanControl.FinalConstruct();
  132. // LanControl.Disconnect();
  133. }
  134. void TestConnect()
  135. {
  136. // CLanControl LanControl;
  137. //
  138. // LanControl.FinalConstruct();
  139. // LanControl.Connect();
  140. }
  141. int __cdecl main(int argc, char* argv[])
  142. {
  143. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  144. _Module.Init(ObjectMap, GetModuleHandle(NULL), &LIBID_UPnPInternetGatewayDeviceLib);
  145. Test();
  146. // if(argc == 1)
  147. // {
  148. // TestConnect();
  149. // }
  150. // else
  151. // {
  152. // TestDisconnect();
  153. // }
  154. _Module.Term();
  155. CoUninitialize();
  156. return 0;
  157. }