home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / vbarrays.arj / HUGEARR.TXT < prev    next >
Encoding:
Text File  |  1991-05-30  |  14.0 KB  |  429 lines

  1.                                 HUGEARR.DLL
  2.                Huge array support for Microsoft Visual Basic
  3.  
  4.               from Microsoft Product Support Services 5/30/91
  5.  
  6. HUGEARR.DLL is a dynamic-link library (DLL) which contains functions 
  7. for creation, maintenance, and deletion of arrays larger than 64K from 
  8. Microsoft Visual Basic version 1.00 for Windows. This DLL also gives 
  9. the ability to create arrays with more than 32,767 (32K) elements per 
  10. dimension, and to redimension arrays while preserving the data inside 
  11. of the arrays.
  12.  
  13. The following files are provided:
  14.  
  15.    HUGEARR.DLL, HUGEARR.BAS, HUGEARR.C, HUGEARR.DEF, HUGEARR.H,  
  16.    HUGEARR.TXT, MAKEFILE  
  17.  
  18. To use the functions in HUGEARR.DLL, simply copy the declarations 
  19. contained in HUGEARR.BAS into your global module in Visual Basic and 
  20. copy HUGEARR.DLL to your Windows directory. The functions can then be 
  21. used like any other Windows DLL function.
  22.  
  23. HUGEARR.DLL allocates memory using the Windows API function 
  24. GlobalAlloc. This means that the largest array that can be allocated 
  25. is 1 MB in standard mode, and 64 MB in 386 enhanced mode for Windows.
  26.  
  27. The following routines are contained in HUGEARR.DLL. For a complete
  28. description of the parameters and/or return values of these routines,
  29. see Visual Basic's Declare statement for the routine in question in 
  30. the file HUGEARR.BAS. 
  31.  
  32. HUGEARR.DLL Language Reference:
  33. ------------------------------
  34.  
  35. -------------------------------------------------------------------------
  36. HugeDim:
  37.  
  38. Action: Dimensions an array and returns a handle to that array.
  39.  
  40. Syntax: HugeDim(recsize%, limit&)
  41.  
  42. Argument       Description
  43. --------       -----------
  44.  
  45. recsize%       The size of each element in the array.  (i.e. an integer
  46.                would be 2, a double would be 8.)  You can use the Len()
  47.                function to determine the size of any data type if you are
  48.                unsure.
  49.  
  50. limit&         The upper bound of the array.  The lower bound of all arrays
  51.                is 0, so for example: 'HugeDim(2, 10)' would create an 
  52.                integer array of elements 0 through 10.
  53.  
  54. Remarks:
  55. -------
  56.  
  57. You should not try to create a huge array of variable-length strings, or of
  58. a user-defined type that contains variable-length strings.  Visual Basic's
  59. string handling routines would not know of the existence of any string that
  60. was controlled by HUGEARR.DLL, and a UAE ("Unrecoverable Application Error")
  61. would probably result.  Fixed-length strings are okay though.
  62.  
  63. If the total size (in bytes) of the array is going to be bigger than 64K,
  64. the size of each element has to be an integer power of two (1, 2, 4, 8,
  65. and so forth.)  This is necessary so that the no element of an array 
  66. straddles a segment boundary.
  67.  
  68. HugeDim returns a handle to the array that was created, and that handle is
  69. used when referring to that array with other commands.  If an error
  70. occurred (such as out of memory), it will return a negative number.  Error
  71. codes are detailed below under the section entitled 'Error Codes'.
  72.  
  73. Example:
  74. -------
  75.  
  76. Sub Command1_Click()
  77.      Dim hArray as integer
  78.      Dim variable as SomeUserDefinedType
  79.  
  80.      hArray = HugeDim(Len(variable), 10)
  81.      If hArray < 0 Then
  82.           print "Error dimensioning array:"; hArray
  83.           Stop
  84.      End If
  85.           .
  86.           .
  87.           .
  88.      i% = HugeErase(hArray)
  89. End Sub
  90.  
  91. -------------------------------------------------------------------------
  92. HugeErase:
  93.  
  94. Action: Erases an array that was previously dimensioned using HugeDim.
  95.  
  96. Syntax: HugeErase(hArray%)
  97.  
  98. Argument       Description
  99. --------       -----------
  100.  
  101. hArray%        The handle to the array.  (The same handle that was returned
  102.                by HugeDim.)
  103.  
  104. Remarks:
  105. --------
  106.  
  107. You should be sure to HugeErase all arrays that were HugeDim'd.  Failing to
  108. do so would cause the memory used by the array to not be freed up until the
  109. application quits.  If many arrays are dimensioned but never erased memory
  110. would keep being allocated without being freed, eventually degrading system
  111. performance.
  112.  
  113. Example:  Refer to the example for HugeDim.
  114.  
  115. -------------------------------------------------------------------------
  116. HugeRedim:
  117.  
  118. Action:  Redimensions an array created with HugeDim to a different size.
  119.  
  120. Syntax:  HugeRedim(hArray%, limit&)
  121.  
  122. Argument       Description
  123. --------       -----------
  124.  
  125. hArray%        The handle to the array to redimension.
  126.  
  127. limit&         The new upper bound of the array.
  128.  
  129. Remarks:
  130. --------
  131.  
  132. HugeRedim, unlike Visual Basic's ReDim, preserves all the data in the
  133. array.  If you want to erase the contents of the array, you should erase it
  134. and the dimension it again.
  135.  
  136. If the size of the array would go over 64K when it is redimensioned, it is
  137. subject to the same size restrictions detailed in the discussion of
  138. HugeDim.
  139.  
  140. You cannot change the size of the elements in the array, only the number of
  141. elements.
  142.  
  143. Example:
  144. --------
  145.  
  146. Sub Command1_Click()
  147.      .
  148.      .
  149.      i% = HugeRedim(hArray, 50)
  150.      if i% < 0 then
  151.           print "error redimensioning array:"; hArray
  152.           stop
  153.      End If
  154.  
  155.      e% = HugeErase(hArray)
  156. End Sub
  157.  
  158. -------------------------------------------------------------------------
  159. GetHugeEl, SetHugeEl:
  160.  
  161. Action:  Gets or sets the contents of an array element.
  162.  
  163. Syntax:  GetHugeEl(hArray%, el&, variable)
  164.         SetHugeEl(hArray%, el&, variable)
  165.  
  166. Argument       Description
  167. --------       -----------
  168.  
  169. hArray%        The handle to the array.
  170.  
  171. el&            The element of the array to set or get the data from.
  172.  
  173. variable       The variable to get into, or to set the array from.
  174.  
  175. Remarks:
  176. --------
  177.  
  178. It is extremely important that the type for the variable passed to
  179. GetHugeEl or SetHugeEl matches that type of the variable used in the
  180. HugeDim statement, if the types are of different lengths, you will mostly
  181. likely get a UAE or overwrite other data.  If you want to be sure that the
  182. types match you can use the Alias keyword when you declare the function to
  183. introduce type checking.  Refer to the section below entitled 'Aliasing'
  184. for more information on aliases.
  185.  
  186. Example:
  187. --------
  188. Sub Command2_Click()
  189.      .
  190.      .
  191.      .
  192.      hArray = HugeDim(len(i%), 10)
  193.      If hArray < 0 Then Stop
  194.      e% = SetHugeEl(hArray, 1, 54%)               ' puts 54 into element #1
  195.      If e% < 0 Then Stop                ' check error code
  196.      e% = GetHugeEl(hArray, 1, i%)           ' get element #1 into i%
  197.      Print i%
  198.  
  199.      e% = HugeErase(hArray)
  200. End Sub
  201.  
  202. -----------------------------------------------------------------------
  203. HugeInt:
  204. HugeLong:
  205. HugeSingle:
  206. HugeDouble:
  207. HugeCurrency:
  208.  
  209. Action:  Retrieves an element from an array of a given type.
  210.  
  211. Syntax:  HugeInt(hArray%, el&)
  212.          HugeLong(hArray%, el&)
  213.          HugeSingle(hArray%, el&)
  214.          HugeDouble(hArray%, el&)
  215.          HugeCurrency(hArray%, el&)
  216.  
  217. Argument       Description
  218. --------       -----------
  219.  
  220. hArray%        The handle to the array.
  221.  
  222. el&            The element to retrieve.
  223.  
  224. Remarks:
  225. --------
  226.  
  227. This family of functions is provided as a shortcut alternative to GetHugeEl
  228. to retrieve a given data type while in an expression.  For example:
  229.  
  230.      value = HugeDouble(hArray, 0) * HugeDouble(hArray, 1)
  231.  
  232. The example above could have been done using GetHugeEl; however, the values
  233. returned by the two HugeDouble calls would have to be assigned to
  234. variables, and then the variables would be used in the expression.  The
  235. example below expands more on this.
  236.  
  237. IMPORTANT: None of these functions return error codes, so you should use
  238. them only if you are positive that the value of hArray% and el& are legal
  239. values.  If a error does occur (such as a "Subscript Out of Range"), you
  240. will get meaningless results.  You should use GetHugeEl if you need to be
  241. able to check error codes.
  242.  
  243. Example:
  244. --------
  245.  
  246. Sub Command3_Click()
  247.      Dim hArray As Integer
  248.  
  249.      hArray = HugeDim(len(i%), 10)
  250.      If hArray < 0 Then Stop
  251.  
  252.      e% = SetHugeEl(hArray, 0, 3%)           ' Put 3 in element #0
  253.      If e% < 0 Then Stop                     ' Check for errors
  254.      e% = SetHugeEl(hArray, 1, 4%)           ' Put 4 in element #1
  255.      If e% < 0 Then Stop
  256.  
  257.      e% = GetHugeEl(hArray, 0, i%)           ' Get value of element #0
  258.      If e% < 0 Then Stop
  259.      e% = GetHugeEl(hArray, 1, j%)           ' Get value of element #1
  260.      If e% < 0 Then Stop
  261.      Print Sqr(i% ^ 2 + j ^ 2)
  262.  
  263.                                              ' Alternate (and faster) 
  264.                                              ' way of doing the above.
  265.      Print Sqr(HugeInt(hArray, 0) ^ 2 + HugeInt(hArray, 1) ^ 2)
  266.  
  267.      e% = HugeErase(hArray)
  268. End Sub
  269.  
  270. -------------------------------------------------------------------------
  271. HugeUbound:
  272.  
  273. Action:  Returns the upper bound of a give array.
  274.  
  275. Syntax:  HugeUbound(hArray%)
  276.  
  277. Argument       Description
  278. --------       -----------
  279.  
  280. hArray%        The handle to the array.
  281.  
  282. Remarks:
  283. --------
  284.  
  285. This function is the same as Basic's 'Ubound' function.  It is used to
  286. return the current upper bound of an array.
  287.  
  288. If HugeUbound returns an negative value, it represents an error code.
  289.  
  290. Example:
  291. --------
  292.  
  293. Sub Command4_Click()
  294.      Dim hArray as integer
  295.  
  296.      hArray = HugeDim(len(i%), 23)
  297.      If hArray < 0 Then Stop
  298.  
  299.      for j=0 to HugeUbound(hArray)           ' Initialize array to 10's
  300.           e% = SetHugeEl(hArray, j, 10%)
  301.           If e% < 0 Then Stop
  302.      Next j
  303.      .
  304.      .
  305.      .
  306. End Sub
  307.  
  308. ------------------------------------------------------------------------
  309. NumHugeArrays:
  310.  
  311. Action:  Returns the number of free huge arrays available.
  312.  
  313. Syntax:  NumHugeArrays
  314.  
  315. Remarks:
  316. -------
  317.  
  318. This command is included mostly for debugging purposes.  It is used to find
  319. out how many array could be dimensioned at that time by the program.
  320.  
  321. Example:
  322. --------
  323.  
  324. Sub Command5_Click()
  325.  
  326.      Debug.Print NumHugeArrays
  327.  
  328. End Sub
  329.  
  330. ---------------------------------------------------------------------------
  331. Error Codes:
  332.  
  333. The following functions return error codes as described below:
  334.  
  335.      HugeDim, HugeErase, HugeRedim, GetHugeEl, SetHugeEl, HugeUbound
  336.  
  337. The possible error codes are:
  338.  
  339.      0    The function was successful.
  340.  
  341.      -1   "Out of Memory"  There is not enough global memory for the
  342.           HugeDim or HugeRedim
  343.  
  344.      -2   "To Many Arrays"  There are no free arrays left to be
  345.           dimensioned.
  346.  
  347.      -3   "Bad Element Size"  The array that you are trying to dimension is
  348.           greater than 64K, and the element size is not an integer power of
  349.           2.
  350.  
  351.      -4   "Subscript out of Range"  The array element you are trying to
  352.           access is outside the bounds of the array.
  353.  
  354.      -5   "Illegal Array Handle"  The array that was referenced is not a
  355.           valid array.  Either it is not dimensioned, or the handle value
  356.           is illegal.
  357. ------------------------------------------------------------------------
  358. Aliasing:
  359.  
  360. The GetHugeEl and SetHugeEl functions transfer data back and forth from an
  361. array. Because these functions must work with any data type, they are
  362. declared 'As Any' in their declarations.  This has the disadvantage that it
  363. defeats Basic's built-in type checking, and allows the posibility of
  364. passing the wrong data type.
  365.  
  366. To work around this potential problem, you can use Basic's 'Alias' keyword
  367. in the declaration.  For example, if you wanted GetHugeEl to work with the
  368. data type 'UserType', you might rename the function to 'GetUserType', and
  369. alias it to 'GetHugeEl'.
  370.  
  371. The declaration for GetHugeEl contained in "HUGEARR.BAS"  is:
  372.  
  373.      Declare Function GetHugeEl Lib "hugearr.dll" (ByVal Index%,
  374.      ByVal el&, buffer As Any) As Integer
  375.  
  376. To force Basic to do type checking on the call, you would rewrite the
  377. declaration to be:
  378.  
  379.      Declare Function GetUserType Lib "hugearr.dll" Alias "GetHugeEl"
  380.      (ByVal Index%, ByVal el&, buffer As UserType) As Integer
  381.  
  382. Note that the 'buffer As Any' has been changed to 'buffer As UserType'.
  383. Because the function no longer has the 'As Any' type Basic will be able to
  384. raise an error if you try to pass the wrong data type.
  385.  
  386. A function can be aliased any number of times, so you may want to create an
  387. alias for each data type that you are using.
  388.  
  389. -----------------------------------------------------------------------
  390. Constants:
  391.  
  392. The SetHugeEl routine is used to assign values to an array.  For example,
  393. the following statement
  394.  
  395.      i% = SetHugeEl(hArray, 1, v%)
  396.  
  397. would assign the value stored in v% to element #1 of the array hArray.
  398. However, a problem can arise when you try to use a constant in place of the
  399. 'v%' above.  For example:
  400.  
  401.      i% = SetHugeEl(hArray, 1, 15)
  402.  
  403. In this case, Visual Basic would assume that the number 15 is an integer
  404. constant and would pass a pointer to an integer to the SetHugeEl routine; 
  405. even if hArray is a (for instance) array of double-precision numbers.  
  406. To work around this potential problem, you should always use a type suffix 
  407. when passing constants to SetHugeEl.  If you wanted to assign the value 
  408. of 15 to a double precision array, you would use the statement:
  409.  
  410.      i% = SetHugeEl(hArray, 1, 15#)
  411.  
  412. --------------------------------------------------------------------------
  413.  
  414. HUGEARR.DLL Memory and Capacity:
  415.  
  416. HUGEARR.DLL allocates memory using the Windows API function GlobalAlloc.
  417. This means that the largest array that can be allocated is 1 MB in standard
  418. mode, and 64 MB in 386 enhanced mode.
  419.  
  420. If you forget to HugeErase an array that was allocated with HugeDim,
  421. Windows will automatically deallocate the memory after your application
  422. terminates.  However, HUGEARR.DLL keeps the information it needs to
  423. maintain the arrays in it's own private area.  This means that any array
  424. which is not HugeErase'd will not have it's information released, and the
  425. array will not be marked as free.  If two applications are both using the
  426. DLL, and the first application HugeDim's all of the arrays and then quits 
  427. without HugeEraseing them, the second application will not be able to 
  428. create any arrays.
  429.