home *** CD-ROM | disk | FTP | other *** search
- MORE API
-
- MoreAPI demonstrates three flavors of API functions that can be used in
- VB Applications. MOREAPI.MAK requires the following files:
-
- MOREAPI.FRM
- RESTARTW.FRM
- WVS.FRM
-
- I've also compiled MOREAPI into an EXE to quickly demo some of these
- functions.
-
- *****************************************************************************
-
- 1. Getting the full path and name of your EXE.
-
- There are many situations when an App needs to know the exact drive and
- subdirectory it was started from. The reasons will vary but the most
- common one is because it keeps files in that subdirectory that it needs
- to reference. The API call GetModuleFileName will return the full path
- and EXE name. Note: during development, in the VB environment, this function
- retuns [drive:][\path\]VB.EXE but when you compile your App it will return
- your Application's full path and EXE name, e.g. E:\WINAPPS\MYAPP.EXE
-
- Place these in your global module or in the general/declarations section:
-
- Declare Function GetClassWord Lib "User" (ByVal Hwnd As Integer,
- ByVal nIndex As Integer) As Integer
-
- Declare Function GetModuleFileName Lib "Kernel" (ByVal hModule As Integer,
- ByVal lpFilename As String,
- ByVal nSize As Integer) As Integer
-
- Const GCW_HMODULE = (-16)
-
- We need to use GetClassWord to obtain a handle to our module. This is the
- first parameter required by GetModuleFileName. In addition, we need
- to prepare a return buffer long enough to hold our EXE name:
-
- MyEXEName$ = String$(127, 0)
- hMymodule% = GetClassWord(MyForm.Hwnd, GCW_HMODULE)
- x% = GetModuleFileName(hMyModule%, MyEXEName$, Len(MyEXEName$))
-
- After running this code sample, MyEXEName$ will contain the full path and
- name of our EXE. This is demonstrated in MOREAPI.MAK. MOREAPI.EXE returns
- the correct drive, path and EXE name.
-
- *****************************************************************************
-
- 2. Using WVSPrintF for formatting or conversion.
-
- The API function WVSPrintF can come in handy in several situations. It is
- documented in the Windows Programmer's reference but here are some examples:
-
- Place this in your global module or the general/declarations section:
-
- Declare Function WVSPrintF Lib "User" (ByVal lpOutput As String,
- ByVal lpFormat As String,
- lpArglist As Integer) As Integer
-
- The first parameter is a return buffer that we prepare, the second one is the
- string with all the formatting codes, and the third parameter is an integer
- or the first member of an array of integers containing the values to be
- formatted/converted.
-
- a. This will convert a signed integer to an unsigned integer:
-
- Buf$ = String$(128, 0)
- x% = WVSPrintF(Buf$, "The unsigned value of -29108 is %u", -29108)
-
- We could also turn this into a function:
-
- Function MakeUnsignedInt (SignedInt As Integer) As Long
- Buf$ = String$(30, 0)
- x% = WVSPrintF(Buf$, "%u", SignedInt)
- MakeUnsignedInt = Val(Buf$)
- End Function
-
- b. This will return the Hexadecimal representation of an integer:
-
- Buf$ = String$(128, 0)
- x% = WVSPrintF(Buf$, "Integer 1991 is Hex %X", 1991) ' Hex 7C7
-
- Alternatively we can use:
-
- x% = WVSPrintF(Buf$, "Integer 1991 is Hex %04X", 1991) ' Hex 07C7
-
- The "04" here between "%" and "X" instructs WVSPrintF to pad the left of
- the output string with zeros and that the expected output width is four
- characters.
-
- c. An array of integers can be converted or formatted with WVSPrintF:
-
- Buf$ = String$(128, 0)
- ReDim MyArray%(2)
- MyArray%(1) = 42
- MyArray%(2) = 43
- x% = WVSPrintF(Buf$, "Decimal 42 is Hex %X" + Chr$(10) +
- "Decimal 43 is Hex %X", MyArray%(1))
-
- d. In addition we can fake the MakeLong macro that VB doesn't have by
- passing two integers and letting the API create the union:
-
- Buf$ = String$(128, 0)
- ReDim MakeLongArray%(2)
- MakeLongArray%(1) = 2 'Low Word
- MakeLongArray%(2) = 23 'High Word
- x% = WVSPrintF(Buf$, "Low Word = 2, High Word = 23" + Chr$(10) +
- "Makelong = %li", MakeLongArray%(1))
-
- e. Or WVSPrintF can be used to create a HEX dump:
-
- AString$ = "Visual Basic" ' Sample string
- Dump$ = Space$(3 * Len(AString$)) ' Buffer for formatting codes
- ReDim IntArray%(Len(AString$)) ' Int Array to hold AString$
- For i% = 1 To Len(AString$)
- IntArray%(i%) = Asc(Mid$(AString$, i%, 1))
- Mid$(Dump$, ((i% - 1) * 3 + 1), 3) = "%X "
- Next i%
- Buf$ = String$(Len(Dump$), 0) ' Return Buffer
- x% = WVSPrintF(Buf$, Dump$, IntArray%(1))
-
- All these WVSPrintF uses are demonstrated in MOREAPI.MAK.
-
- ****************************************************************************
-
- 3. The many ways to Exit Windows.
-
- There are times when your Application may make changes to the Windows'
- drivers or WIN.INI that in order to take effect a WM_WININICHANGE message
- won't do. Well, there are two ways to accomplish this with the same API
- function.
-
- Declare Function ExitWindows Lib "User" (ByVal dwReserved As Long,
- ByVal wReturnCode As Integer) As Integer
-
- Please note: the declaration for ExitWindows is wrong in WINAPI.TXT. The
- correct declaration should pass wReturnCode ByVal.
-
- Normal Exit / Go to DOS
-
- Okay% = ExitWindows(0, 0)
-
- This call will terminate the Windows session and return the user to DOS.
-
-
- Magic Exit / Restart Windows
-
- I don't take credit for discovering this. Credit goes to SP Services for
- discovering and sharing this with Windows programmers.
-
- Besides the normal exit, the API also accepts an undocumented value passed
- to dwReserved that Restarts the windows session. This value is &H42.
-
- Okay% = ExitWindows(&H42, 0)
-
- This will cause Windows to re-initialize without returning the user to the
- DOS prompt. It works the same at the Setup App when run from inside Windows.
-
- ExitWindows is demonstrated in MOREAPI.MAK
-
- ****************************************************************************
-
- 4. New line can be Chr$(10).
-
- You'll notice that through out MOREAPI I only use Chr$(10) to create a new line
- instead of the recommended Chr$(13) + Chr$(10). So far this has been working
- well. I don't know if future versions of Windows will support this.
-
- ****************************************************************************
-
- That's all.
-
- Enjoy.
-
- Costas Kitsos
- September 1991
-