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.

226 lines
5.1 KiB

  1. /****************************************************************************
  2. PROGRAM: UTIL.C
  3. PURPOSE: System utility routines
  4. ****************************************************************************/
  5. #include "xerox.h"
  6. #include <string.h>
  7. /****************************************************************************
  8. FUNCTION: Alloc
  9. PURPOSE: Allocates memory to hold the specified number of bytes
  10. RETURNS : Pointer to allocated memory or NULL on failure
  11. ****************************************************************************/
  12. PVOID
  13. Alloc(
  14. SIZE_T Bytes
  15. )
  16. {
  17. HANDLE hMem;
  18. PVOID Buffer;
  19. hMem = LocalAlloc(LMEM_MOVEABLE, Bytes + sizeof(hMem));
  20. if (hMem == NULL) {
  21. return(NULL);
  22. }
  23. // Lock down the memory
  24. //
  25. Buffer = LocalLock(hMem);
  26. if (Buffer == NULL) {
  27. LocalFree(hMem);
  28. return(NULL);
  29. }
  30. //
  31. // Store the handle at the start of the memory block and return
  32. // a pointer to just beyond it.
  33. //
  34. *((PHANDLE)Buffer) = hMem;
  35. return (PVOID)(((PHANDLE)Buffer)+1);
  36. }
  37. /****************************************************************************
  38. FUNCTION: GetAllocSize
  39. PURPOSE: Returns the allocated size of the specified memory block.
  40. The block must have been previously allocated using Alloc
  41. RETURNS : Size of memory block in bytes or 0 on error
  42. ****************************************************************************/
  43. SIZE_T
  44. GetAllocSize(
  45. PVOID Buffer
  46. )
  47. {
  48. HANDLE hMem;
  49. hMem = *(((PHANDLE)Buffer) - 1);
  50. return(LocalSize(hMem) - sizeof(hMem));
  51. }
  52. /****************************************************************************
  53. FUNCTION: Free
  54. PURPOSE: Frees the memory previously allocated with Alloc
  55. RETURNS : TRUE on success, otherwise FALSE
  56. ****************************************************************************/
  57. BOOL
  58. Free(
  59. PVOID Buffer
  60. )
  61. {
  62. HANDLE hMem;
  63. hMem = *(((PHANDLE)Buffer) - 1);
  64. LocalUnlock(hMem);
  65. return(LocalFree(hMem) == NULL);
  66. }
  67. /****************************************************************************
  68. FUNCTION: AddItem
  69. PURPOSE: Adds the item string and data to the specified control
  70. The control is assumed to be a list-box unless fCBox == TRUE
  71. in which case the control is assumed to be a ComboBox
  72. RETURNS: Index at which the item was added or < 0 on error
  73. ****************************************************************************/
  74. INT
  75. AddItem(
  76. HWND hDlg,
  77. INT ControlID,
  78. LPSTR String,
  79. LONG_PTR Data,
  80. BOOL fCBox
  81. )
  82. {
  83. HWND hwnd;
  84. INT iItem;
  85. USHORT AddStringMsg = LB_ADDSTRING;
  86. USHORT SetDataMsg = LB_SETITEMDATA;
  87. if (fCBox) {
  88. AddStringMsg = CB_ADDSTRING;
  89. SetDataMsg = CB_SETITEMDATA;
  90. }
  91. hwnd = GetDlgItem(hDlg, ControlID);
  92. iItem = (INT)SendMessage(hwnd, AddStringMsg, 0, (LONG_PTR)String);
  93. if (iItem >= 0) {
  94. SendMessage(hwnd, SetDataMsg, iItem, Data);
  95. }
  96. return(iItem);
  97. }
  98. /****************************************************************************
  99. FUNCTION: AddItemhwnd
  100. PURPOSE: Adds the item string and data to the specified control
  101. The control is assumed to be a list-box unless fCBox == TRUE
  102. in which case the control is assumed to be a ComboBox
  103. RETURNS: Index at which the item was added or < 0 on error
  104. ****************************************************************************/
  105. INT
  106. AddItemhwnd(
  107. HWND hwnd,
  108. LPSTR String,
  109. LONG_PTR Data,
  110. BOOL fCBox
  111. )
  112. {
  113. INT iItem;
  114. USHORT AddStringMsg = LB_ADDSTRING;
  115. USHORT SetDataMsg = LB_SETITEMDATA;
  116. if (fCBox) {
  117. AddStringMsg = CB_ADDSTRING;
  118. SetDataMsg = CB_SETITEMDATA;
  119. }
  120. iItem = (INT)SendMessage(hwnd, AddStringMsg, 0, (LONG_PTR)String);
  121. if (iItem >= 0) {
  122. SendMessage(hwnd, SetDataMsg, iItem, Data);
  123. }
  124. return(iItem);
  125. }
  126. /****************************************************************************
  127. FUNCTION: FindData
  128. PURPOSE: Searches for the specified data in a combo box or lbox.
  129. RETURNS: Index of matching item or < 0 on error
  130. ****************************************************************************/
  131. LONG_PTR
  132. FindData(
  133. HWND hwnd,
  134. LONG_PTR data,
  135. BOOL fCBox
  136. )
  137. {
  138. INT cItems;
  139. USHORT GetCountMsg = LB_GETCOUNT;
  140. USHORT GetDataMsg = LB_GETITEMDATA;
  141. if (fCBox) {
  142. GetCountMsg = CB_GETCOUNT;
  143. GetDataMsg = CB_GETITEMDATA;
  144. }
  145. cItems = (INT)SendMessage(hwnd, GetCountMsg, 0, 0);
  146. if (cItems >= 0) {
  147. INT iItem;
  148. LONG_PTR ItemData;
  149. for (iItem =0; iItem < cItems; iItem ++) {
  150. ItemData = SendMessage(hwnd, GetDataMsg, iItem, 0);
  151. if (data == ItemData) {
  152. return(iItem);
  153. }
  154. }
  155. }
  156. return(-1);
  157. }