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.

293 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 1996-1997 Microsoft Corporation
  3. Module Name:
  4. formtray.c
  5. Abstract:
  6. Unidrv form-to-tray assignent
  7. Environment:
  8. Windows NT printer drivers
  9. Revision History:
  10. 01/07/97 -amandan-
  11. Created it.
  12. --*/
  13. #include "lib.h"
  14. #include "unilib.h"
  15. FORM_TRAY_TABLE
  16. PGetAndConvertOldVersionFormTrayTable(
  17. IN HANDLE hPrinter,
  18. OUT PDWORD pdwSize
  19. )
  20. /*++
  21. Routine Description:
  22. Retrieve the old Unidrv form-to-tray assignment table from registry and
  23. convert it to the new format for the caller
  24. Arguments:
  25. hPrinter - Handle to the printer object
  26. pdwSize - Returns the form-to-tray assignment table size
  27. Return Value:
  28. Pointer to form-to-tray assignment table read from the registry
  29. NULL if there is an error
  30. --*/
  31. {
  32. PWSTR pwstrNewTable;
  33. PWSTR pwstrOld, pwstrEnd, pwstrNew;
  34. DWORD dwTableSize, dwNewTableSize, dwTrayName, dwFormName;
  35. FORM_TRAY_TABLE pFormTrayTable;
  36. //
  37. // Read unidrv form-tray-assignment table, kludgy since unidrv
  38. // table does not include a size
  39. //
  40. if ((pFormTrayTable = PtstrGetPrinterDataString(hPrinter,
  41. REGVAL_TRAYFORM_TABLE_RASDD,
  42. &dwTableSize)) == NULL)
  43. return NULL;
  44. //
  45. // Convert the old format form-to-tray assignment table to new format
  46. // OLD NEW
  47. // Tray Name Tray Name
  48. // Form Name Form Name
  49. // SelectStr
  50. //
  51. pwstrOld = pFormTrayTable;
  52. pwstrEnd = pwstrOld + (dwTableSize / sizeof(WCHAR) - 1);
  53. //
  54. // Figuring out the size of new table,
  55. // the last field in the table must be a NUL so add count for it here first
  56. //
  57. dwNewTableSize = 1;
  58. while (pwstrOld < pwstrEnd && *pwstrOld != NUL)
  59. {
  60. dwTrayName = wcslen(pwstrOld) + 1;
  61. pwstrOld += dwTrayName;
  62. dwFormName = wcslen(pwstrOld) + 1;
  63. pwstrOld += dwFormName;
  64. //
  65. // New format contain only TrayName and FormName
  66. //
  67. dwNewTableSize += dwTrayName + dwFormName;
  68. //
  69. // Skip SelectStr
  70. //
  71. pwstrOld += wcslen(pwstrOld) + 1;
  72. }
  73. dwNewTableSize *= sizeof(WCHAR);
  74. if ((pwstrOld != pwstrEnd) ||
  75. (pwstrNewTable = MemAlloc(dwNewTableSize)) == NULL)
  76. {
  77. ERR(( "Couldn't convert form-to-tray assignment table.\n"));
  78. MemFree(pFormTrayTable);
  79. return FALSE;
  80. }
  81. pwstrOld = pFormTrayTable ;
  82. pwstrNew = pwstrNewTable;
  83. while (pwstrOld < pwstrEnd)
  84. {
  85. //
  86. // Copy slot name, form name
  87. //
  88. PWSTR pwstrSave = pwstrOld;
  89. pwstrOld += wcslen(pwstrOld) + 1;
  90. pwstrOld += wcslen(pwstrOld) + 1;
  91. memcpy(pwstrNew, pwstrSave, (DWORD)(pwstrOld - pwstrSave) * sizeof(WCHAR));
  92. pwstrNew += (pwstrOld - pwstrSave);
  93. //
  94. // skip SelectStr
  95. //
  96. pwstrOld += wcslen(pwstrOld) + 1;
  97. }
  98. *pwstrNew = NUL;
  99. if (pdwSize)
  100. *pdwSize = dwNewTableSize;
  101. MemFree(pFormTrayTable);
  102. return(pwstrNewTable);
  103. }
  104. #ifndef KERNEL_MODE
  105. BOOL
  106. BSaveAsOldVersionFormTrayTable(
  107. IN HANDLE hPrinter,
  108. IN FORM_TRAY_TABLE pwstrTable,
  109. IN DWORD dwTableSize
  110. )
  111. /*++
  112. Routine Description:
  113. Save form-to-tray assignment table in NT 4.0 compatible format
  114. Arguments:
  115. hPrinter - Handle to the current printer
  116. pwstrTable - Points to new format form-tray table
  117. dwTableSize - Size of form-tray table to be saved, in bytes
  118. Return Value:
  119. TRUE if successful, FALSE if there is an error
  120. --*/
  121. {
  122. DWORD dwOldTableSize;
  123. PWSTR pwstrNew, pwstrOld, pwstrOldTable;
  124. DWORD dwStatus;
  125. //
  126. // Find out how much memory to allocate for old format table
  127. //
  128. ASSERT(dwTableSize % sizeof(WCHAR) == 0 && dwTableSize >= sizeof(WCHAR));
  129. pwstrNew = pwstrTable;
  130. dwOldTableSize = dwTableSize;
  131. while (*pwstrNew != NUL)
  132. {
  133. //
  134. // Skip tray name, form name
  135. //
  136. pwstrNew += wcslen(pwstrNew) + 1;
  137. //
  138. // If form name is "Not Available", NT4 drivers writes L"0" to
  139. // the buffer so we do the same.
  140. //
  141. if (*pwstrNew == NUL)
  142. {
  143. dwOldTableSize += sizeof(WCHAR);
  144. pwstrNew++;
  145. }
  146. else
  147. pwstrNew += wcslen(pwstrNew) + 1;
  148. //
  149. // Extra 2 characters per entry for SelectStr
  150. //
  151. dwOldTableSize += 2*sizeof(WCHAR);
  152. }
  153. if ((pwstrOldTable = MemAllocZ(dwOldTableSize)) == NULL)
  154. {
  155. ERR(( "Memory allocation failed\n"));
  156. return FALSE;
  157. }
  158. //
  159. // Copy the new tray, form to old format table
  160. //
  161. pwstrNew = pwstrTable;
  162. pwstrOld = pwstrOldTable;
  163. while (*pwstrNew != NUL)
  164. {
  165. //
  166. // Copy slot name, form name
  167. //
  168. PWSTR pwstrSave = pwstrNew;
  169. pwstrNew += wcslen(pwstrNew) + 1;
  170. memcpy(pwstrOld, pwstrSave, (DWORD)(pwstrNew - pwstrSave) * sizeof(WCHAR));
  171. pwstrOld += (pwstrNew - pwstrSave);
  172. //
  173. // If form name is "Not Available", NT4 drivers writes L"0" to
  174. // the buffer so we do the same.
  175. //
  176. if (*pwstrNew == NUL)
  177. {
  178. *pwstrOld++ = L'0';
  179. *pwstrOld++ = NUL;
  180. pwstrNew++;
  181. }
  182. else
  183. {
  184. pwstrSave = pwstrNew;
  185. pwstrNew += wcslen(pwstrNew) + 1;
  186. memcpy(pwstrOld, pwstrSave, (DWORD)(pwstrNew - pwstrSave) * sizeof(WCHAR));
  187. pwstrOld += (pwstrNew - pwstrSave);
  188. }
  189. //
  190. // Set SelectStr to be NUL
  191. //
  192. *pwstrOld++ = L'0';
  193. *pwstrOld++ = NUL;
  194. }
  195. *pwstrOld = NUL;
  196. //
  197. // Save to registry under old key
  198. //
  199. dwStatus = SetPrinterData(hPrinter,
  200. REGVAL_TRAYFORM_TABLE_RASDD,
  201. REG_MULTI_SZ,
  202. (PBYTE) pwstrOldTable,
  203. dwOldTableSize);
  204. MemFree(pwstrOldTable);
  205. return (dwStatus == ERROR_SUCCESS);
  206. }
  207. #endif // !KERNEL_MODE