home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / VISUAL_B / CODIGO_1 / MORE_API / MOREAPI.INF < prev    next >
Encoding:
Text File  |  1991-09-30  |  6.4 KB  |  178 lines

  1.                              MORE API
  2.  
  3. MoreAPI demonstrates three flavors of API functions that can be used in
  4. VB Applications.  MOREAPI.MAK requires the following files:
  5.  
  6.         MOREAPI.FRM
  7.         RESTARTW.FRM
  8.         WVS.FRM
  9.  
  10. I've also compiled MOREAPI into an EXE to quickly demo some of these
  11. functions.
  12.  
  13. *****************************************************************************
  14.  
  15. 1.  Getting the full path and name of your EXE.
  16.  
  17. There are many situations when an App needs to know the exact drive and
  18. subdirectory it was started from.  The reasons will vary but the most 
  19. common one is because it keeps files in that subdirectory that it needs 
  20. to reference.  The API call GetModuleFileName will return the full path
  21. and EXE name.  Note: during development, in the VB environment, this function
  22. retuns [drive:][\path\]VB.EXE but when you compile your App it will return
  23. your Application's full path and EXE name, e.g. E:\WINAPPS\MYAPP.EXE
  24.  
  25. Place these in your global module or in the general/declarations section:
  26.  
  27. Declare Function GetClassWord Lib "User" (ByVal Hwnd As Integer, 
  28.                 ByVal nIndex As Integer) As Integer
  29.  
  30. Declare Function GetModuleFileName Lib "Kernel" (ByVal hModule As Integer, 
  31.                 ByVal lpFilename As String, 
  32.                 ByVal nSize As Integer) As Integer
  33.  
  34. Const GCW_HMODULE = (-16)
  35.  
  36. We need to use GetClassWord to obtain a handle to our module.  This is the
  37. first parameter required by GetModuleFileName.  In addition, we need
  38. to prepare a return buffer long enough to hold our EXE name:
  39.  
  40.     MyEXEName$ = String$(127, 0)
  41.     hMymodule% = GetClassWord(MyForm.Hwnd, GCW_HMODULE)
  42.     x% = GetModuleFileName(hMyModule%, MyEXEName$, Len(MyEXEName$))
  43.  
  44. After running this code sample, MyEXEName$ will contain the full path and
  45. name of our EXE.  This is demonstrated in MOREAPI.MAK.  MOREAPI.EXE returns
  46. the correct drive, path and EXE name.
  47.  
  48. *****************************************************************************
  49.  
  50. 2.  Using WVSPrintF for formatting or conversion.
  51.  
  52. The API function WVSPrintF can come in handy in several situations.  It is
  53. documented in the Windows Programmer's reference but here are some examples:
  54.  
  55. Place this in your global module or the general/declarations section:
  56.  
  57. Declare Function WVSPrintF Lib "User" (ByVal lpOutput As String, 
  58.                 ByVal lpFormat As String, 
  59.                 lpArglist As Integer) As Integer
  60.  
  61. The first parameter is a return buffer that we prepare, the second one is the
  62. string with all the formatting codes, and the third parameter is an integer
  63. or the first member of an array of integers containing the values to be
  64. formatted/converted.
  65.  
  66. a. This will convert a signed integer to an unsigned integer:
  67.  
  68.     Buf$ = String$(128, 0)
  69.     x% = WVSPrintF(Buf$, "The unsigned value of -29108 is %u", -29108)
  70.  
  71.    We could also turn this into a function:
  72.  
  73.      Function MakeUnsignedInt (SignedInt As Integer) As Long
  74.          Buf$ = String$(30, 0)
  75.          x% = WVSPrintF(Buf$, "%u", SignedInt)
  76.          MakeUnsignedInt = Val(Buf$)
  77.      End Function
  78.  
  79. b. This will return the Hexadecimal representation of an integer:
  80.  
  81.     Buf$ = String$(128, 0)
  82.     x% = WVSPrintF(Buf$, "Integer 1991 is Hex %X", 1991)      ' Hex 7C7
  83.  
  84.    Alternatively we can use: 
  85.  
  86.     x% = WVSPrintF(Buf$, "Integer 1991 is Hex %04X", 1991)   ' Hex 07C7
  87.  
  88.    The "04" here between "%" and "X" instructs WVSPrintF to pad the left of 
  89.    the output string with zeros and that the expected output width is four 
  90.    characters.
  91.  
  92. c. An array of integers can be converted or formatted with WVSPrintF:
  93.  
  94.     Buf$ = String$(128, 0)
  95.     ReDim MyArray%(2)
  96.     MyArray%(1) = 42
  97.     MyArray%(2) = 43
  98.     x% = WVSPrintF(Buf$, "Decimal 42 is Hex %X" + Chr$(10) + 
  99.                     "Decimal 43 is Hex %X", MyArray%(1))
  100.    
  101. d. In addition we can fake the MakeLong macro that VB doesn't have by 
  102.    passing two integers and letting the API create the union:
  103.  
  104.     Buf$ = String$(128, 0)
  105.     ReDim MakeLongArray%(2)
  106.     MakeLongArray%(1) = 2   'Low Word
  107.     MakeLongArray%(2) = 23  'High Word
  108.     x% = WVSPrintF(Buf$, "Low Word = 2, High Word = 23" + Chr$(10) + 
  109.                     "Makelong = %li", MakeLongArray%(1))
  110.  
  111. e. Or WVSPrintF can be used to create a HEX dump:
  112.  
  113.     AString$ = "Visual Basic"               ' Sample string
  114.     Dump$ = Space$(3 * Len(AString$))       ' Buffer for formatting codes
  115.     ReDim IntArray%(Len(AString$))          ' Int Array to hold AString$
  116.     For i% = 1 To Len(AString$)
  117.        IntArray%(i%) = Asc(Mid$(AString$, i%, 1))
  118.        Mid$(Dump$, ((i% - 1) * 3 + 1), 3) = "%X "
  119.     Next i%
  120.     Buf$ = String$(Len(Dump$), 0)           ' Return Buffer
  121.     x% = WVSPrintF(Buf$, Dump$, IntArray%(1))
  122.  
  123. All these WVSPrintF uses are demonstrated in MOREAPI.MAK. 
  124.  
  125. ****************************************************************************
  126.  
  127. 3.  The many ways to Exit Windows.
  128.  
  129. There are times when your Application may make changes to the Windows'
  130. drivers or WIN.INI that in order to take effect a WM_WININICHANGE message
  131. won't do.  Well, there are two ways to accomplish this with the same API
  132. function.
  133.  
  134. Declare Function ExitWindows Lib "User" (ByVal dwReserved As Long, 
  135.                 ByVal wReturnCode As Integer) As Integer
  136.  
  137. Please note: the declaration for ExitWindows is wrong in WINAPI.TXT.  The
  138. correct declaration should pass wReturnCode ByVal.
  139.  
  140. Normal Exit / Go to DOS
  141.  
  142. Okay% = ExitWindows(0, 0)
  143.  
  144. This call will terminate the Windows session and return the user to DOS.
  145.  
  146.  
  147. Magic Exit / Restart Windows
  148.  
  149. I don't take credit for discovering this.  Credit goes to SP Services for
  150. discovering and sharing this with Windows programmers.
  151.  
  152. Besides the normal exit, the API also accepts an undocumented value passed
  153. to dwReserved that Restarts the windows session.  This value is &H42.  
  154.  
  155. Okay% = ExitWindows(&H42, 0)
  156.  
  157. This will cause Windows to re-initialize without returning the user to the
  158. DOS prompt. It works the same at the Setup App when run from inside Windows.
  159.  
  160. ExitWindows is demonstrated in MOREAPI.MAK
  161.  
  162. ****************************************************************************
  163.  
  164. 4.  New line can be Chr$(10).
  165.  
  166. You'll notice that through out MOREAPI I only use Chr$(10) to create a new line
  167. instead of the recommended Chr$(13) + Chr$(10).  So far this has been working
  168. well.  I don't know if future versions of Windows will support this.
  169.  
  170. ****************************************************************************
  171.  
  172. That's all.
  173.  
  174. Enjoy.
  175.  
  176. Costas Kitsos
  177. September 1991
  178.