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.

467 lines
8.3 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 "faxxp.h"
  11. #pragma hdrstop
  12. //
  13. // globals
  14. //
  15. BOOL oleInitialized;
  16. LPSTR Platforms[] =
  17. {
  18. "Windows NT x86",
  19. "Windows NT R4000",
  20. "Windows NT Alpha_AXP",
  21. "Windows NT PowerPC"
  22. };
  23. LPVOID
  24. MapiMemAlloc(
  25. DWORD Size
  26. )
  27. /*++
  28. Routine Description:
  29. Memory allocator.
  30. Arguments:
  31. Size - Number of bytes to allocate.
  32. Return Value:
  33. Pointer to the allocated memory or NULL for failure.
  34. --*/
  35. {
  36. LPVOID ptr;
  37. HRESULT hResult;
  38. hResult = gpfnAllocateBuffer( Size, &ptr );
  39. if (hResult) {
  40. ptr = NULL;
  41. } else {
  42. ZeroMemory( ptr, Size );
  43. }
  44. return ptr;
  45. }
  46. VOID
  47. MapiMemFree(
  48. LPVOID ptr
  49. )
  50. /*++
  51. Routine Description:
  52. Memory de-allocator.
  53. Arguments:
  54. ptr - Pointer to the memory block.
  55. Return Value:
  56. None.
  57. --*/
  58. {
  59. if (ptr) {
  60. gpfnFreeBuffer( ptr );
  61. }
  62. }
  63. HRESULT WINAPI
  64. OpenServiceProfileSection(
  65. LPMAPISUP pSupObj,
  66. LPPROFSECT * ppProfSectObj
  67. )
  68. /*++
  69. Routine Description:
  70. This function opens the profile section of this service, where the
  71. properties of a FAX provider (AB, MS, or XP) are stored.
  72. Arguments:
  73. pSupObj - Pointer to the provider support object
  74. ppProfSectObj - Where we return a pointer to the service profile
  75. section of the provider
  76. Return Value:
  77. An HRESULT.
  78. --*/
  79. {
  80. SPropTagArray sptService = { 1, { PR_SERVICE_UID } };
  81. LPPROFSECT pProvProfSectObj;
  82. ULONG cValues;
  83. LPSPropValue pProp;
  84. HRESULT hResult;
  85. //
  86. // Get the PROVIDER profile section
  87. //
  88. hResult = pSupObj->OpenProfileSection(
  89. NULL,
  90. MAPI_MODIFY,
  91. &pProvProfSectObj
  92. );
  93. if (!hResult) {
  94. // Get the UID of the profile section of the service where this provider is installed
  95. hResult = pProvProfSectObj->GetProps (&sptService, FALSE, &cValues, &pProp);
  96. if (SUCCEEDED(hResult)) {
  97. if (S_OK == hResult) {
  98. // Now, with the obtained UID, open the profile section of the service
  99. hResult = pSupObj->OpenProfileSection ((LPMAPIUID)pProp->Value.bin.lpb,
  100. MAPI_MODIFY,
  101. ppProfSectObj);
  102. } else {
  103. hResult = E_FAIL;
  104. }
  105. MemFree( pProp );
  106. }
  107. pProvProfSectObj->Release();
  108. }
  109. return hResult;
  110. }
  111. LPSTR
  112. RemoveLastNode(
  113. LPTSTR Path
  114. )
  115. /*++
  116. Routine Description:
  117. Removes the last node from a path string.
  118. Arguments:
  119. Path - Path string.
  120. Return Value:
  121. Pointer to the path string.
  122. --*/
  123. {
  124. DWORD i;
  125. if (Path == NULL || Path[0] == 0) {
  126. return Path;
  127. }
  128. i = strlen(Path)-1;
  129. if (Path[i] == '\\') {
  130. Path[i] = 0;
  131. i -= 1;
  132. }
  133. for (; i>0; i--) {
  134. if (Path[i] == '\\') {
  135. Path[i+1] = 0;
  136. break;
  137. }
  138. }
  139. return Path;
  140. }
  141. PDEVMODE
  142. GetPerUserDevmode(
  143. LPTSTR PrinterName
  144. )
  145. {
  146. PDEVMODE DevMode = NULL;
  147. LONG Size;
  148. PRINTER_DEFAULTS PrinterDefaults;
  149. HANDLE hPrinter;
  150. PrinterDefaults.pDatatype = NULL;
  151. PrinterDefaults.pDevMode = NULL;
  152. PrinterDefaults.DesiredAccess = PRINTER_READ;
  153. if (!OpenPrinter( PrinterName, &hPrinter, &PrinterDefaults )) {
  154. DebugPrint(( TEXT("OpenPrinter() failed, ec=%d"), GetLastError() ));
  155. return NULL;
  156. }
  157. Size = DocumentProperties(
  158. NULL,
  159. hPrinter,
  160. PrinterName,
  161. NULL,
  162. NULL,
  163. 0
  164. );
  165. if (Size < 0) {
  166. goto exit;
  167. }
  168. DevMode = (PDEVMODE) MemAlloc( Size );
  169. if (DevMode == NULL) {
  170. goto exit;
  171. }
  172. Size = DocumentProperties(
  173. NULL,
  174. hPrinter,
  175. PrinterName,
  176. DevMode,
  177. NULL,
  178. DM_OUT_BUFFER
  179. );
  180. if (Size < 0) {
  181. MemFree( DevMode );
  182. goto exit;
  183. }
  184. exit:
  185. ClosePrinter( hPrinter );
  186. return DevMode;
  187. }
  188. BOOL
  189. GetUserInfo(
  190. LPTSTR PrinterName,
  191. PUSER_INFO UserInfo
  192. )
  193. {
  194. LPBYTE DevMode;
  195. PDMPRIVATE PrivateDevMode = NULL;
  196. LPSTR AnsiScratch;
  197. HKEY hKey;
  198. DWORD Size;
  199. DevMode = (LPBYTE) GetPerUserDevmode( PrinterName );
  200. if (DevMode) {
  201. PrivateDevMode = (PDMPRIVATE) (DevMode + ((PDEVMODE) DevMode)->dmSize);
  202. }
  203. if (PrivateDevMode && PrivateDevMode->signature == DRIVER_SIGNATURE) {
  204. AnsiScratch = UnicodeStringToAnsiString( PrivateDevMode->billingCode );
  205. if (AnsiScratch) {
  206. strncpy( UserInfo->BillingCode, AnsiScratch, sizeof( UserInfo->BillingCode ) );
  207. MemFree( AnsiScratch );
  208. }
  209. }
  210. if (RegOpenKey( HKEY_CURRENT_USER, REGKEY_FAX_USERINFO, &hKey ) == ERROR_SUCCESS) {
  211. Size = sizeof(UserInfo->Company);
  212. if (RegQueryValueEx( hKey, REGVAL_COMPANY, NULL, NULL, (LPBYTE)UserInfo->Company, &Size) != ERROR_SUCCESS) {
  213. UserInfo->Company[0] = 0;
  214. }
  215. Size = sizeof(UserInfo->Dept);
  216. if (RegQueryValueEx( hKey, REGVAL_DEPT, NULL, NULL, (LPBYTE)UserInfo->Dept, &Size) != ERROR_SUCCESS) {
  217. UserInfo->Dept[0] = 0;
  218. }
  219. RegCloseKey( hKey );
  220. }
  221. if (DevMode) {
  222. MemFree( DevMode );
  223. }
  224. return TRUE;
  225. }
  226. LPSTR
  227. GetStringProperty(
  228. LPSPropValue pProps,
  229. DWORD PropId
  230. )
  231. {
  232. if (PROP_TYPE(pProps[PropId].ulPropTag) == PT_ERROR) {
  233. return StringDup( "" );
  234. }
  235. return StringDup( pProps[PropId].Value.LPSZ );
  236. }
  237. DWORD
  238. GetDwordProperty(
  239. LPSPropValue pProps,
  240. DWORD PropId
  241. )
  242. {
  243. if (PROP_TYPE(pProps[PropId].ulPropTag) == PT_ERROR) {
  244. return 0;
  245. }
  246. return pProps[PropId].Value.ul;
  247. }
  248. DWORD
  249. GetBinaryProperty(
  250. LPSPropValue pProps,
  251. DWORD PropId,
  252. LPVOID Buffer,
  253. DWORD SizeOfBuffer
  254. )
  255. {
  256. if (PROP_TYPE(pProps[PropId].ulPropTag) == PT_ERROR) {
  257. return 0;
  258. }
  259. if (pProps[PropId].Value.bin.cb > SizeOfBuffer) {
  260. return 0;
  261. }
  262. CopyMemory( Buffer, pProps[PropId].Value.bin.lpb, pProps[PropId].Value.bin.cb );
  263. return pProps[PropId].Value.bin.cb;
  264. }
  265. PVOID
  266. MyGetPrinter(
  267. LPSTR PrinterName,
  268. DWORD level
  269. )
  270. /*++
  271. Routine Description:
  272. Wrapper function for GetPrinter spooler API
  273. Arguments:
  274. hPrinter - Identifies the printer in question
  275. level - Specifies the level of PRINTER_INFO_x structure requested
  276. Return Value:
  277. Pointer to a PRINTER_INFO_x structure, NULL if there is an error
  278. --*/
  279. {
  280. HANDLE hPrinter;
  281. PBYTE pPrinterInfo = NULL;
  282. DWORD cbNeeded;
  283. PRINTER_DEFAULTS PrinterDefaults;
  284. PrinterDefaults.pDatatype = NULL;
  285. PrinterDefaults.pDevMode = NULL;
  286. PrinterDefaults.DesiredAccess = PRINTER_READ; //PRINTER_ALL_ACCESS;
  287. if (!OpenPrinter( PrinterName, &hPrinter, &PrinterDefaults )) {
  288. return NULL;
  289. }
  290. if (!GetPrinter( hPrinter, level, NULL, 0, &cbNeeded ) &&
  291. GetLastError() == ERROR_INSUFFICIENT_BUFFER &&
  292. (pPrinterInfo = (PBYTE) MemAlloc( cbNeeded )) &&
  293. GetPrinter( hPrinter, level, pPrinterInfo, cbNeeded, &cbNeeded ))
  294. {
  295. ClosePrinter( hPrinter );
  296. return pPrinterInfo;
  297. }
  298. ClosePrinter( hPrinter );
  299. MemFree( pPrinterInfo );
  300. return NULL;
  301. }
  302. LPSTR
  303. GetServerName(
  304. LPSTR PrinterName
  305. )
  306. /*++
  307. Routine Description:
  308. retrieve the servername given a printer name
  309. Arguments:
  310. PrinterName - Identifies the printer in question
  311. Return Value:
  312. Pointer to a string, NULL if there is an error
  313. --*/
  314. {
  315. PPRINTER_INFO_2 PrinterInfo = NULL;
  316. LPSTR ServerName = NULL;
  317. if (!PrinterName) {
  318. goto exit;
  319. }
  320. if (!(PrinterInfo = (PPRINTER_INFO_2) MyGetPrinter(PrinterName,2))) {
  321. goto exit;
  322. }
  323. if (PrinterInfo->pServerName) {
  324. ServerName = StringDup(PrinterInfo->pServerName);
  325. }
  326. exit:
  327. if (PrinterInfo) {
  328. MemFree(PrinterInfo);
  329. }
  330. return ServerName;
  331. }