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 / apicvt.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-17  |  16.2 KB  |  480 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmCVT 
  4.    Caption         =   "VB Declaration File to Database Converter"
  5.    ClientHeight    =   3645
  6.    ClientLeft      =   1095
  7.    ClientTop       =   1770
  8.    ClientWidth     =   6120
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   8.25
  12.       Charset         =   0
  13.       Weight          =   700
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    ForeColor       =   &H80000008&
  19.    LinkTopic       =   "Form1"
  20.    PaletteMode     =   1  'UseZOrder
  21.    ScaleHeight     =   3645
  22.    ScaleWidth      =   6120
  23.    Begin VB.CheckBox chkAPICat 
  24.       Caption         =   "Use Win32API Categories"
  25.       Height          =   315
  26.       Left            =   2280
  27.       TabIndex        =   8
  28.       Top             =   2220
  29.       Value           =   1  'Checked
  30.       Width           =   2595
  31.    End
  32.    Begin VB.CheckBox chkUpdate 
  33.       Caption         =   "Update Mode"
  34.       Height          =   255
  35.       Left            =   300
  36.       TabIndex        =   7
  37.       Top             =   2220
  38.       Value           =   1  'Checked
  39.       Width           =   1695
  40.    End
  41.    Begin VB.Timer tmrConvert 
  42.       Enabled         =   0   'False
  43.       Interval        =   100
  44.       Left            =   5640
  45.       Top             =   240
  46.    End
  47.    Begin VB.CommandButton cmdGO 
  48.       Caption         =   "Go"
  49.       Enabled         =   0   'False
  50.       Height          =   495
  51.       Left            =   3240
  52.       TabIndex        =   5
  53.       Top             =   1320
  54.       Width           =   1095
  55.    End
  56.    Begin VB.Frame Frame1 
  57.       Caption         =   "Source API"
  58.       Height          =   1155
  59.       Left            =   240
  60.       TabIndex        =   2
  61.       Top             =   960
  62.       Width           =   2055
  63.       Begin VB.CheckBox chkWin16 
  64.          Caption         =   "WIN16"
  65.          Enabled         =   0   'False
  66.          Height          =   255
  67.          Left            =   120
  68.          TabIndex        =   4
  69.          Top             =   660
  70.          Width           =   1575
  71.       End
  72.       Begin VB.CheckBox chkWin32 
  73.          Caption         =   "WIN32"
  74.          Enabled         =   0   'False
  75.          Height          =   255
  76.          Left            =   120
  77.          TabIndex        =   3
  78.          Top             =   300
  79.          Value           =   1  'Checked
  80.          Width           =   1755
  81.       End
  82.    End
  83.    Begin MSComDlg.CommonDialog CMDialog1 
  84.       Left            =   5340
  85.       Top             =   720
  86.       _ExtentX        =   847
  87.       _ExtentY        =   847
  88.       _Version        =   327680
  89.       CancelError     =   -1  'True
  90.    End
  91.    Begin VB.Label lblCurrentLine 
  92.       Appearance      =   0  'Flat
  93.       BackColor       =   &H80000005&
  94.       BackStyle       =   0  'Transparent
  95.       ForeColor       =   &H80000008&
  96.       Height          =   855
  97.       Left            =   240
  98.       TabIndex        =   6
  99.       Top             =   2640
  100.       Width           =   5715
  101.    End
  102.    Begin VB.Label lblSRCFile 
  103.       BackStyle       =   0  'Transparent
  104.       Caption         =   "No Source File Specified"
  105.       Height          =   255
  106.       Left            =   240
  107.       TabIndex        =   1
  108.       Top             =   480
  109.       Width           =   4995
  110.    End
  111.    Begin VB.Label lblDBFile 
  112.       BackStyle       =   0  'Transparent
  113.       Caption         =   "No Database File Specified"
  114.       Height          =   255
  115.       Left            =   240
  116.       TabIndex        =   0
  117.       Top             =   120
  118.       Width           =   5355
  119.    End
  120.    Begin VB.Menu mnuFile 
  121.       Caption         =   "File"
  122.       Begin VB.Menu mnuSelectDB 
  123.          Caption         =   "Select Database"
  124.       End
  125.       Begin VB.Menu mnuSelectSRC 
  126.          Caption         =   "Select Source"
  127.       End
  128.    End
  129.    Begin VB.Menu CmdAbout 
  130.       Caption         =   "About"
  131.       NegotiatePosition=   3  'Right
  132.    End
  133. Attribute VB_Name = "frmCVT"
  134. Attribute VB_GlobalNameSpace = False
  135. Attribute VB_Creatable = False
  136. Attribute VB_PredeclaredId = True
  137. Attribute VB_Exposed = False
  138. ' Copyright (c) 1996 by Desaware
  139. ' Part of the Desaware API Toolkit
  140. ' All Rights Reserved
  141. Option Explicit
  142. Dim db As Database
  143. Dim tblCategories As Table
  144. Dim tblParameters As Table
  145. Dim tblConstants As Table
  146. Dim tblFunctions As Table
  147. Dim tblTypes As Table
  148. Dim APIdb As Database   ' Windows reference database
  149. Dim tblAPI As Table     ' Windows Win32API table
  150. Const WINAPIDB$ = "win32api.mdb"
  151. Const DELETEPARAMS$ = "DELETE FROM [Parameters] WHERE Parameters!FUNCTIONID = "
  152. Const DELETEPARAMS16$ = "DELETE FROM [Parameters16] WHERE Parameters16!FUNCTIONID = "
  153. Private Sub AddCurrentObject()
  154.     Dim ctr%
  155.     Dim catid%
  156.     Dim typedef$
  157.     Dim oldid&
  158.     Select Case objType
  159.         Case 0
  160.         Case 1, 2   ' Function or sub
  161.                 ' Prevent duplicates
  162.                 oldid& = 0
  163.                 tblFunctions.Seek "=", objName$
  164.                 If Not tblFunctions.NoMatch Then
  165.                     If chkUpdate.Value <> 1 Then ' Warn on duplicates
  166.                         CvtResult$ = CvtResult$ & "Duplicate Function: " & objName$ & CRLF$
  167.                         Exit Sub
  168.                     Else
  169.                         tblFunctions.Edit
  170.                         oldid& = tblFunctions!ID
  171.                     End If
  172.                 Else
  173.                     tblFunctions.AddNew
  174.                 End If
  175.                 tblFunctions!Name = objName$
  176.                 ' Filter out aliases that exactly match the function name
  177.                 If objAlias$ <> "" And objAlias$ <> objName$ Then
  178.                     tblFunctions!ALIAS = objAlias$
  179.                 Else
  180.                     tblFunctions!ALIAS = Null
  181.                 End If
  182.                 tblFunctions!LIBRARY = objLib$
  183.                 If objType = 1 Then
  184.                     tblFunctions!ISFUNCTION = False
  185.                     tblFunctions!RETURN = 0
  186.                 Else
  187.                     tblFunctions!ISFUNCTION = True
  188.                     tblFunctions!RETURN = objReturn%
  189.                 End If
  190.                 catid% = GetCategoryID(objName$, objAlias$)
  191.                 If catid% >= 0 Then
  192.                     tblFunctions.Fields("CATEGORY") = catid%
  193.                 Else
  194.                     tblFunctions.Fields("CATEGORY") = 0
  195.                 End If
  196.                 tblFunctions.Update
  197.                 tblFunctions.Bookmark = tblFunctions.LastModified
  198.                 ' Delete any existing entries for this one
  199.                 
  200.                 If oldid& > 0 Then
  201.                     If chkWin32.Value = 1 Then
  202.                         db.Execute DELETEPARAMS$ & oldid&
  203.                     Else
  204.                         db.Execute DELETEPARAMS16$ & oldid&
  205.                     End If
  206.                 End If
  207.                     
  208.                 For ctr = 1 To UBound(pDesc)
  209.                     tblParameters.AddNew
  210.                     tblParameters!Name = pDesc(ctr).ParamName
  211.                     tblParameters!Type = pDesc(ctr).paramType
  212.                     tblParameters!IsArray = pDesc(ctr).IsArray
  213.                     tblParameters!IsByVal = pDesc(ctr).IsByVal
  214.                     tblParameters!FUNCTIONID = tblFunctions!ID
  215.                     tblParameters.Update
  216.                 Next ctr
  217.                 
  218.                 
  219.         Case 3  ' Type
  220.                 ' Prevent duplicates
  221.                 typedef$ = ""
  222.                 tblTypes.Seek "=", objName$
  223.                 If Not tblTypes.NoMatch Then
  224.                     If chkUpdate.Value <> 1 Then ' Warn on duplicates
  225.                         CvtResult$ = CvtResult$ & "Duplicate Type: " & objName$ & CRLF$
  226.                         Exit Sub
  227.                     Else
  228.                         tblTypes.Edit
  229.                     End If
  230.                 Else
  231.                     tblTypes.AddNew
  232.                 End If
  233.                 tblTypes!Name = objName$
  234.                 catid% = GetCategoryID(objName$, "")
  235.                 If catid% >= 0 Then tblTypes.Fields("CATEGORY") = tblCategories!ID
  236.                 For ctr = 1 To UBound(pDesc)
  237.                     typedef$ = typedef$ & pDesc(ctr).ParamName & Chr$(10)
  238.                 Next ctr
  239.                 tblTypes!TYPEINFO = typedef$
  240.                 tblTypes.Update
  241.                 
  242.         
  243.         Case 4      ' Constant
  244.                 tblConstants.Seek "=", objName$
  245.                 If Not tblConstants.NoMatch Then
  246.                     If chkUpdate.Value <> 1 Then ' Warn on duplicates
  247.                         CvtResult$ = CvtResult$ & "Duplicate Constant: " & objName$ & CRLF$
  248.                         Exit Sub
  249.                     Else
  250.                         tblConstants.Edit
  251.                     End If
  252.                 Else
  253.                     tblConstants.AddNew
  254.                 End If
  255.                 tblConstants!Name = objName$
  256.                 tblConstants!Type = objReturn%
  257.                 tblConstants!Value = objLib$
  258.                 
  259.                 catid% = GetCategoryID(objName$, "")
  260.                 If catid% >= 0 Then tblConstants.Fields("CATEGORY") = tblCategories!ID
  261.                 tblConstants.Update
  262.     End Select
  263. End Sub
  264. ' Enable/Disable controls as needed
  265. Private Sub CheckEnables()
  266.     Dim SetEnables As Integer
  267.     If tmrConvert.Enabled Then
  268.         chkWin32.Enabled = False
  269.         chkWin16.Enabled = False
  270.         mnuFile.Enabled = False
  271.         cmdGO.Enabled = True
  272.         Exit Sub
  273.     End If
  274.         
  275.     mnuFile.Enabled = True
  276.     SetEnables = TargetDataBase$ <> "" And SourceFileName$ <> ""
  277.     chkWin32.Enabled = SetEnables
  278.     chkWin16.Enabled = SetEnables
  279.     cmdGO.Enabled = SetEnables And (chkWin32.Value = 1 Or chkWin16.Value = 1)
  280. End Sub
  281. ' In update mode, existing functions are updated according
  282. ' to the current entry - this allows update based on the
  283. ' text file.
  284. Private Sub chkUpdate_Click()
  285. End Sub
  286. Private Sub chkWin16_Click()
  287.     If chkWin16.Value = 1 Then chkWin32.Value = 0 Else chkWin32.Value = 1
  288.     CheckEnables
  289. End Sub
  290. Private Sub chkWin32_Click()
  291.     If chkWin32.Value = 1 Then chkWin16.Value = 0 Else chkWin16.Value = 1
  292.     CheckEnables
  293. End Sub
  294. Private Sub CmdAbout_Click()
  295.     frmAbout.Show 1
  296. End Sub
  297. Private Sub cmdGO_Click()
  298.     If tmrConvert.Enabled Then DoConversion 2 Else DoConversion 1
  299. End Sub
  300. ' Perform the conversion
  301. '   startit% is set 1 on first call
  302. '   startit% is set to 2 to abort
  303. Private Sub DoConversion(ByVal startit%)
  304.     Static FileHandle%
  305.     Dim src$
  306.     Dim parseres%
  307.     If startit% = 1 And tmrConvert.Enabled Then
  308.         MsgBox "File Conversion is already in progress"
  309.         Exit Sub
  310.     End If
  311.     If startit% = 1 Then
  312.         FileHandle% = FreeFile
  313.         On Error GoTo FileErr
  314.         Open SourceFileName$ For Input Access Read As #FileHandle%
  315.         On Error GoTo 0
  316.         tmrConvert.Enabled = True
  317.         cmdGO.Caption = "Stop"
  318.         CheckEnables
  319.         CvtResult$ = ""
  320.         Set tblCategories = db.OpenTable("Categories")
  321.         tblCategories.Index = "DESCRIPTION"
  322.         If chkWin32.Value = 1 Then
  323.             Set tblParameters = db.OpenTable("Parameters")
  324.             Set tblTypes = db.OpenTable("WIN32 Types")
  325.             Set tblFunctions = db.OpenTable("WIN32 Functions")
  326.             Set tblConstants = db.OpenTable("WIN32 Constants")
  327.         Else
  328.             Set tblParameters = db.OpenTable("Parameters16")
  329.             Set tblTypes = db.OpenTable("WIN16 Types")
  330.             Set tblFunctions = db.OpenTable("WIN16 Functions")
  331.             Set tblConstants = db.OpenTable("WIN16 Constants")
  332.         End If
  333.         tblTypes.Index = "NAME"
  334.         tblFunctions.Index = "NAME"
  335.         tblConstants.Index = "NAME"
  336.         tblParameters.Index = "FUNCTIONID"
  337.     End If
  338.     If tmrConvert.Enabled = False Then Exit Sub
  339.     If EOF(FileHandle%) Or startit% = 2 Then
  340.         Close FileHandle%
  341.         ' Process rest of end conditon
  342.         tmrConvert.Enabled = False
  343.         cmdGO.Caption = "Go"
  344.         CheckEnables
  345.         
  346.         Set tblCategories = Nothing
  347.         Set tblParameters = Nothing
  348.         Set tblTypes = Nothing
  349.         Set tblFunctions = Nothing
  350.         Set tblConstants = Nothing
  351.         If CvtResult$ <> "" Then frmResults.Show 1
  352.         Exit Sub
  353.     End If
  354.         
  355.     Line Input #FileHandle%, src$
  356.     lblCurrentLine.Caption = src$
  357.     lblCurrentLine.Refresh
  358.     parseres% = am1ParseLine(src$)
  359.     Select Case parseres%
  360.         Case 0  ' Object not complete
  361.         Case 1  ' Add information to database here
  362.                 AddCurrentObject
  363.         
  364.         Case 2  ' Syntax error
  365.                 CvtResult$ = CvtResult$ & src$ & CRLF$
  366.     End Select
  367.     Exit Sub
  368. FileErr:
  369.     MsgBox Error
  370.     Exit Sub
  371. End Sub
  372. Private Sub Form_Load()
  373.     Dim apidbase$
  374.     CRLF$ = Chr$(13) & Chr$(10)
  375.     apidbase$ = App.Path
  376.     If Right$(apidbase$, 1) <> "\" Then apidbase$ = apidbase$ & "\"
  377.     apidbase$ = apidbase$ & WINAPIDB$
  378.     If Len(Dir$(apidbase$)) > 0 And chkAPICat.Value = 1 Then
  379.         Set APIdb = OpenDatabase(apidbase$)
  380.         Set tblAPI = APIdb.OpenTable("WIN32API")
  381.         tblAPI.Index = "Element Name"
  382.     End If
  383.     am1Initialize
  384. End Sub
  385. '   Attempt to retreive a category name from the WinAPI reference
  386. Private Function GetAPICategory$(apiname$, aliasname$)
  387.     tblAPI.Seek "=", apiname$
  388.     If tblAPI.NoMatch Then
  389.         GetAPICategory$ = ""
  390.         Exit Function
  391.     End If
  392.     tblAPI.Edit
  393.     If Not IsNull(tblAPI!Subsystem) Then
  394.         GetAPICategory$ = tblAPI!Subsystem
  395.     Else
  396.         If Len(aliasname$) > 0 Then
  397.             tblAPI.Seek "=", aliasname$
  398.             If tblAPI.NoMatch Then
  399.                 GetAPICategory$ = ""
  400.             Else
  401.                 tblAPI.Edit
  402.                 If Not IsNull(tblAPI!Subsystem) Then GetAPICategory$ = tblAPI!Subsystem Else GetAPICategory$ = ""
  403.             End If
  404.         Else
  405.             GetAPICategory$ = ""
  406.         End If
  407.     End If
  408. End Function
  409. ' Returns the category ID to use, -1 if none
  410. Private Function GetCategoryID(ByVal elementname$, ByVal aliasname$) As Long
  411.     If chkAPICat.Value = 1 Then
  412.         If tblAPI Is Nothing Then
  413.             GetCategoryID = -1
  414.             Exit Function
  415.         End If
  416.         Category$ = GetAPICategory$(elementname$, aliasname$)
  417.     End If
  418.     If Category$ <> "" Then
  419.         tblCategories.Seek "=", Category$
  420.         If tblCategories.NoMatch Then
  421.             tblCategories.AddNew
  422.             tblCategories!Description = Category$
  423.             tblCategories.Update
  424.             tblCategories.Bookmark = tblCategories.LastModified
  425.         End If
  426.         GetCategoryID = tblCategories!ID
  427.     Else
  428.         GetCategoryID = -1
  429.     End If
  430. End Function
  431. Private Sub mnuSelectDB_Click()
  432.     Dim f$
  433.     CMDialog1.Filter = "Database|*.MDB"
  434.     CMDialog1.Flags = OFN_CREATEPROMPT Or OFN_HIDEREADONLY
  435.     CMDialog1.DefaultExt = "*.MDB"
  436.     On Error GoTo cancelled
  437.     CMDialog1.Action = 1
  438.     f$ = CMDialog1.filename
  439.     On Error GoTo NoTemplate
  440.     If Len(Dir$(f$)) = 0 Then   ' New file
  441.         FileCopy App.Path & "\" & "apitmpl.mdb", f$
  442.     End If
  443.     On Error GoTo NoDatabase
  444.     Set db = OpenDatabase(f$)
  445.     TargetDataBase$ = f$
  446.     lblDBFile.Caption = "Database: " & TargetDataBase$
  447.     CheckEnables
  448.     Exit Sub
  449. NoDatabase:
  450.     MsgBox "Unable to open specified database"
  451.     Resume cancelled
  452. NoTemplate:
  453.     MsgBox "Unable to copy from template database"
  454. cancelled:
  455.     lblDBFile.Caption = "No Database File Specified"
  456.     Exit Sub
  457. End Sub
  458. Private Sub mnuSelectSRC_Click()
  459.     Dim f$
  460.     CMDialog1.Filter = "Text files|*.TXT|VB Modules|*.BAS"
  461.     CMDialog1.Flags = OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY
  462.     CMDialog1.DefaultExt = "*.TXT"
  463.     On Error GoTo cancelled2
  464.     CMDialog1.Action = 1
  465.     f$ = CMDialog1.filename
  466.     SourceFileName$ = f$
  467.     lblSRCFile.Caption = "Source File: " & SourceFileName$
  468.     CheckEnables
  469.     Exit Sub
  470. cancelled2:
  471.     lblSRCFile.Caption = "No Source File Specified"
  472.     Exit Sub
  473. End Sub
  474. Private Sub tmrConvert_Timer()
  475.     Dim cnt%
  476.     For cnt% = 1 To 10
  477.         DoConversion 0
  478.     Next cnt%
  479. End Sub
  480.