Leaked source code of windows server 2003
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.

598 lines
12 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. faxroute.cpp
  5. Abstract:
  6. This file implements the CFaxRoutingMethod and
  7. CFaxRoutingMethods interfaces.
  8. Author:
  9. Wesley Witt (wesw) 1-June-1997
  10. Environment:
  11. User Mode
  12. --*/
  13. #include "stdafx.h"
  14. #include "faxroute.h"
  15. CFaxRoutingMethod::CFaxRoutingMethod()
  16. {
  17. m_pFaxPort = NULL;
  18. m_LastFaxError = NO_ERROR;
  19. m_DeviceId = 0;
  20. m_Enabled = FALSE;
  21. m_DeviceName = NULL;
  22. m_Guid = NULL;
  23. m_FunctionName = NULL;
  24. m_ImageName = NULL;
  25. m_FriendlyName = NULL;
  26. }
  27. CFaxRoutingMethod::~CFaxRoutingMethod()
  28. {
  29. if (m_pFaxPort)
  30. {
  31. m_pFaxPort->Release();
  32. }
  33. SysFreeString( m_DeviceName );
  34. SysFreeString( m_Guid );
  35. SysFreeString( m_FunctionName );
  36. SysFreeString( m_ImageName );
  37. SysFreeString( m_FriendlyName );
  38. SysFreeString( m_ExtensionName );
  39. FaxFreeBuffer( m_RoutingData );
  40. }
  41. BOOL
  42. CFaxRoutingMethod::Initialize(
  43. CFaxPort *i_pFaxPort,
  44. DWORD i_DeviceId,
  45. BOOL i_Enabled,
  46. LPCWSTR i_DeviceName,
  47. LPCWSTR i_Guid,
  48. LPCWSTR i_FunctionName,
  49. LPCWSTR i_FriendlyName,
  50. LPCWSTR i_ImageName,
  51. LPCWSTR i_ExtensionName
  52. )
  53. {
  54. m_pFaxPort = i_pFaxPort;
  55. if (!m_pFaxPort)
  56. {
  57. return FALSE;
  58. }
  59. if ( FAILED(m_pFaxPort->AddRef()) )
  60. {
  61. m_pFaxPort = NULL;
  62. return FALSE;
  63. }
  64. m_DeviceId = i_DeviceId;
  65. m_Enabled = i_Enabled;
  66. m_DeviceName = SysAllocString(i_DeviceName);
  67. m_Guid = SysAllocString(i_Guid);
  68. m_FunctionName = SysAllocString(i_FunctionName);
  69. m_ImageName = SysAllocString(i_ImageName);
  70. m_FriendlyName = SysAllocString(i_FriendlyName);
  71. m_ExtensionName = SysAllocString(i_ExtensionName);
  72. m_RoutingData = NULL;
  73. if ( (!m_DeviceName && i_DeviceName) ||
  74. (!m_Guid && i_Guid) ||
  75. (!m_FunctionName && i_FunctionName) ||
  76. (!m_ImageName && i_ImageName) ||
  77. (!m_FriendlyName && i_FriendlyName) ||
  78. (!m_ExtensionName && i_ExtensionName)
  79. )
  80. {
  81. m_LastFaxError = E_OUTOFMEMORY;
  82. goto error;
  83. }
  84. DWORD Size = 0;
  85. if (!FaxGetRoutingInfoW( m_pFaxPort->GetPortHandle(), m_Guid, &m_RoutingData, &Size ))
  86. {
  87. m_RoutingData = NULL;
  88. m_LastFaxError = GetLastError();
  89. goto error;
  90. }
  91. if (Size == 0)
  92. {
  93. FaxFreeBuffer( m_RoutingData );
  94. m_RoutingData = NULL;
  95. }
  96. return TRUE;
  97. error:
  98. SysFreeString(m_DeviceName);
  99. SysFreeString(m_Guid);
  100. SysFreeString(m_FunctionName);
  101. SysFreeString(m_ImageName);
  102. SysFreeString(m_FriendlyName);
  103. SysFreeString(m_ExtensionName);
  104. m_pFaxPort->Release();
  105. m_pFaxPort = NULL;
  106. return FALSE;
  107. }
  108. STDMETHODIMP CFaxRoutingMethod::InterfaceSupportsErrorInfo(REFIID riid)
  109. {
  110. static const IID* arr[] = { &IID_IFaxRoutingMethod };
  111. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++) {
  112. if (InlineIsEqualGUID(*arr[i],riid)) {
  113. return S_OK;
  114. }
  115. }
  116. return S_FALSE;
  117. }
  118. STDMETHODIMP CFaxRoutingMethod::get_DeviceId(long * pVal)
  119. {
  120. if (!pVal) {
  121. return E_POINTER;
  122. }
  123. __try {
  124. *pVal = m_DeviceId;
  125. return S_OK;
  126. } __except (EXCEPTION_EXECUTE_HANDLER) {
  127. }
  128. return E_UNEXPECTED;
  129. }
  130. STDMETHODIMP CFaxRoutingMethod::get_Enable(BOOL * pVal)
  131. {
  132. if (!pVal) {
  133. return E_POINTER;
  134. }
  135. __try {
  136. *pVal = m_Enabled;
  137. return S_OK;
  138. } __except (EXCEPTION_EXECUTE_HANDLER) {
  139. }
  140. return E_UNEXPECTED;
  141. }
  142. STDMETHODIMP CFaxRoutingMethod::put_Enable(BOOL newVal)
  143. {
  144. if (!FaxEnableRoutingMethodW( m_pFaxPort->GetPortHandle(), m_Guid, newVal)) {
  145. return Fax_HRESULT_FROM_WIN32(GetLastError());
  146. }
  147. m_Enabled = newVal;
  148. return S_OK;
  149. }
  150. STDMETHODIMP CFaxRoutingMethod::get_DeviceName(BSTR * pVal)
  151. {
  152. if (!pVal)
  153. {
  154. return E_POINTER;
  155. }
  156. BSTR Copy = SysAllocString(m_DeviceName);
  157. if (!Copy && m_DeviceName)
  158. {
  159. return E_OUTOFMEMORY;
  160. }
  161. __try
  162. {
  163. *pVal = Copy;
  164. return S_OK;
  165. }
  166. __except (EXCEPTION_EXECUTE_HANDLER)
  167. {
  168. SysFreeString(Copy);
  169. }
  170. return E_UNEXPECTED;
  171. }
  172. STDMETHODIMP CFaxRoutingMethod::get_Guid(BSTR * pVal)
  173. {
  174. if (!pVal)
  175. {
  176. return E_POINTER;
  177. }
  178. BSTR Copy = SysAllocString(m_Guid);
  179. if (!Copy && m_Guid)
  180. {
  181. return E_OUTOFMEMORY;
  182. }
  183. __try
  184. {
  185. *pVal = Copy;
  186. return S_OK;
  187. }
  188. __except (EXCEPTION_EXECUTE_HANDLER)
  189. {
  190. SysFreeString(Copy);
  191. }
  192. return E_UNEXPECTED;
  193. }
  194. STDMETHODIMP CFaxRoutingMethod::get_FunctionName(BSTR * pVal)
  195. {
  196. if (!pVal)
  197. {
  198. return E_POINTER;
  199. }
  200. BSTR Copy = SysAllocString(m_FunctionName);
  201. if (!Copy && m_FunctionName)
  202. {
  203. return E_OUTOFMEMORY;
  204. }
  205. __try
  206. {
  207. *pVal = Copy;
  208. return S_OK;
  209. }
  210. __except (EXCEPTION_EXECUTE_HANDLER)
  211. {
  212. SysFreeString(Copy);
  213. }
  214. return E_UNEXPECTED;
  215. }
  216. STDMETHODIMP CFaxRoutingMethod::get_ImageName(BSTR * pVal)
  217. {
  218. if (!pVal)
  219. {
  220. return E_POINTER;
  221. }
  222. BSTR Copy = SysAllocString(m_ImageName);
  223. if (!Copy && m_ImageName)
  224. {
  225. return E_OUTOFMEMORY;
  226. }
  227. __try
  228. {
  229. *pVal = Copy;
  230. return S_OK;
  231. }
  232. __except (EXCEPTION_EXECUTE_HANDLER)
  233. {
  234. SysFreeString(Copy);
  235. }
  236. return E_UNEXPECTED;
  237. }
  238. STDMETHODIMP CFaxRoutingMethod::get_FriendlyName(BSTR * pVal)
  239. {
  240. if (!pVal)
  241. {
  242. return E_POINTER;
  243. }
  244. BSTR Copy = SysAllocString(m_FriendlyName);
  245. if (!Copy && m_FriendlyName)
  246. {
  247. return E_OUTOFMEMORY;
  248. }
  249. __try
  250. {
  251. *pVal = Copy;
  252. return S_OK;
  253. }
  254. __except (EXCEPTION_EXECUTE_HANDLER)
  255. {
  256. SysFreeString(Copy);
  257. }
  258. return E_UNEXPECTED;
  259. }
  260. STDMETHODIMP CFaxRoutingMethod::get_ExtensionName(BSTR * pVal)
  261. {
  262. if (!pVal)
  263. {
  264. return E_POINTER;
  265. }
  266. BSTR Copy = SysAllocString(m_ExtensionName);
  267. if (!Copy && m_ExtensionName)
  268. {
  269. return E_OUTOFMEMORY;
  270. }
  271. __try
  272. {
  273. *pVal = Copy;
  274. return S_OK;
  275. }
  276. __except (EXCEPTION_EXECUTE_HANDLER)
  277. {
  278. SysFreeString(Copy);
  279. }
  280. return E_UNEXPECTED;
  281. }
  282. STDMETHODIMP CFaxRoutingMethod::get_RoutingData(BSTR * pVal)
  283. {
  284. if (!pVal)
  285. {
  286. return E_POINTER;
  287. }
  288. BSTR Copy = NULL;
  289. __try
  290. {
  291. if (m_RoutingData == NULL)
  292. {
  293. Copy = SysAllocString(_T(""));
  294. if (!Copy)
  295. {
  296. return E_OUTOFMEMORY;
  297. }
  298. }
  299. else if (*((LPDWORD)m_RoutingData) == 0 || *((LPDWORD)m_RoutingData) == 1)
  300. {
  301. Copy = SysAllocString((LPWSTR)(m_RoutingData + sizeof(DWORD)) );
  302. if (!Copy && ((LPWSTR)(m_RoutingData + sizeof(DWORD))) )
  303. {
  304. return E_OUTOFMEMORY;
  305. }
  306. }
  307. else
  308. {
  309. return E_UNEXPECTED;
  310. }
  311. *pVal = Copy;
  312. return S_OK;
  313. }
  314. __except (EXCEPTION_EXECUTE_HANDLER)
  315. {
  316. SysFreeString(Copy);
  317. }
  318. return E_UNEXPECTED;
  319. }
  320. CFaxRoutingMethods::CFaxRoutingMethods()
  321. {
  322. m_pFaxPort = NULL;
  323. m_LastFaxError = 0;
  324. m_MethodCount = 0;
  325. m_VarVect = NULL;
  326. }
  327. CFaxRoutingMethods::~CFaxRoutingMethods()
  328. {
  329. if (m_pFaxPort) {
  330. m_pFaxPort->Release();
  331. }
  332. if (m_VarVect) {
  333. delete [] m_VarVect;
  334. }
  335. }
  336. BOOL CFaxRoutingMethods::Init(CFaxPort *pFaxPort)
  337. {
  338. HRESULT hr;
  339. if (!pFaxPort)
  340. {
  341. return FALSE;
  342. }
  343. m_pFaxPort = pFaxPort;
  344. hr = m_pFaxPort->AddRef();
  345. if (FAILED(hr))
  346. {
  347. m_pFaxPort = NULL;
  348. return FALSE;
  349. }
  350. PFAX_ROUTING_METHODW RoutingMethod = NULL;
  351. DWORD Size = 0;
  352. //
  353. // get the routing methods from the server
  354. //
  355. if (!FaxEnumRoutingMethodsW( m_pFaxPort->GetPortHandle(), &RoutingMethod, &m_MethodCount ))
  356. {
  357. m_LastFaxError = GetLastError();
  358. return FALSE;
  359. }
  360. //
  361. // enumerate the methods
  362. //
  363. m_VarVect = new CComVariant[m_MethodCount];
  364. if (!m_VarVect)
  365. {
  366. FaxFreeBuffer( RoutingMethod );
  367. return FALSE;
  368. }
  369. for (DWORD i=0; i<m_MethodCount; i++)
  370. {
  371. //
  372. // create the object
  373. //
  374. CComObject<CFaxRoutingMethod> *pFaxRoutingMethod;
  375. hr = CComObject<CFaxRoutingMethod>::CreateInstance( &pFaxRoutingMethod );
  376. if (FAILED(hr))
  377. {
  378. delete [] m_VarVect;
  379. m_VarVect = NULL;
  380. FaxFreeBuffer( RoutingMethod );
  381. return FALSE;
  382. }
  383. //
  384. // set the values
  385. //
  386. if (!pFaxRoutingMethod->Initialize(
  387. m_pFaxPort,
  388. RoutingMethod[i].DeviceId,
  389. RoutingMethod[i].Enabled,
  390. RoutingMethod[i].DeviceName,
  391. RoutingMethod[i].Guid,
  392. RoutingMethod[i].FunctionName,
  393. RoutingMethod[i].FriendlyName,
  394. RoutingMethod[i].ExtensionImageName,
  395. RoutingMethod[i].ExtensionFriendlyName
  396. ))
  397. {
  398. delete [] m_VarVect;
  399. m_VarVect = NULL;
  400. FaxFreeBuffer( RoutingMethod );
  401. return FALSE;
  402. }
  403. //
  404. // get IDispatch pointer
  405. //
  406. LPDISPATCH lpDisp = NULL;
  407. hr = pFaxRoutingMethod->QueryInterface( IID_IDispatch, (void**)&lpDisp );
  408. if (FAILED(hr))
  409. {
  410. delete [] m_VarVect;
  411. m_VarVect = NULL;
  412. FaxFreeBuffer( RoutingMethod );
  413. return FALSE;
  414. }
  415. //
  416. // create a variant and add it to the collection
  417. //
  418. CComVariant &var = m_VarVect[i];
  419. __try
  420. {
  421. var.vt = VT_DISPATCH;
  422. var.pdispVal = lpDisp;
  423. hr = S_OK;
  424. }
  425. __except (EXCEPTION_EXECUTE_HANDLER)
  426. {
  427. hr = E_UNEXPECTED;
  428. }
  429. if (FAILED(hr))
  430. {
  431. delete [] m_VarVect;
  432. m_VarVect = NULL;
  433. FaxFreeBuffer( RoutingMethod );
  434. return FALSE;
  435. }
  436. }
  437. FaxFreeBuffer( RoutingMethod );
  438. return TRUE;
  439. }
  440. STDMETHODIMP CFaxRoutingMethods::get_Item(long Index, VARIANT * retval)
  441. {
  442. if (!retval)
  443. {
  444. return E_POINTER;
  445. }
  446. //
  447. // use 1-based index, VB like
  448. //
  449. if ((Index < 1) || (Index > (long) m_MethodCount)) {
  450. return E_INVALIDARG;
  451. }
  452. __try
  453. {
  454. VariantInit( retval );
  455. retval->vt = VT_UNKNOWN;
  456. retval->punkVal = NULL;
  457. return VariantCopy( retval, &m_VarVect[Index-1] );
  458. } __except (EXCEPTION_EXECUTE_HANDLER)
  459. {
  460. }
  461. return E_UNEXPECTED;
  462. }
  463. STDMETHODIMP CFaxRoutingMethods::get_Count(long * pVal)
  464. {
  465. if (!pVal) {
  466. return E_POINTER;
  467. }
  468. __try {
  469. *pVal = m_MethodCount;
  470. return S_OK;
  471. } __except (EXCEPTION_EXECUTE_HANDLER) {
  472. }
  473. return E_UNEXPECTED;
  474. }