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.

277 lines
4.5 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. util.cpp
  5. Abstract:
  6. This module contains utility routines for the fax transport provider.
  7. Author:
  8. Wesley Witt (wesw) 13-Aug-1996
  9. --*/
  10. #include "faxext.h"
  11. #include "debugex.h"
  12. //
  13. // globals
  14. //
  15. BOOL oleInitialized;
  16. LPVOID
  17. MapiMemAlloc(
  18. SIZE_T Size
  19. )
  20. /*++
  21. Routine Description:
  22. Memory allocator.
  23. Arguments:
  24. Size - Number of bytes to allocate.
  25. Return Value:
  26. Pointer to the allocated memory or NULL for failure.
  27. --*/
  28. {
  29. LPVOID ptr=NULL;
  30. HRESULT hResult;
  31. // [Win64bug] MAPIAllocateBuffer should accpet size_t as allocating size
  32. hResult = MAPIAllocateBuffer( DWORD(Size), &ptr );
  33. if (S_OK == hResult)
  34. {
  35. ZeroMemory( ptr, Size );
  36. }
  37. return ptr;
  38. }
  39. LPVOID
  40. MapiMemReAlloc(
  41. LPVOID ptr,
  42. SIZE_T Size
  43. )
  44. /*++
  45. Routine Description:
  46. Memory re-allocator.
  47. Arguments:
  48. ptr - pointer to pre allocated buffer
  49. Size - Number of bytes to allocate.
  50. Return Value:
  51. Pointer to the allocated memory or NULL for failure.
  52. --*/
  53. {
  54. LPVOID NewPtr = NULL;
  55. HRESULT hResult;
  56. // [Win64bug] MAPIAllocateBuffer should accpet size_t as allocating size
  57. hResult = MAPIAllocateMore(DWORD(Size), ptr ,&NewPtr);
  58. if (S_OK == hResult)
  59. {
  60. ZeroMemory( NewPtr, Size );
  61. }
  62. return NewPtr;
  63. }
  64. VOID
  65. MapiMemFree(
  66. LPVOID ptr
  67. )
  68. /*++
  69. Routine Description:
  70. Memory de-allocator.
  71. Arguments:
  72. ptr - Pointer to the memory block.
  73. Return Value:
  74. None.
  75. --*/
  76. {
  77. if (ptr) {
  78. MAPIFreeBuffer( ptr );
  79. }
  80. }
  81. PVOID
  82. MyGetPrinter(
  83. LPTSTR PrinterName,
  84. DWORD level
  85. )
  86. /*++
  87. Routine Description:
  88. Wrapper function for GetPrinter spooler API
  89. Arguments:
  90. hPrinter - Identifies the printer in question
  91. level - Specifies the level of PRINTER_INFO_x structure requested
  92. Return Value:
  93. Pointer to a PRINTER_INFO_x structure, NULL if there is an error
  94. --*/
  95. {
  96. HANDLE hPrinter;
  97. PBYTE pPrinterInfo = NULL;
  98. DWORD cbNeeded;
  99. PRINTER_DEFAULTS PrinterDefaults;
  100. PrinterDefaults.pDatatype = NULL;
  101. PrinterDefaults.pDevMode = NULL;
  102. PrinterDefaults.DesiredAccess = PRINTER_READ; //PRINTER_ALL_ACCESS;
  103. if (!OpenPrinter( PrinterName, &hPrinter, &PrinterDefaults )) {
  104. return NULL;
  105. }
  106. if (!GetPrinter( hPrinter, level, NULL, 0, &cbNeeded ) &&
  107. GetLastError() == ERROR_INSUFFICIENT_BUFFER &&
  108. (pPrinterInfo = (PBYTE) MemAlloc( cbNeeded )) &&
  109. GetPrinter( hPrinter, level, pPrinterInfo, cbNeeded, &cbNeeded ))
  110. {
  111. ClosePrinter( hPrinter );
  112. return pPrinterInfo;
  113. }
  114. ClosePrinter( hPrinter );
  115. MemFree( pPrinterInfo );
  116. return NULL;
  117. }
  118. LPTSTR
  119. RemoveLastNode(
  120. LPTSTR Path
  121. )
  122. /*++
  123. Routine Description:
  124. Removes the last node from a path string.
  125. Arguments:
  126. Path - Path string.
  127. Return Value:
  128. Pointer to the path string.
  129. --*/
  130. {
  131. LPTSTR Pstr = NULL;
  132. if (Path == NULL || Path[0] == 0)
  133. {
  134. return Path;
  135. }
  136. Pstr = _tcsrchr(Path,TEXT('\\'));
  137. if( Pstr && (*_tcsinc(Pstr)) == '\0' )
  138. {
  139. // the last character is a backslash, truncate it...
  140. _tcsset(Pstr,TEXT('\0'));
  141. Pstr = _tcsdec(Path,Pstr);
  142. }
  143. Pstr = _tcsrchr(Path,TEXT('\\'));
  144. if( Pstr )
  145. {
  146. _tcsnset(_tcsinc(Pstr),TEXT('\0'),1);
  147. }
  148. return Path;
  149. }
  150. void
  151. ErrorMsgBox(
  152. HINSTANCE hInstance,
  153. HWND hWnd,
  154. DWORD dwMsgId
  155. )
  156. /*++
  157. Routine Description:
  158. Display error message box
  159. Arguments:
  160. hInstance - [in] resource instance handle
  161. hWnd - [in] window handle
  162. dwMsgId - [in] string resource ID
  163. Return Value:
  164. none
  165. --*/
  166. {
  167. TCHAR* ptCaption=NULL;
  168. TCHAR tszCaption[MAX_PATH];
  169. TCHAR tszMessage[MAX_PATH];
  170. DBG_ENTER(TEXT("ErrorMsgBox"));
  171. if(!LoadString( hInstance, IDS_FAX_EXT_MESSAGE, tszCaption, sizeof(tszCaption)/sizeof(TCHAR)))
  172. {
  173. CALL_FAIL(GENERAL_ERR, TEXT("LoadString"), ::GetLastError());
  174. }
  175. else
  176. {
  177. ptCaption = tszCaption;
  178. }
  179. if(!LoadString( hInstance, dwMsgId, tszMessage, sizeof(tszMessage)/sizeof(TCHAR)))
  180. {
  181. CALL_FAIL(GENERAL_ERR, TEXT("LoadString"), ::GetLastError());
  182. Assert(FALSE);
  183. return;
  184. }
  185. MessageBeep(MB_ICONEXCLAMATION);
  186. AlignedMessageBox(hWnd, tszMessage, ptCaption, MB_OK | MB_ICONERROR);
  187. }