home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / desaware / apitools / listapi.bas < prev    next >
Encoding:
BASIC Source File  |  1997-02-13  |  16.4 KB  |  450 lines

  1. Attribute VB_Name = "modListAPI"
  2. ' Copyright ⌐ 1996 by Desaware Inc.
  3. ' Part of the Desaware API Toolkit
  4. ' All Rights Reserved
  5.  
  6. Option Explicit
  7. Public NL As String
  8. Public lstHwnd&
  9.  
  10.  
  11. ' constants for the cmbType combo box
  12. Public Const CMB_DECLARES = 0
  13. Public Const CMB_TYPES = 1
  14. Public Const CMB_CONSTANTS = 2
  15. Public which%  'Variable that holds one of the above constants
  16.  
  17. ' Which API set should I copy?
  18. Public Const OPT_WIN16 = 0
  19. Public Const OPT_WIN32 = 1
  20. Public Const OPT_BOTH = 2
  21.  
  22. 'Variables containing options
  23. Public options% 'OPT_WIN16, OPT_WIN32, or OPT_BOTH
  24. Public Comments% ' Should I insert comments?
  25. Public Glbl% ' Should I put "public" in front of constants?
  26. Public Searching%
  27.  
  28. 'Addin stuff
  29. Public IsAddin%
  30. Public objVBInst As Object
  31. Public AlreadyCopied% 'Were the selected items already copied to the clipboard?
  32.                       ' Used in the mnuFileExit  procedure
  33.  
  34. Public DeclaresSelected() As DefInfo  'Structures that contain the selected elements
  35. Public TypesSelected() As DefInfo     'These are used to fill the lstSelected list box.
  36. Public ConstantsSelected() As DefInfo
  37.  
  38. ' Number of entries in the corresponding FOOBARselected() array
  39. Public TypesNo&
  40. Public DeclaresNo&
  41. Public ConstantsNo&
  42.  
  43. ' Structure datatype.
  44. Public Type DefInfo
  45.     Free As Integer 'Whether this entry has been freed by a removal
  46.     DefName As String ' The name of the type/constant/function
  47. End Type
  48.  
  49.  
  50. 'Constants/functions used to find strings in the lstSelected listbox
  51. Public Const LB_FINDSTRINGEXACT = &H1A2
  52. Public Const WM_KEYDOWN& = &H100&
  53. Public Const WM_KEYUP& = &H101&
  54. Public Const WM_CHAR& = &H102&
  55.  
  56.  
  57. Public Declare Function SendMessage& Lib "user32" Alias "SendMessageA" (ByVal hwnd&, ByVal wMsg&, ByVal wParam&, lParam As Any)
  58. Public Declare Function OSWritePrivateProfileString% Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal AppName$, ByVal KeyName$, ByVal keydefault$, ByVal FileName$)
  59. Public Declare Function SendMessageByString& Lib "user32" Alias "SendMessageA" (ByVal hwnd&, ByVal wMsg&, ByVal wParam&, ByVal lParam$)
  60. Public Declare Function GetFocus& Lib "user32" ()
  61.  
  62. Sub Main()
  63.    frmAPI.Show
  64. End Sub
  65.  
  66. ' Adds an item from the string to the
  67. ' lstSelected listbox. Adds the new entry to the
  68. ' array.
  69. ' This function is called from cmdQuery_Click
  70. Public Sub QueryAddItem(myArray() As DefInfo, myCount&, myName$)
  71.     Dim WhichFree& ' The index of the first free element in the array.
  72.     Dim i&
  73.     
  74.     ' If there is aready that string in the lstSelected list box, return an error.
  75.     If SendMessageByString&(frmAPI.lstSelected.hwnd, LB_FINDSTRINGEXACT, 0, _
  76.                 myName$) >= 0 Then
  77. '        Beep
  78. '        MsgBox myName$ & ": That entry is already selected.", 48, "API Copier"
  79.         Exit Sub
  80.     End If
  81.     
  82.     WhichFree& = -1 ' Nothing yet.
  83.  
  84.     For i& = 0 To myCount& - 1
  85.         If myArray(i&).Free = True Then ' If it's free then...
  86.             WhichFree& = i& ' We found it!
  87.             Exit For
  88.         End If
  89.     Next i&
  90.  
  91.     If WhichFree& = -1 Then  ' If we didn't find any extra holes,
  92.         ReDim Preserve myArray(myCount&) ' We add one more element to the end of the array.
  93.         WhichFree& = myCount& ' The last one is the free element
  94.         myCount& = myCount& + 1 ' Increment count
  95.     End If
  96.  
  97.     myArray(WhichFree&).Free = False ' It's not free anymore
  98.     myArray(WhichFree&).DefName = myName$ 'Set the name field of the structure
  99.     frmAPI.lstSelected.AddItem myArray(WhichFree&).DefName ' Add it to the listbox
  100.     frmAPI.lstSelected.ItemData(frmAPI.lstSelected.NewIndex) = _
  101.                     WhichFree& ' Link the element number to the listbox item.
  102. End Sub
  103. ' Adds an item from the current record of the database to the
  104. ' lstSelected listbox. Adds the new entry to the
  105. ' array.
  106. ' This function is called from cmdAdd_click
  107. Public Sub AAddItem(myArray() As DefInfo, myCount&)
  108.     UpdateBoundField ' Make sure dataAPI2 is up to date - 4/14/96 DSA
  109.     
  110.     ' If there is aready that string in the lstSelected list box, return an error.
  111.     If SendMessageByString&(frmAPI.lstSelected.hwnd, LB_FINDSTRINGEXACT, 0, _
  112.                 CStr(frmAPI.dataAPI2.Recordset("NAME").Value)) >= 0 Then
  113.         Beep
  114.         MsgBox "That entry is already selected.", 48, "API Copier"
  115.         Exit Sub
  116.     End If
  117.     AlreadyCopied% = False
  118.     Dim WhichFree& ' The index of the first free element in the array.
  119.     Dim i&
  120.     WhichFree& = -1 ' Nothing yet.
  121.  
  122.     For i& = 0 To myCount& - 1
  123.         If myArray(i&).Free = True Then ' If it's free then...
  124.             WhichFree& = i& ' We found it!
  125.             Exit For
  126.         End If
  127.     Next i&
  128.  
  129.     If WhichFree& = -1 Then  ' If we didn't find any extra holes,
  130.         ReDim Preserve myArray(myCount&) ' We add one more element to the end of the array.
  131.         WhichFree& = myCount& ' The last one is the free element
  132.         myCount& = myCount& + 1 ' Increment count
  133.     End If
  134.  
  135.     myArray(WhichFree&).Free = False ' It's not free anymore
  136.     myArray(WhichFree&).DefName = CStr(frmAPI.dataAPI2.Recordset("Name").Value) 'Set the name field
  137.                                                                          'Of the structure
  138.     frmAPI.lstSelected.AddItem myArray(WhichFree&).DefName ' Add it to the listbox
  139.     frmAPI.lstSelected.ItemData(frmAPI.lstSelected.NewIndex) = WhichFree& ' Link the element number
  140.                                                             ' To the listbox item.
  141. End Sub
  142.  
  143.  
  144.  
  145.  
  146. ' Remove an item from the lstSelected listbox and the array
  147. Public Sub ARemoveItem(myArray() As DefInfo, myCount&, Index&)
  148.     Dim ArrayIndex&, i&
  149.     ArrayIndex& = frmAPI.lstSelected.ItemData(Index&)
  150.     myArray(ArrayIndex&).Free = True ' Free it.
  151.     frmAPI.lstSelected.RemoveItem Index&
  152.     AlreadyCopied% = False
  153. End Sub
  154.  
  155.  
  156. Public Function CopyConstants%(tableName$, clipstring$)
  157.     Dim tblConsts As Object, i&, smthCopied%
  158. '**************************************
  159. '* WINDOWS CONSTANTS
  160.     DoEvents
  161.     Set tblConsts = frmAPI.dataAPI.Database.OpenTable(tableName$ + " Constants")
  162.     tblConsts.Index = "NAME"
  163.  
  164.     For i& = 0 To ConstantsNo& - 1 'Loop for every constant without the "Free" flag.
  165.         frmAPI.pctStatus.Line (0, 0)-(i& / 3 / ConstantsNo& * frmAPI.pctStatus.Width + frmAPI.pctStatus.Width / 3, frmAPI.pctStatus.Height), QBColor(9), BF
  166.         If ConstantsSelected(i&).Free = False Then
  167.             smthCopied = True
  168.             tblConsts.Seek "=", ConstantsSelected(i&).DefName
  169.             If tblConsts.NoMatch Then ' Add appropriate comment IF neccessary
  170.                 If Comments% Then
  171.                     clipstring$ = clipstring$ & "'Const " & ConstantsSelected(i&).DefName & " is not available "
  172.                     clipstring$ = clipstring$ & "in the " & tableName$ & " API." & NL
  173.                 End If
  174.             Else
  175.  
  176.                 'Check the options: If Glbl%, then add "PUBLIC" to constant definition.
  177.                 If Glbl% = True Then
  178.                     clipstring$ = clipstring$ & "Public "
  179.                 Else
  180.                     clipstring$ = clipstring$ & "Private "
  181.                 End If
  182.  
  183.                 clipstring$ = clipstring$ & "Const "
  184.                 clipstring$ = clipstring$ & tblConsts("NAME")
  185.  
  186.                 Select Case tblConsts("TYPE")
  187.                     Case 0: 'Variant
  188.                     Case 1: 'Integer
  189.                         clipstring$ = clipstring$ & "%"
  190.                     Case 2: 'Long
  191.                         clipstring$ = clipstring$ & "&"
  192.                     Case 3: 'single
  193.                         clipstring$ = clipstring$ & "!"
  194.                     Case 4: 'double
  195.                         clipstring$ = clipstring$ & "#"
  196.                     Case 5: 'currency
  197.                         clipstring$ = clipstring$ & "@"
  198.                     Case 6: 'string
  199.                         clipstring$ = clipstring$ & "$"
  200.                 End Select
  201.                 'Extract the value from the table and paste it onto the end.
  202.                 clipstring$ = clipstring$ & " = " & tblConsts("VALUE") & NL
  203.             End If 'Nomatch
  204.             End If ' IsFree
  205.        Next i&
  206.     CopyConstants% = smthCopied%
  207. End Function
  208.  
  209. Public Function CopyFunctions%(tableName$, clipstring$)
  210.     Dim tblFuncs As Object, i&, smthCopied%
  211. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  212. ''''''' WINDOWS FUNCTIONS
  213.     frmAPI.Status = "Copying & " & tableName$ & " Functions..."
  214.     DoEvents
  215.     Set tblFuncs = frmAPI.dataAPI.Database.OpenTable(tableName$ + " Functions")
  216.     tblFuncs.Index = "NAME"
  217.     For i& = 0 To DeclaresNo& - 1
  218.         If DeclaresSelected(i&).Free = False Then
  219.             smthCopied = True
  220.             tblFuncs.Seek "=", DeclaresSelected(i&).DefName
  221.             If tblFuncs.NoMatch Then
  222.                 If Comments% Then
  223.                     clipstring$ = clipstring$ & "'Function " & DeclaresSelected(i&).DefName & " is not available "
  224.                     clipstring$ = clipstring$ & "in the " & tableName$ & " API." & NL
  225.                 End If
  226.             Else
  227.                 ' Do either private or public
  228.                 If Glbl% = True Then
  229.                     clipstring$ = clipstring$ & "Public "
  230.                 Else
  231.                     clipstring$ = clipstring$ & "Private "
  232.                 End If
  233.  
  234.                 ' Run the "GetDeclaration" function that actually generates the full text.
  235.                 clipstring$ = clipstring$ & GetDeclaration(tableName$, DeclaresSelected(i&).DefName) & NL
  236.             End If
  237.         End If
  238.         frmAPI.pctStatus.Line (0, 0)-(i& / DeclaresNo& / 3 * frmAPI.pctStatus.Width + frmAPI.pctStatus.Width * 2 / 3, frmAPI.pctStatus.Height), QBColor(9), BF
  239.     Next i&
  240.     CopyFunctions% = smthCopied%
  241. End Function
  242. Public Function CopyTypes%(tableName$, clipstring$)
  243.     Dim tblTypes As Object, i&, j%, pstring$, smthCopied%
  244. '**************************************
  245. '* Types:
  246.     frmAPI.Status = "Copying & " & tableName$ & " Types..."
  247.     DoEvents
  248.     Set tblTypes = frmAPI.dataAPI.Database.OpenTable(tableName$ + " Types") ' Open the table.
  249.     tblTypes.Index = "NAME" ' Search by name.
  250.     For i& = 0 To TypesNo& - 1
  251.         j% = 0
  252.         frmAPI.pctStatus.Line (0, 0)-(i& / 3 / TypesNo& * frmAPI.pctStatus.Width, frmAPI.pctStatus.Height), QBColor(9), BF
  253.         If TypesSelected(i&).Free = False Then
  254.             smthCopied = True
  255.             tblTypes.Seek "=", TypesSelected(i&).DefName ' Get record.
  256.             If tblTypes.NoMatch Then
  257.                 If Comments% Then
  258.                     clipstring$ = clipstring$ & "'TYPE " & TypesSelected(i&).DefName & " is not available "
  259.                     clipstring$ = clipstring$ & "in the " & tableName$ & " API." & NL
  260.                 End If
  261.             Else
  262.                 'Public or private
  263.                 If Glbl% = True Then
  264.                     clipstring$ = clipstring$ & "Public "
  265.                 Else
  266.                     clipstring$ = clipstring$ & "Private "
  267.                 End If
  268.  
  269.                 'Add the "Type FOOBAR" header to the string
  270.                 clipstring$ = clipstring$ & "Type " & TypesSelected(i&).DefName & NL
  271.  
  272.                 'Get the first line of the type.
  273.                 pstring$ = ParseAnyString$(tblTypes("TypeInfo"), j%, Chr$(10))
  274.  
  275.                 ' put the TAB (Chr$(9)) Character before every line.
  276.                 While pstring$ <> ""
  277.                     clipstring$ = clipstring$ + Chr$(9) & pstring$ & NL
  278.                     j% = j% + 1
  279.                     pstring$ = ParseAnyString$(tblTypes("TypeInfo"), j%, Chr$(10))
  280.                 Wend
  281.  
  282.                 'Add the "END TYPE" string and two newlines.
  283.                 clipstring$ = clipstring$ + "End Type" & NL & NL
  284.             End If
  285.         End If
  286.     Next i&
  287.     CopyTypes% = smthCopied%
  288. End Function
  289.  
  290.  
  291. ' Removes all the items in lstSelected and reloads it from
  292. ' the DefInfo array give as an argument
  293. '
  294. ' All array elements marked "free" are discarded.
  295. '
  296. '
  297. Public Sub ReloadSelectedList(myArray() As DefInfo, myCount&)
  298. '    Dim i&
  299. '    While lstSelected.ListCount
  300. '        lstSelected.RemoveItem 0
  301. '    Wend
  302. '    If myCount& = 0 Then Exit Sub
  303. '    For i& = 0 To myCount& - 1
  304. '        If myArray(i&).Free = False Then
  305. '            lstSelected.AddItem myArray(i&).DefName
  306. '        End If
  307. '    Next i&
  308. End Sub
  309.  
  310. ' Parses a declaration based on database entries.
  311. ' Passed TableName$: Either WIN32 or WIN16
  312. '        FuncName$: The name of the function to generate the declaration for.
  313. '
  314. '
  315. Public Function GetDeclaration$(tableName$, FuncName$)
  316.     Dim s$
  317.     Dim quote$
  318.     Dim FirstParam%, ParamCount%
  319.     Dim isfunction%
  320.     Dim dll$
  321.     Dim tblParams As Object, tblFunctions As Object
  322.     quote$ = """"
  323.     Set tblFunctions = frmAPI.dataAPI.Database.OpenTable(tableName$ + " Functions")
  324.     tblFunctions.Index = "name"
  325.     tblFunctions.Seek "=", FuncName$
  326.     If tableName$ = "WIN32" Then
  327.         Set tblParams = frmAPI.dataAPI.Database.OpenRecordset("SELECT * FROM [Parameters] WHERE FUNCTIONID = " & tblFunctions("ID"))
  328.     Else
  329.         Set tblParams = frmAPI.dataAPI.Database.OpenRecordset("SELECT * FROM [Parameters16] WHERE FUNCTIONID = " & tblFunctions("ID"))
  330.     End If
  331.     s$ = "Declare "
  332.  
  333.     'Function or sub
  334.     If tblFunctions("IsFunction") = "True" Then
  335.         s$ = s$ & "Function "
  336.         isfunction% = True
  337.     Else
  338.         s$ = s$ & "Sub "
  339.     End If
  340.  
  341.     s$ = s$ & FuncName$
  342.     ' Add the Function Return Type-Declaration char
  343.     If isfunction% Then
  344.         Select Case tblFunctions("Return")
  345.             Case "1"
  346.                 s$ = s$ & "%"
  347.             Case "2"
  348.                 s$ = s$ & "&"
  349.             Case "3"
  350.                 s$ = s$ & "!"
  351.             Case "4"
  352.                 s$ = s$ & "#"
  353.             Case "5"
  354.                 s$ = s$ & "@"
  355.             Case "6"
  356.                 s$ = s$ & "$"
  357.         End Select
  358.     End If
  359.  
  360.     s$ = s$ & " Lib "
  361.     dll$ = tblFunctions("LIBRARY") ' Get dll name
  362.     dll$ = LCase$(dll$)
  363.     Select Case dll$
  364.         Case "kernel32"
  365.         Case "user32"
  366.         Case "gdi32"
  367.         Case "gdi"
  368.         Case "kernel"
  369.         Case "user"
  370.         Case Else
  371.             If InStr(dll$, ".") = 0 Then
  372.                 dll$ = dll$ & ".dll"
  373.             End If
  374.     End Select
  375.     ' Add "libname"
  376.     s$ = s$ & quote$ & dll$ & quote$
  377.     If Len(tblFunctions("Alias")) > 0 Then ' Alias
  378.         s$ = s$ & " Alias " & quote$ & tblFunctions("ALIAS") & quote$
  379.     End If
  380.     s$ = s$ & " ("
  381.     ' Now add parameters
  382.     If tblParams.RecordCount > 0 Then tblParams.MoveFirst
  383.     ParamCount% = 0
  384.     FirstParam% = True ' Read the parameters data base
  385.     Do While Not tblParams.EOF
  386.         If Not FirstParam% Then s$ = s$ & ", "
  387.         ParamCount% = ParamCount% + 1
  388.         FirstParam% = False
  389.         If tblParams("ISBYVAL") = "True" Then s$ = s$ & "ByVal "
  390.         s$ = s$ & tblParams("NAME")
  391.         If tblParams("ISARRAY") = "True" Then s$ = s$ & "()"
  392.         s$ = s$ & " As " & tblParams("TYPE")
  393.         tblParams.MoveNext
  394.     Loop
  395.     s$ = s$ & ")"
  396.     If isfunction% And tblFunctions("RETURN") = 7 Then
  397.         s$ = s$ & " As Object"
  398.     End If
  399.     GetDeclaration$ = s$
  400. End Function
  401.  
  402. 'Extracts the idx%'th string from source$, where the
  403. 'substrings are separated by character sep$
  404. 'idx%=0 is the first string
  405. Function ParseAnyString$(Source$, ByVal idx%, ByVal sep$)
  406.     Dim nexttab%, basepos%, thispos%
  407.     Dim res$
  408.     basepos% = 1
  409.     thispos% = 0
  410.     If (Len(Source$) = 0) Then
  411.         ParseAnyString$ = ""
  412.         Exit Function
  413.     End If
  414.     Do
  415.         nexttab% = InStr(basepos%, Source$, sep$)
  416.         If nexttab% = 0 Then nexttab% = Len(Source$) + 1
  417.         'Now points to next tab or 1 past end of string
  418.         'The following should never happen
  419.         'If nexttab% = basepos% Then GoTo ptsloop1
  420.  
  421.         If thispos% = idx% Then
  422.             If nexttab% - basepos% - 1 < 0 Then
  423.                 res$ = ""
  424.             Else
  425.                 res$ = Mid$(Source$, basepos%, nexttab% - basepos%)
  426.             End If
  427.             Exit Do
  428.         End If
  429. ptsloop1:
  430.         basepos% = nexttab% + 1
  431.         thispos% = thispos% + 1
  432.     Loop While (basepos% <= Len(Source$))
  433.     ParseAnyString$ = res$
  434. End Function
  435.  
  436. 'Updates the record when you click on the listbox.
  437. '
  438. Public Sub UpdateBoundField()
  439.    Dim target$
  440.    target = frmAPI.lstAPI.Text
  441.    If target = "" Then Exit Sub
  442.    On Error Resume Next
  443.    frmAPI.dataAPI2.Recordset.FindFirst "Name = '" & target & "'"
  444. End Sub
  445.  
  446.  
  447.  
  448.  
  449.  
  450.