Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » VB.NET » Applications » Downloads
 
Add your own
alternative version
Go to top

VB 9.0, C# 3.0 API Viewer

, 24 May 2012
This is a remake of Pramod Kumar Sing's 2002 API Viewer.
VSAPIVIEWER.zip
APIViewer12
APIViewer12
APIViewer12.sln.docstates.suo
APIViewer12.suo
APIviewer.ICO
APIViewer12.vbproj.user
bin
Debug
API
APIViewer12.exe
APIViewer12.pdb
APIViewer12.vshost.exe
APIViewer12.vshost.exe.manifest
GlassButtonVB.dll
Release
My Project
Application.myapp
Settings.settings
obj
x86
Debug
APIViewer12.exe
APIViewer12.FormSplash.resources
APIViewer12.frmAbout.resources
APIViewer12.frmViewer.resources
APIViewer12.pdb
APIViewer12.Resources.resources
APIViewer12.vbproj.GenerateResource.Cache
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
My Project.Resources.Designer.vb.dll
Release
Resources
arrow_in.png
arrow_left.png
arrow_out.png
arrow_right.png
backgroundGradient_BOB-1.png
Fullscreen.png
OilcanHarry.jpg
Imports System.IO

Namespace RSP
    Public Class ParseAPI

        Dim sOldAPI As String
        Dim sNewAPI As String
        Dim pFile As String
        Dim pReader As StreamReader

        Public Sub New(ByVal sFile As String)
            If File.Exists(sFile) Then
                pFile = sFile
            Else
                pFile = ""
            End If
        End Sub

        Public Sub GetTypeInfo()
            Dim sReturn As String = String.Empty
            Dim sCSharp As String = String.Empty
            Dim sCSharpInput As String = String.Empty
            Dim sKey As String = String.Empty
            Dim bStart As Boolean = False
            Dim bTypeStatement As Boolean = False

            If pFile <> "" Then
                If Not File.Exists(pFile) Then
                    Types = Nothing
                    Return
                End If

                Dim sr As StreamReader = File.OpenText(pFile)
                Dim input As String

                input = sr.ReadLine()

                While Not input Is Nothing
                    If bStart = False And InStr(1, input, APISTARTTYPE) <> 0 Then
                        If Not input.StartsWith("'") Then
                            If Split(input, " ").Length() <= 2 Then
                                sKey = Split(input, " ")(1).ToString()
                                sCSharpInput = input
                                input = input.Replace(APISTARTTYPE, APITYPE + APISCOPE + " Structure ").ToString()
                                sCSharpInput = sCSharpInput.Replace(APISTARTTYPE, APITYPECSHARP + vbCrLf + APISCOPE + " " + APISTRUCTURECSHARP).ToString() + APISTARTSCOPECSHARP
                                bStart = True
                                bTypeStatement = True
                            End If
                        End If
                    End If

                    If bStart Then
                        If InStr(1, input, APIENDTYPE) <> 0 Then
                            sCSharpInput = input
                            input = input.Replace(APIENDTYPE, "End Structure").ToString()
                            sCSharpInput = sCSharpInput.Replace(APIENDTYPE, APIENDSCOPECSHARP).ToString()
                            sReturn += input & vbCrLf
                            sReturn = sReturn.Replace(APILONG, APIINTEGER)
                            sCSharp += sCSharpInput & vbCrLf
                            sCSharp = sCSharp.Replace(APILONG, APIINTEGER)
                            sCSharp = ChangeDeclartion(sCSharp)
                            Types.Add(sKey, sReturn)
                            Types.Add(sKey, sCSharp, True)
                            sKey = ""
                            sReturn = ""
                            sCSharp = ""
                            bStart = False
                        Else
                            Dim bReturn As Boolean = False
                            Dim sMarshal As String = ""
                            Dim sCSharpMarshal As String = ""
                            Dim sTmp As String

                            bReturn = MarshalStringValue(input, sMarshal)

                            If bReturn Then
                                sCSharpMarshal = sMarshal.Replace("<", "[")
                                sCSharpMarshal = sCSharpMarshal.Replace(">", "]" + vbCrLf)
                                sTmp = sCSharpMarshal.Substring(sCSharpMarshal.IndexOf("]" + vbCrLf) + 1)
                                sTmp = sTmp.Replace(APISCOPE, "").Trim()
                                sTmp = ChangeNotation(sTmp)
                                sCSharp += sCSharpMarshal.Substring(0, sCSharpMarshal.IndexOf("]" + vbCrLf) + Len("]" + vbCrLf)) + APISCOPE + sTmp + APIENDSTATEMENTCSHARP
                                sReturn += sMarshal + vbCrLf
                            Else
                                bReturn = False
                                sMarshal = ""
                                bReturn = MarshalArrayValue(input, sMarshal)

                                If bReturn And (Not bTypeStatement) Then
                                    sCSharpMarshal = sMarshal.Replace("<", "[")
                                    sCSharpMarshal = sCSharpMarshal.Replace(">", "]" + vbCrLf)
                                    sTmp = sCSharpMarshal.Substring(sCSharpMarshal.IndexOf("]" + vbCrLf) + 1)
                                    sTmp = sTmp.Replace(APISCOPE, "").Trim()
                                    sTmp = ChangeNotation(sTmp)
                                    sCSharp += sCSharpMarshal.Substring(0, sCSharpMarshal.IndexOf("]" + vbCrLf) + Len("]" + vbCrLf)) + APISCOPE + sTmp + APIENDSTATEMENTCSHARP
                                    sReturn += sMarshal + vbCrLf
                                ElseIf bTypeStatement Then
                                    sCSharp += sCSharpInput
                                    sReturn += input & vbCrLf
                                Else
                                    sTmp = ChangeNotation(sCSharpInput)
                                    sCSharp += APISCOPE + sTmp & APIENDSTATEMENTCSHARP
                                    sReturn += APISCOPE + input & vbCrLf
                                End If
                            End If
                        End If
                        bTypeStatement = False
                    End If
                    input = sr.ReadLine()
                    sCSharpInput = input
                End While
                sr.Close()
                Return
            Else
                Return
            End If
        End Sub

        Public Sub GetConstInfo()
            Dim sKey As String
            Dim sCSharp As String

            If pFile <> "" Then
                If Not File.Exists(pFile) Then
                    Types = Nothing
                    Return
                End If

                Dim sr As StreamReader = File.OpenText(pFile)
                Dim input As String

                input = sr.ReadLine()

                While Not input Is Nothing
                    If InStr(1, input, APISTARTCONST) <> 0 Then
                        'Check for the Comment
                        If Not input.StartsWith("'") Then
                            'Look Ahead for false data
                            If Split(input, "=").Length() <= 2 Then
                                sKey = Split(input, " ")(1).ToString()
                                input = APISCOPE & input
                                sCSharp = input
                                sCSharp = sCSharp.Replace("Const", "const int") + ";"
                                sCSharp = sCSharp.Replace("'", "//")
                                Constants.Add(sKey, input)
                                Constants.Add(sKey, sCSharp, True)
                            End If
                        End If
                    End If
                    input = sr.ReadLine()
                End While
                sr.Close()
                Return
            Else
                Return
            End If
        End Sub

        Public Sub GetDeclareInfo()
            Dim sStructures As String
            Dim sKey As String
            Dim sCSharp As String
            Dim sCSharpStruct As String
            Dim sCSharpReturnType As String = String.Empty
            Dim sLib As String

            If pFile <> "" Then
                If Not File.Exists(pFile) Then
                    Types = Nothing
                    Return
                End If

                Dim sr As StreamReader = File.OpenText(pFile)
                Dim input As String

                input = sr.ReadLine()

                While Not input Is Nothing
                    If InStr(1, input, APISTARTDECLARE) <> 0 Then
                        'Check for the Comment
                        If Not input.StartsWith("'") Then
                            'Look Ahead for false data
                            If Split(input, " ").Length() > 5 Then
                                sKey = Split(input, " ")(2).ToString()
                                sLib = Split(input, " ")(4).ToString()
                                If InStr(sLib, ".") = 0 Then sLib = sLib.Substring(0, Len(sLib) - 1) + ".dll" + sLib.Substring(Len(sLib) - 1)
                                sStructures = ""
                                Try
                                    sStructures = input.Substring(input.IndexOf("(") + 1, ((input.IndexOf(")")) - (input.IndexOf("(") + 1)))
                                    sCSharpStruct = sStructures
                                    If Split(input, " ")(1).Trim().ToString() = "Function" Then
                                        sCSharpReturnType = Split(input.Substring(input.IndexOf(")") + 1), "As ")(1).ToString()
                                    End If
                                Catch e As Exception
                                    sStructures = ""
                                End Try

                                Dim vData() As String = sStructures.Split(",")
                                Dim vCSharpData() As String = sStructures.Split(",")
                                Dim iCount As Integer = vData.Length()
                                Dim i As Integer
                                Dim bReturn As Boolean
                                Dim sMarshalValue As String = String.Empty
                                Dim vVariable() As String

                                If iCount >= 0 Then
                                    For i = 0 To iCount - 1
                                        bReturn = False
                                        vVariable = vData(i).Trim().Split(" ")
                                        Dim sTmp As String
                                        sTmp = vCSharpData(i)
                                        sTmp = ChangeNotation(sTmp)
                                        If vVariable.Length() >= 2 Then
                                            If vVariable.Length() > 3 Then
                                                bReturn = MarshalStructValue(vVariable(3).ToString(), sMarshalValue)
                                            Else
                                                bReturn = MarshalStructValue(vVariable(1).ToString(), sMarshalValue)
                                            End If
                                        End If
                                        If bReturn Then
                                            vData(i) = sMarshalValue + " " + vData(i)
                                            vCSharpData(i) = APISTRUCTCSHARP + " " + sTmp
                                        Else
                                            vCSharpData(i) = sTmp
                                        End If
                                    Next
                                    sStructures = String.Join(",", vData)
                                    If sStructures <> "" Then
                                        input = input.Replace(input.Substring(input.IndexOf("(") + 1, ((input.IndexOf(")")) - (input.IndexOf("(") + 1))), sStructures)
                                    End If
                                End If

                                sCSharp = APIDLLIMPORTECSHARP
                                sCSharp = Replace(sCSharp, APIDLLIMPORT, sLib) + vbCrLf
                                sCSharp += APISCOPE + " " + APISTARTCSHARP + IIf(sCSharpReturnType <> "", " " & sCSharpReturnType, " void ") + " " & sKey + "(" + IIf(vCSharpData Is Nothing, " ", Join(vCSharpData, ",")) + ")" + APIENDSTATEMENTCSHARP
                                sCSharpReturnType = ""
                                sCSharp = ChangeDeclartion(sCSharp)
                                input = APISCOPE & input
                                input = input.Replace(APILONG, APIINTEGER)
                                Declares.Add(sKey, input)
                                Declares.Add(sKey, sCSharp, True)
                            End If
                        End If
                    End If
                    input = sr.ReadLine()
                End While
                sr.Close()
                Return
            Else
                Return
            End If
        End Sub

        Public Sub GetSubsInfo()
            Dim sStructures As String
            Dim sKey As String
            Dim sCSharp As String
            Dim sCSharpStruct As String
            Dim sCSharpReturnType As String = String.Empty
            Dim sLib As String

            If pFile <> "" Then
                If Not File.Exists(pFile) Then
                    Types = Nothing
                    Return
                End If

                Dim sr As StreamReader = File.OpenText(pFile)
                Dim input As String

                input = sr.ReadLine()

                While Not input Is Nothing
                    If InStr(1, input, APISTARTDECLARE) <> 0 Then
                        'Check for the Comment
                        If Not input.StartsWith("'") Then
                            'Look Ahead for false data
                            If Split(input, " ").Length() > 5 Then
                                sKey = Split(input, " ")(2).ToString()
                                sLib = Split(input, " ")(4).ToString()
                                If InStr(sLib, ".") = 0 Then sLib = sLib.Substring(0, Len(sLib) - 1) + ".dll" + sLib.Substring(Len(sLib) - 1)
                                sStructures = ""
                                Try
                                    sStructures = input.Substring(input.IndexOf("(") + 1, ((input.IndexOf(")")) - (input.IndexOf("(") + 1)))
                                    sCSharpStruct = sStructures
                                    If Split(input, " ")(1).Trim().ToString() = "Sub" Then
                                        sCSharpReturnType = Split(input.Substring(input.IndexOf(")") + 1), )(1).ToString()
                                    End If
                                Catch e As Exception
                                    sStructures = ""
                                End Try

                                Dim vData() As String = sStructures.Split(",")
                                Dim vCSharpData() As String = sStructures.Split(",")
                                Dim iCount As Integer = vData.Length()
                                Dim i As Integer
                                Dim bReturn As Boolean
                                Dim sMarshalValue As String = String.Empty
                                Dim vVariable() As String

                                If iCount >= 0 Then
                                    For i = 0 To iCount - 1
                                        bReturn = False
                                        vVariable = vData(i).Trim().Split(" ")
                                        Dim sTmp As String
                                        sTmp = vCSharpData(i)
                                        sTmp = ChangeNotation(sTmp)

                                        If vVariable.Length() >= 2 Then
                                            If vVariable.Length() > 3 Then
                                                bReturn = MarshalStructValue(vVariable(3).ToString(), sMarshalValue)
                                            Else
                                                bReturn = MarshalStructValue(vVariable(1).ToString(), sMarshalValue)
                                            End If
                                        End If

                                        If bReturn Then
                                            vData(i) = sMarshalValue + " " + vData(i)
                                            vCSharpData(i) = APISTRUCTCSHARP + " " + sTmp
                                        Else
                                            vCSharpData(i) = sTmp
                                        End If

                                    Next

                                    sStructures = String.Join(",", vData)

                                    If sStructures <> "" Then
                                        input = input.Replace(input.Substring(input.IndexOf("(") + 1, ((input.IndexOf(")")) - (input.IndexOf("(") + 1))), sStructures)
                                    End If
                                End If

                                sCSharp = APIDLLIMPORTECSHARP
                                sCSharp = Replace(sCSharp, APIDLLIMPORT, sLib) + vbCrLf
                                sCSharp += APISCOPE + " " + APISTARTCSHARP + IIf(sCSharpReturnType <> "", " " & sCSharpReturnType, " void ") + " " & sKey + "(" + IIf(vCSharpData Is Nothing, " ", Join(vCSharpData, ",")) + ")" + APIENDSTATEMENTCSHARP
                                sCSharpReturnType = ""
                                sCSharp = ChangeDeclartion(sCSharp)
                                input = APISCOPE & input
                                input = input.Replace(APILONG, APIINTEGER)
                                Torpedoes.Add(sKey, input)
                                Torpedoes.Add(sKey, sCSharp, True)
                            End If
                        End If
                    End If
                    input = sr.ReadLine()
                End While
                sr.Close()
                Return
            Else
                Return
            End If
        End Sub

        Private Function MarshalStructValue(ByVal strA As String, ByRef sReturn As String) As Boolean

            If String.Compare(strA, "Long") = 0 Then Return False
            If String.Compare(strA, "Integer") = 0 Then Return False
            If String.Compare(strA, "Byte") = 0 Then Return False
            If String.Compare(strA, "Any") = 0 Then Return False
            If String.Compare(strA, "Boolean") = 0 Then Return False
            If String.Compare(strA, "String") = 0 Then Return False
            sReturn = APISTRUCT

            Return True

        End Function

        Private Function MarshalArrayValue(ByVal strA As String, ByRef sReturn As String) As Boolean
            Dim vData As String
            Dim vVariable As String

            'Check for the Array in the structure
            If strA.IndexOf("(") > 0 Then
                'Got the Array, Marshal this Array
                vData = strA.Substring(strA.IndexOf("(") + 1, ((strA.IndexOf(")")) - (strA.IndexOf("(") + 1)))
                vVariable = APIARRAY
                vVariable = Replace(vVariable, APISIZEOFARRAY, vData)
                strA = strA.Replace(vData, "")
                strA = strA.Replace("(", "")
                strA = strA.Replace(")", "")
                sReturn = vVariable & APISCOPE & strA
                Return True
            End If

            Return False

        End Function

        Private Function MarshalStringValue(ByVal strA As String, ByRef sReturn As String) As Boolean
            Dim vData As String
            Dim vVariable As String

            'Check for the Array in the structure
            If strA.IndexOf("*") > 0 Then
                'Got the Array, Marshal this Array
                vData = strA.Substring(strA.IndexOf("*") + 1)
                vVariable = APISTRING
                vVariable = Replace(vVariable, APISIZEOFARRAY, vData)
                strA = strA.Replace(vData, "")
                strA = strA.Replace("*", "")
                strA = strA.Trim()
                sReturn = vVariable & APISCOPE & strA
                Return True
            End If

            Return False

        End Function

        Private Function ChangeDeclartion(ByVal strA As String) As String

            strA = strA.Replace(APIINTEGER, APIINTEGERCSHARP)
            strA = strA.Replace(APILONG, APIINTEGERCSHARP)
            strA = strA.Replace(APIBYTEVB, APIBYTECSHARP)
            strA = strA.Replace(APIFLOATVB, APIFLOATCSHARP)
            strA = strA.Replace(APISTRVB, APISTRCSHARP)
            strA = strA.Replace(APIANYVB, APIINTEGERCSHARP)

            Return strA

        End Function

        Private Function ChangeNotation(ByVal strA As String) As String
            Dim strOld As String

            strA = strA.Replace(APIBYREFVB, "")
            strA = strA.Replace(APIBYVALVB, "")

            If InStr(strA, "'") > 0 Then
                strA = strA.Substring(0, InStr(strA, "'") - 1).Trim()
            End If

            Dim vData() As String = Split(strA.Trim().ToString(), APISEPERATORAS)

            If vData.Length > 1 Then
                strOld = vData(vData.Length - 1) + " " + vData(vData.Length - 2)
                Return strOld
            End If

            Return ""

        End Function
    End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

rspercy65
Retired
United States United States
I am currently retired.
I have no degree but I have some programming experience
when I was in college(Cobol, Pascal).
 
My accomplishments thus far are;
Best VB.Net article for January(2009)
Best VB.Net article for July(2009)

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 24 May 2012
Article Copyright 2010 by rspercy65
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid