home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / BASIC / HUGEARR / HUGEARR.TXT < prev    next >
Text File  |  1993-12-01  |  18KB  |  554 lines

  1.  
  2.                                 HUGEARR.DLL
  3.                Huge array support for Microsoft Visual Basic
  4.  
  5.               from Microsoft Product Support Services 6/4/91
  6.               with additions by various authors
  7.  
  8. The following information applies to Microsoft Visual Basic
  9. programming system version 1.0 for Microsoft Windows, and to Microsoft
  10. Windows 3.0 Software Development Kit (SDK).
  11.  
  12. HUGEARR.DLL is a dynamic-link library (DLL) which contains functions
  13. for creation, maintenance, and deletion of arrays larger than 64K from
  14. Microsoft Visual Basic version 1.00 for Windows. This DLL also gives
  15. the ability to create arrays with more than 32,767 (32K) elements per
  16. dimension, and to redimension arrays while preserving the data inside
  17. of the arrays.
  18.  
  19. To use the functions in HUGEARR.DLL, simply copy the declarations
  20. contained in HUGEARR.VBG into your global module in Visual Basic, copy
  21. the declarations in HUGEARR.VBM into any module, and copy HUGEARR.DLL
  22. to your Windows directory. The functions can then be used like any other
  23. Windows DLL function.
  24.  
  25. HUGEARR.DLL allocates memory using the Windows API function
  26. GlobalAlloc. This means that the largest array that can be allocated
  27. is 1 MB in standard mode, and 64 MB in 386 enhanced mode for Windows.
  28.  
  29. The following routines are contained in HUGEARR.DLL. For a complete
  30. description of the parameters and/or return values of these routines,
  31. see Visual Basic's Declare statement for the routine in question in
  32. the file HUGEARR.VBM.
  33.  
  34. HUGEARR.DLL Language Reference:
  35. ------------------------------
  36.  
  37. -------------------------------------------------------------------------
  38. VBHugeDim:
  39.  
  40. Action: Dimensions an array and returns a handle to that array.
  41.  
  42. Syntax: VBHugeDim(recsize%, limit&)
  43.  
  44. Argument       Description
  45. --------       -----------
  46.  
  47. recsize%       The size of each element in the array.  (i.e. an integer
  48.                would be 2, a double would be 8.)  You can use the Len()
  49.                function to determine the size of any data type if you are
  50.                unsure.
  51.  
  52. limit&         The upper bound of the array.  The lower bound of all arrays
  53.                is 0, so for example: 'VBHugeDim(2, 10)' would create an
  54.                integer array of elements 0 through 10.
  55.  
  56. Remarks:
  57. -------
  58.  
  59. You should not try to create a huge array of variable-length strings, or of
  60. a user-defined type that contains variable-length strings.  Visual Basic's
  61. string handling routines would not know of the existence of any string that
  62. was controlled by HUGEARR.DLL, and a UAE ("Unrecoverable Application Error")
  63. would probably result.  Fixed-length strings are okay though.
  64.  
  65. VBHugeDim returns a handle to the array that was created, and that handle is
  66. used when referring to that array with other commands.  If an error
  67. occurred (such as out of memory), it will return a negative number.  Error
  68. codes are detailed below under the section entitled 'Error Codes'.
  69.  
  70. Example:
  71. -------
  72.  
  73. Sub Command1_Click()
  74.      Dim hArray as integer
  75.      Dim variable as SomeUserDefinedType
  76.  
  77.      hArray = VBHugeDim(Len(variable), 10)
  78.      If hArray < 0 Then
  79.           print "Error dimensioning array:"; hArray
  80.           Stop
  81.      End If
  82.           .
  83.           .
  84.           .
  85.      i% = VBHugeErase(hArray)
  86. End Sub
  87.  
  88. -------------------------------------------------------------------------
  89. VBHugeErase:
  90.  
  91. Action: Erases an array that was previously dimensioned using VBHugeDim.
  92.  
  93. Syntax: VBHugeErase(hArray%)
  94.  
  95. Argument       Description
  96. --------       -----------
  97.  
  98. hArray%        The handle to the array.  (The same handle that was returned
  99.                by VBHugeDim.)
  100.  
  101. Remarks:
  102. --------
  103.  
  104. You should be sure to VBHugeErase all arrays that were VBHugeDim'd.  Failing to
  105. do so would cause the memory used by the array to not be freed up until the
  106. application quits.  If many arrays are dimensioned but never erased memory
  107. would keep being allocated without being freed, eventually degrading system
  108. performance.
  109.  
  110. Example:  Refer to the example for VBHugeDim.
  111.  
  112. -------------------------------------------------------------------------
  113. VBHugeRedim:
  114.  
  115. Action:  Redimensions an array created with VBHugeDim to a different size.
  116.  
  117. Syntax:  VBHugeRedim(hArray%, limit&)
  118.  
  119. Argument       Description
  120. --------       -----------
  121.  
  122. hArray%        The handle to the array to redimension.
  123.  
  124. limit&         The new upper bound of the array.
  125.  
  126. Remarks:
  127. --------
  128.  
  129. VBHugeRedim, unlike Visual Basic's ReDim, preserves all the data in the
  130. array.  If you want to erase the contents of the array, you should erase it
  131. and the dimension it again.
  132.  
  133. If the size of the array would go over 64K when it is redimensioned, it is
  134. subject to the same size restrictions detailed in the discussion of
  135. VBHugeDim.
  136.  
  137. You cannot change the size of the elements in the array, only the number of
  138. elements.
  139.  
  140. Example:
  141. --------
  142.  
  143. Sub Command1_Click()
  144.      .
  145.      .
  146.      i% = VBHugeRedim(hArray, 50)
  147.      if i% < 0 then
  148.           print "error redimensioning array:"; hArray
  149.           stop
  150.      End If
  151.  
  152.      e% = VBHugeErase(hArray)
  153. End Sub
  154.  
  155. -------------------------------------------------------------------------
  156. VBHugeGet, VBHugeSet:
  157.  
  158. Action:  Gets or sets the contents of an array element.
  159.  
  160. Syntax:  VBHugeGet(hArray%, el&, variable)
  161.          VBHugeSet(hArray%, el&, variable)
  162.  
  163. Argument       Description
  164. --------       -----------
  165.  
  166. hArray%        The handle to the array.
  167.  
  168. el&            The element of the array to set or get the data from.
  169.  
  170. variable       The variable to get into, or to set the array from.
  171.  
  172. Remarks:
  173. --------
  174.  
  175. It is extremely important that the type for the variable passed to
  176. VBHugeGet or VBHugeSet matches that type of the variable used in the
  177. VBHugeDim statement, if the types are of different lengths, you will mostly
  178. likely get a UAE or overwrite other data.  If you want to be sure that the
  179. types match you can use the Alias keyword when you declare the function to
  180. introduce type checking.  Refer to the section below entitled 'Aliasing'
  181. for more information on aliases.
  182.  
  183. Example:
  184. --------
  185. Sub Command2_Click()
  186.      .
  187.      .
  188.      .
  189.      hArray = VBHugeDim(len(i%), 10)
  190.      If hArray < 0 Then Stop
  191.      e% = VBHugeSet(hArray, 1, 54%)               ' puts 54 into element #1
  192.      If e% < 0 Then Stop                ' check error code
  193.      e% = VBHugeGet(hArray, 1, i%)           ' get element #1 into i%
  194.      Print i%
  195.  
  196.      e% = VBHugeErase(hArray)
  197. End Sub
  198.  
  199. -------------------------------------------------------------------------
  200. VBHugeGetNum, VBHugeSetNum:
  201.  
  202. Action:  Gets or sets the contents of multiple array elements.
  203.  
  204. Syntax:  VBHugeGetNum(hArray%, el&, nelem%, variable)
  205.          VBHugeSetNum(hArray%, el&, nelem%, variable)
  206.  
  207. Argument       Description
  208. --------       -----------
  209.  
  210. hArray%        The handle to the array.
  211.  
  212. el&            The first element of the array to set or get the data from.
  213.  
  214. nelem%         The number of elements to be set or fetched.
  215.  
  216. variable       The variable to get into, or to set the array from.
  217.  
  218. Remarks:
  219. --------
  220. See the Remarks for VBHugeGet, VBHugeSet.
  221.  
  222. Example:
  223. --------
  224. Sub Command2_Click()
  225.      .
  226.      .
  227.      Dim VBArray() as Double
  228.      ReDim VBArray(1 to 1000)
  229.      ' Assign something to the elements in VBArray.
  230.      .
  231.      .
  232.  
  233.      hArray = VBHugeDim(len(i#), 1000)
  234.      If hArray < 0 Then Stop
  235.  
  236.      ' Copy 1000 elements from VB array VBArray(1...1000) to huge array.
  237.      e% = VBHugeSetNum(hArray, 1, 1000, VBArray(1))
  238.      ' check error code
  239.      If e% < 0 Then Stop
  240.  
  241.      ' Copy 1000 elements from huge array to VB array VBArray(1...1000).
  242.      e% = VBHugeGetNum(hArray, 1, 1000, VBArray(1))
  243.  
  244.      e% = VBHugeErase(hArray)
  245. End Sub
  246.  
  247. ------------------------------------------------------------------------
  248. VBHugeSave:
  249.  
  250. Action:  Saves the specified number of elements of a huge array to the
  251.          named file.
  252.  
  253. Syntax:  VBHugeSave(hArray%, NEl&, RecLen%, Fn$)
  254.  
  255. Remarks:
  256. -------
  257.  
  258. The number of elements to save as well as the size of each element,
  259. together with the full filename of the file to be written to must be
  260. spupplied.
  261.  
  262. Example:
  263. --------
  264.  
  265. Sub Command6_Click()
  266.  
  267.        ErrCode& = VBHugeSave(hArray, hArrayElements&, hArryRecLen%, "FILE.ARR")
  268.        If ErrCode& < 0 Then Stop
  269.  
  270. End Sub
  271.  
  272. ---------------------------------------------------------------------------
  273. VBHugeLoad:
  274.  
  275. Action:  Loads a huge array from the named file.
  276.  
  277. Syntax:  VBHugeLoad(hArray%, RecLen%, Fn$)
  278.  
  279. Remarks:
  280. -------
  281.  
  282. The file is read until EOF is found.  The number of element read are
  283. returned unless an error occurs.  The Array to be loaded must be
  284. allocated sufficient space to hold the array before this function is
  285. called.
  286.  
  287. Example:
  288. --------
  289.  
  290. Sub Command7_Click()
  291.  
  292.        Open "MYFILE.ARR" For Random As #1 Len = hArrayRecLen%
  293.        hArrayElements& = LOF(1) / hArrayRecLen%
  294.        Close #1
  295.        hArray = VBHugeDim(hArrayRecLen%, hArrayElements&)
  296.        If hMain > 0 Then
  297.          hArrayElements& = VBHugeLoad(hArray, hArrayRecLen%, "MYFILE.ARR")
  298.          If hArrayElements& < 0& Then Stop
  299.        Else
  300.          Stop
  301.        End If
  302.  
  303. End Sub
  304.  
  305. -----------------------------------------------------------------------
  306. VBHugeInt:
  307. VBHugeLong:
  308. VBHugeSingle:
  309. VBHugeDouble:
  310. VBHugeCurrency:
  311.  
  312. Action:  Retrieves an element from an array of a given type.
  313.  
  314. Syntax:  VBHugeInt(hArray%, el&)
  315.          VBHugeLong(hArray%, el&)
  316.          VBHugeSingle(hArray%, el&)
  317.          VBHugeDouble(hArray%, el&)
  318.          VBHugeCurrency(hArray%, el&)
  319.  
  320. Argument       Description
  321. --------       -----------
  322.  
  323. hArray%        The handle to the array.
  324.  
  325. el&            The element to retrieve.
  326.  
  327. Remarks:
  328. --------
  329.  
  330. This family of functions is provided as a shortcut alternative to VBHugeGet
  331. to retrieve a given data type while in an expression.  For example:
  332.  
  333.      value = VBHugeDouble(hArray, 0) * VBHugeDouble(hArray, 1)
  334.  
  335. The example above could have been done using VBHugeGet; however, the values
  336. returned by the two VBHugeDouble calls would have to be assigned to
  337. variables, and then the variables would be used in the expression.  The
  338. example below expands more on this.
  339.  
  340. IMPORTANT: None of these functions return error codes, so you should use
  341. them only if you are positive that the value of hArray% and el& are legal
  342. values.  If a error does occur (such as a "Subscript Out of Range"), you
  343. will get meaningless results.  You should use VBHugeGet if you need to be
  344. able to check error codes.
  345.  
  346. Example:
  347. --------
  348.  
  349. Sub Command3_Click()
  350.      Dim hArray As Integer
  351.  
  352.      hArray = VBHugeDim(len(i%), 10)
  353.      If hArray < 0 Then Stop
  354.  
  355.      e% = VBHugeSet(hArray, 0, 3%)           ' Put 3 in element #0
  356.      If e% < 0 Then Stop                     ' Check for errors
  357.      e% = VBHugeSet(hArray, 1, 4%)           ' Put 4 in element #1
  358.      If e% < 0 Then Stop
  359.  
  360.      e% = VBHugeGet(hArray, 0, i%)           ' Get value of element #0
  361.      If e% < 0 Then Stop
  362.      e% = VBHugeGet(hArray, 1, j%)           ' Get value of element #1
  363.      If e% < 0 Then Stop
  364.      Print Sqr(i% ^ 2 + j ^ 2)
  365.  
  366.                                              ' Alternate (and faster)
  367.                                              ' way of doing the above.
  368.      Print Sqr(VBHugeInt(hArray, 0) ^ 2 + VBHugeInt(hArray, 1) ^ 2)
  369.  
  370.      e% = VBHugeErase(hArray)
  371. End Sub
  372.  
  373. -------------------------------------------------------------------------
  374. VBHugeUbound:
  375.  
  376. Action:  Returns the upper bound of a give array.
  377.  
  378. Syntax:  VBHugeUbound(hArray%)
  379.  
  380. Argument       Description
  381. --------       -----------
  382.  
  383. hArray%        The handle to the array.
  384.  
  385. Remarks:
  386. --------
  387.  
  388. This function is the same as Basic's 'Ubound' function.  It is used to
  389. return the current upper bound of an array.
  390.  
  391. If VBHugeUbound returns an negative value, it represents an error code.
  392.  
  393. Example:
  394. --------
  395.  
  396. Sub Command4_Click()
  397.      Dim hArray as integer
  398.  
  399.      hArray = VBHugeDim(len(i%), 23)
  400.      If hArray < 0 Then Stop
  401.  
  402.      for J& = 0 to VBHugeUbound(hArray)           ' Initialize array to 10's
  403.           ErrCode% = VBHugeSet(hArray, J&, 10%)
  404.           If ErrCode% < 0 Then Stop
  405.      Next J&
  406.      .
  407.      .
  408.      .
  409. End Sub
  410.  
  411. ------------------------------------------------------------------------
  412. VBHugeNumArrays:
  413.  
  414. Action:  Returns the number of free huge arrays available.
  415.  
  416. Syntax:  VBHugeNumArrays()
  417.  
  418. Remarks:
  419. -------
  420.  
  421. This command is included mostly for debugging purposes.  It is used to find
  422. out how many array could be dimensioned at that time by the program.
  423.  
  424. Example:
  425. --------
  426.  
  427. Sub Command5_Click()
  428.  
  429.      Debug.Print VBHugeNumArrays()
  430.  
  431. End Sub
  432.  
  433. ---------------------------------------------------------------------------
  434. Error Codes:
  435.  
  436. All functions (except for VBHugeInt, VBHugeLong, VBHugeSingle, VBHugeDouble,
  437. and VBHugeCurrency) return error codes as described below.
  438.  
  439. The possible error codes are:
  440.  
  441.      0    The function was successful.
  442.  
  443.      -1   "Out of Memory"  There is not enough global memory for the
  444.           VBHugeDim or VBHugeRedim
  445.  
  446.      -2   "To Many Arrays"  There are no free arrays left to be
  447.           dimensioned.
  448.  
  449.      -3   "Bad Element Size"  This error condition is no longer possible.
  450.  
  451.      -4   "Subscript out of Range"  The array element you are trying to
  452.           access is outside the bounds of the array.
  453.  
  454.      -5   "Illegal Array Handle"  The array that was referenced is not a
  455.           valid array.  Either it is not dimensioned, or the handle value
  456.           is illegal.
  457.  
  458.      -7   "Error Opening File"  The file specified could not be opened.
  459.  
  460.      -8   "Error Writing to File"  An error occurred during a file write
  461.           operation
  462.  
  463.      -9   "Error Reading File"  An error occurred during a file read
  464.           operation
  465.  
  466. ------------------------------------------------------------------------
  467. Aliasing:
  468.  
  469. The VBHugeGet and VBHugeSet functions transfer data back and forth from an
  470. array. Because these functions must work with any data type, they are
  471. declared 'As Any' in their declarations.  This has the disadvantage that it
  472. defeats Basic's built-in type checking, and allows the posibility of
  473. passing the wrong data type.
  474.  
  475. To work around this potential problem, you can use Basic's 'Alias' keyword
  476. in the declaration.  For example, if you wanted VBHugeGet to work with the
  477. data type 'UserType', you might rename the function to 'GetUserType', and
  478. alias it to 'VBHugeGet'.
  479.  
  480. The declaration for VBHugeGet contained in "HUGEARR.BAS"  is:
  481.  
  482.      Declare Function VBHugeGet Lib "hugearr.dll" (ByVal Index%,
  483.      ByVal el&, buffer As Any) As Integer
  484.  
  485. To force Basic to do type checking on the call, you would rewrite the
  486. declaration to be:
  487.  
  488.      Declare Function GetUserType Lib "hugearr.dll" Alias "VBHugeGet"
  489.      (ByVal Index%, ByVal el&, buffer As UserType) As Integer
  490.  
  491. Note that the 'buffer As Any' has been changed to 'buffer As UserType'.
  492. Because the function no longer has the 'As Any' type Basic will be able to
  493. raise an error if you try to pass the wrong data type.
  494.  
  495. A function can be aliased any number of times, so you may want to create an
  496. alias for each data type that you are using.
  497.  
  498. -----------------------------------------------------------------------
  499. Constants:
  500.  
  501. The VBHugeSet routine is used to assign values to an array.  For example,
  502. the following statement
  503.  
  504.      i% = VBHugeSet(hArray, 1, v%)
  505.  
  506. would assign the value stored in v% to element #1 of the array hArray.
  507. However, a problem can arise when you try to use a constant in place of the
  508. 'v%' above.  For example:
  509.  
  510.      i% = VBHugeSet(hArray, 1, 15)
  511.  
  512. In this case, Visual Basic would assume that the number 15 is an integer
  513. constant and would pass a pointer to an integer to the VBHugeSet routine;
  514. even if hArray is a (for instance) array of double-precision numbers.
  515. To work around this potential problem, you should always use a type suffix
  516. when passing constants to VBHugeSet.  If you wanted to assign the value
  517. of 15 to a double precision array, you would use the statement:
  518.  
  519.      i% = VBHugeSet(hArray, 1, 15#)
  520.  
  521. --------------------------------------------------------------------------
  522.  
  523. HUGEARR.DLL Memory and Capacity:
  524.  
  525. HUGEARR.DLL allocates memory using the Windows API function GlobalAlloc.
  526. This means that the largest array that can be allocated is 1 MB in standard
  527. mode, and 64 MB in 386 enhanced mode.
  528.  
  529. If you forget to VBHugeErase an array that was allocated with VBHugeDim,
  530. Windows will automatically deallocate the memory after your application
  531. terminates.  However, HUGEARR.DLL keeps the information it needs to
  532. maintain the arrays in it's own private area.  This means that any array
  533. which is not VBHugeErase'd will not have it's information released, and the
  534. array will not be marked as free.  If two applications are both using the
  535. DLL, and the first application VBHugeDim's all of the arrays and then quits
  536. without VBHugeEraseing them, the second application will not be able to
  537. create any arrays.
  538.  
  539. --------------------------------------------------------------------------
  540.  
  541. References:
  542.  
  543. HUGEARR.DLL is written in Microsoft C, and the C source code is
  544. provided with this application note in HUGEARR.C and HUGEARR.H.
  545.  
  546. The following references discuss how to program Windows 3.0 DLL
  547. routines:
  548.  
  549. 1. "Programming Windows: the Microsoft Guide to Writing Applications
  550.    for Windows 3," by Charles Petzold (published by Microsoft Press,
  551.    1990)
  552.  
  553. 2. Microsoft Windows 3.0 Software Development Kit
  554.