Here the actual Powerbasic source code for the Visual Basic code generation Plugin that comes with NPBuilder:




#COMPILE DLL "..\NPpluginvb.dll"

#DIM ALL


#RESOURCE VERSIONINFO

#RESOURCE FILEFLAGS 0

#RESOURCE FILEVERSION 1, 0, 0, 0

#RESOURCE PRODUCTVERSION 1, 0, 0, 0

#RESOURCE STRINGINFO "0409", "04B0"

#RESOURCE VERSION$ "CompanyName",      "Nippon Pulse"

#RESOURCE VERSION$ "FileDescription",  "PluginPB"

#RESOURCE VERSION$ "FILEVERSION",      "01.00.0000"

#RESOURCE VERSION$ "LegalCopyright",   "Copyright© 2022 Christopher Boss"

#RESOURCE VERSION$ "ProductVersion",   "01.00.0000"


#INCLUDE "NPplugin.inc"


FUNCTION INIT_NPTOOL(BYREF ToolInfo AS NPTOOL) EXPORT AS DWORD

     NPInitBuilderAPI ToolInfo ' required to allow calling back to Builder API

     ToolInfo.ToolName = "Visual Basic"

     ToolInfo.ToolImageID = 8

     ToolInfo.ToolLangID =  0 ' language template ID number for above text

     ToolInfo.ToolType = 2    ' code gen plugin

     ToolInfo.lpEvents = CODEPTR(MyFormEvents)

     ToolInfo.lpDesign = CODEPTR(MyEnumAddControls)

     ToolInfo.FormWidth =  65  ' -1 means default

     ToolInfo.FormHeight =  23 ' -1 means default

     FUNCTION = 1

END FUNCTION


GLOBAL VB_GenMode AS LONG

GLOBAL VB_AddSpace AS LONG


FUNCTION MyEnumAddControls(BYREF MyCtrl() AS NPCONTROL) AS LONG

     NPSetCtrlPtr VARPTR(MyCtrl(0))   ' stores pointer for shortcut API calls

     NPSetAttr  100, %NP_Arial12B,  0,  25, "CF"

     NPAddControl "LABEL", 1.25, 1.5, 62, 1.75, "Visual Basic Code Generator"

     ' -------------------------------------

     NPSetAttr  105, %NP_Arial9B, -1, -1, ""

     LOCAL CList$

     CList$ = ""

     CList$ = CList$ + "Generate Module Level Code   (declares and library functions)|"

     CList$ = CList$ + "Generate Get Device List Code|"

     CList$ = CList$ + "Generate Code Block Primary (START/END) Code|"

     CList$ = CList$ + "Generate Code Block Subroutine (SUB) Code"

     VB_GenMode = 1 ' first item

     NPAddControl "COMBOBOX", 4, 1.5, 62, 17.25, CList$

     ' -------------------------------------

     NPSetAttr  107, %NP_Arial9,  0,  15, ""

     NPAddControl "TEXTM", 6, 3.5, 58, 3.5, ""

     ' -------------------------------------

     NPSetAttr  108, %NP_Arial9,  0,  125, ""

     NPAddControl "CHECKBOX", 10.5, 3.5, 58, 1.5, "Build Strings using NP_AddVB API instead of using Visual Basic variables"

     ' -------------------------------------

     NPSetAttr  109, %NP_Arial9,  0,  125, ""

     NPAddControl "CHECKBOX", 12.25, 3.5, 58, 1.5, "Make Code Block SUB routines Private instead of Public"

     ' -------------------------------------

     NPSetAttr  110, %NP_Arial11,  0,  25, ""

     NPAddControl "ODBUTTON", 15, 1.5, 42, 2.75, "Generate Code and Copy to Windows Clipboard"

     ' -------------------------------------

     NPSetAttr  112, %NP_Arial9, -1, -1, ""

     NPAddControl "COMBOBOX", 14.75, 45, 19, 10, "NO Extra Spaces|Offset 5 Extra Spaces|Offset 10 Extra Spaces"

     VB_AddSpace = 0

     ' -------------------------------------

     NPSetAttr  113, %NP_Arial8,  0,  20, "C"

     NPAddControl "LABEL", 16.5, 45, 19, 2, "Only for:   Get Device code|or Code Block Primary code"

     ' -------------------------------------

     NPSetAttr  114, %NP_Arial11,  0,  26, ""

     NPAddControl "ODBUTTON", 19, 1.5, 42, 2.75, "Copy Runtimes (32 bit DLLs) to my VB Project"

     ' -------------------------------------

     NPSetAttr  115, %NP_Arial12,  0,  23, ""

     NPAddControl "ODBUTTON", 19.75, 56, 8, 2.75, "Exit"

     ' -------------------------------------

     NPSetCtrlPtr 0 ' turns off shortcut syntax

     FUNCTION = NPGetControlCount

END FUNCTION


FUNCTION MyFormEvents(BYVAL MyID AS LONG, BYVAL CMsg AS LONG, BYVAL CVal AS LONG, BYREF CancelEvent AS LONG) AS LONG

     SELECT CASE MyID

          CASE %NP_Form    ' parent form event

               IF CMsg = %NP_Loaded THEN

                    UpdateDescText 1

               END IF

               IF CMsg = %NP_Started THEN

               END IF

               IF CMsg = %NP_Close THEN

               END IF

          CASE 100     ' LABEL

          CASE 105     ' COMBOBOX

               IF CMsg = %NP_Change THEN

                    VB_GenMode = CVal + 1

                    UpdateDescText CVal + 1

               END IF

          CASE 110     ' ODBUTTON

               IF CMsg = %NP_Click THEN

                    DoCodeGenVB VB_GenMode

               END IF

          CASE 112  ' combobox

               IF CMsg = %NP_Change THEN

                    VB_AddSpace = CVal

               END IF

          CASE 114

               IF CMsg = %NP_Click THEN

                    NP_RunTask "COPYFILES"

               END IF

          CASE 115     ' ODBUTTON

               IF CMsg = %NP_Click THEN

                    NP_RunTask "EXIT"

               END IF

          CASE ELSE

     END SELECT

     FUNCTION = 1

END FUNCTION


'     CList$ = CList$ + "Generate Code Block Primary (START/END) Code|"

'     CList$ = CList$ + "Generate Code Block Subroutine (SUB) Code"



SUB UpdateDescText(BYVAL index AS LONG)

     LOCAL T$

     T$ = ""

     SELECT CASE index

          CASE 1

               T$ = T$ + "Generates Declares and Library functions which should be pasted" + $CRLF

               T$ = T$ + "between MODULE and END MODULE commands in a Visual Basic Module" + $CRLF

               T$ = T$ + "in your project (code is PUBLIC and visible to entire app)."

          CASE 2

               T$ = T$ + "Generates code which calls a Library Function which returns back each" + $CRLF

               T$ = T$ + "Controller Device currently available to be connected. " + $CRLF

               T$ = T$ + "Sample code to fill a ComboBox control is provided. Paste where needed."

          CASE 3

               T$ = T$ + "Generates code for all Logic Blocks in your Code Block's primary" +$CRLF

               T$ = T$ + "section between the START and END Blocks. Paste this code inside" +$CRLF

               T$ = T$ + "any subroutine in your project where it is best suited."

          CASE 4

               T$ = T$ + "Generates code for all the SUB Logic Block sections of your Code Block" +$CRLF

               T$ = T$ + "as standalone Subroutines using the name of each SUB Logic Block" +$CRLF

               T$ = T$ + "as the Subroutine Name."


          CASE ELSE

     END SELECT

     NP_SetText 107, T$

END SUB


SUB DoCodeGenVB(BYVAL GMode AS LONG)

     LOCAL SP$, SP2$, SName$, EX$

     SP$ = "    "

     SP2$ = SP$ + SP$

     EX$ = ""

     SELECT CASE GMode

          CASE 1    ' declares

               CreateDeclareModule

          CASE 2    ' get device list code

               CreateGetDevList

          CASE 3,4

               LOCAL CBlock$, T$, CData$, BS$, BN$

               LOCAL I&, CT&, PrivFlag AS LONG

               PrivFlag = NP_GetValue(109)

               LOCAL PFlag AS LONG, NSFlag AS LONG

               NSFlag = NP_GetValue(108)

               CData$ = ""

               NP_SetText %NPID_CodeBlock, "<PRG>"

               CBlock$ = NP_GetText(%NPID_CodeBlock)

               CT& = PARSECOUNT(CBlock$, CHR$(13))

               IF VB_AddSpace <> 0 THEN EX$ = SPACE$(VB_AddSpace * 5)

               IF GMode = 3 THEN GOSUB GetOpenCode

               FOR I& = 1 TO CT&

                    T$ = PARSE$(CBlock$, CHR$(13), I&)

                    SELECT CASE AS CONST$ PARSE$(T$, "|",1)

                         CASE "BSCRIPT"

                              IF PFlag = 1 THEN

                                   IF GMode = 3 THEN GOSUB GenBScript

                              ELSE

                                   IF GMode = 4 THEN GOSUB GenBScript

                              END IF

                         CASE "START"

                              PFlag = 1

                         CASE "END"

                              PFlag = 0

                              IF GMode = 3 THEN EXIT FOR

                              IF VB_AddSpace <> 0 THEN EX$ = ""

                         CASE "SUB"

                              IF GMode = 4 THEN

                                   SName$ = PARSE$(T$, "|",2)

                                   IF PrivFlag THEN

                                        CData$ = CData$ + $CRLF + "Private Sub " + SName$ + "(byval ModType as Integer, byval HIDHandle as UInteger)" + $CRLF

                                   ELSE

                                        CData$ = CData$ + $CRLF + "Public Sub " + SName$ + "(byval ModType as Integer, byval HIDHandle as UInteger)" + $CRLF

                                   END IF

                                   GOSUB GetOpenCode

                              END IF

                         CASE "END SUB"

                              IF GMode = 4 THEN

                                   GOSUB GetCloseCode

                                   CData$ = CData$ + "End Sub" + $CRLF + $CRLF

                              END IF

                         CASE "CALLSUB"

                              IF (GMode = 3) AND (PFlag = 1) THEN

                                   SName$ = PARSE$(T$, "|",2)

                                   CData$ = CData$ + EX$ + SP2$ + SName$ +"(ModType, HIDHandle)" + $CRLF

                              END IF

                              IF (GMode = 4) AND (PFlag = 0) THEN

                                   SName$ = PARSE$(T$, "|",2)

                                   CData$ = CData$ + EX$ + SP2$ + SName$ +"(ModType, HIDHandle)" + $CRLF

                              END IF

                         CASE "EMPTY"

                         CASE ELSE

                    END SELECT

               NEXT I&

               IF GMode = 3 THEN GOSUB GetCloseCode

               NP_SetText %NPID_Clipboard, CData$

          CASE ELSE

     END SELECT

     EXIT SUB


GenBScript:

     BN$ = PARSE$(T$, "|",2)

     BN$ = LEFT$(BN$, LEN(BN$) -4)

     NP_SetText %NPID_CodeBlock, "<BSC>" + T$

     BS$ = NP_GetText(%NPID_CodeBlock)

     LOCAL LP&, LPCT&, BT$, NewBS$

     LPCT& = PARSECOUNT(BS$, CHR$(13))

     NewBS$ = ""

     CData$ = CData$ + EX$ + SP2$ + "' ----- " + BN$ + " -----" + $CRLF

     IF NSFlag = 0 THEN

          CData$ = CData$ + EX$ + SP2$ + "PrgStr = " + CHR$(34) + CHR$(34) + $CRLF

     END IF

     FOR LP& =1 TO LPCT&

          BT$ = TRIM$(PARSE$(BS$, CHR$(13), LP&))

          IF LEN(BT$) <> 0 THEN

               BT$ = REMOVE$(BT$, ANY "|" + CHR$(34) )

               IF LEFT$(BT$,1) = "'" THEN    ' a comment

                    NewBS$ = NewBS$ + EX$ + SP2$ + BT$ + $CRLF

               ELSE

                    IF NSFlag THEN

                         NewBS$ = NewBS$ + EX$ + SP2$ + "NP_AddVB( " + CHR$(34) + BT$ + CHR$(34) +" )" + $CRLF

                    ELSE

                         NewBS$ = NewBS$ + EX$ + SP2$ + "PrgStr = PrgStr & " + CHR$(34) + BT$ + CHR$(34) + " & Chr(13)" + $CRLF

                    END IF

               END IF

          END IF

     NEXT LP&


     IF NSFlag THEN

          CData$ = CData$ + EX$ + SP2$ + "NP_StartPrgVB()" + $CRLF

     END IF

     CData$ = CData$ + NewBS$

     GOSUB GetRunCode

RETURN


GetOpenCode:

     IF GMode = 3 THEN

          CData$ = CData$ + EX$ + SP$ + "Dim DevName As String" + $CRLF

          CData$ = CData$ + EX$ + SP$ + "Dim ModType As Integer, HIDHandle As UInteger" + $CRLF

     END IF

     IF NSFlag = 0 THEN

          CData$ = CData$ + EX$ + SP$ + "Dim PrgStr As String" + $CRLF

     END IF

     IF GMode = 3 THEN

          CData$ = CData$ + EX$ + SP$ + "DevName = NP_GetDeviceListVB(1) ' get first device found" + $CRLF

          CData$ = CData$ + EX$ + SP$ + "ModType = NP_OpenDeviceVB(DevName, HIDHandle)" + $CRLF

     END IF

     CData$ = CData$ + EX$ + SP$ + "If ModType <> 0 Then" + $CRLF


RETURN


GetRunCode:

     IF NSFlag = 0 THEN

          CData$ = CData$ + EX$ + SP2$ + "NP_RunProgramVBcb(ModType, HIDHandle, PrgStr, 1)" + $CRLF


     ELSE

          CData$ = CData$ + EX$ + SP2$ + "NP_RunPrgVBcb(ModType, HIDHandle, 1)" + $CRLF

     END IF

RETURN


GetCloseCode:

     IF GMode = 3 THEN

          CData$ = CData$ + EX$ + SP2$ + "NP_CloseDeviceVB(ModType, HIDHandle)" + $CRLF

     END IF

     CData$ = CData$ + EX$ + SP$ + "End If" + $CRLF

RETURN


END SUB


SUB CreateGetDevList()

DATA "Dim MyDevList As String"

DATA "Dim Index As Integer"

DATA "Dim DevCT As Integer"

DATA "DevCT = 0"

DATA "' Me.ComboBox1.Items.Clear()"

DATA "For Index = 1 To 10"

DATA "     MyDevList = NP_GetDeviceListVB(Index)"

DATA "     If MyDevList = || Then Exit For"

DATA "     ' Me.ComboBox1.Items.Add(MyDevList)"

DATA "     DevCT = DevCT + 1"

DATA "Next Index"

DATA "If DevCT > 0 Then"

DATA "     ' Me.ComboBox1.SelectedIndex = 0"

DATA "End If"

     LOCAL M1$, M2$, M3$, SP$

     SP$ = "    "

     IF VB_AddSpace <> 0 THEN SP$ = SP$ + SPACE$(5 * VB_AddSpace)

     M1$ = CHR$(34)

     LOCAL CT&, I&, T$, ClipData$

     CT& = DATACOUNT

     FOR I& = 1 TO CT&

          T$ = READ$(I&)

          REPLACE "|" WITH M1$ IN T$

          ClipData$ = ClipData$ + SP$ + T$ + CHR$(13) + CHR$(10)

     NEXT I&

     NP_SetText %NPID_Clipboard, ClipData$


END SUB


SUB CreateDeclareModule()

DATA "<<1>> NP_GetDeviceListVB Lib |npbscript32.dll| Alias |NP_GetDeviceListVB| (ByVal Index As Integer) As String"

DATA "<<1>> NP_OpenDeviceVB Lib |npbscript32.dll| Alias |NP_OpenDeviceVB| (ByVal DevName As String, ByRef HIDHandle As UInteger) As Integer"

DATA "<<2>> NP_CloseDeviceVB Lib |npbscript32.dll| Alias |NP_CloseDeviceVB| (ByVal ModType As Integer, ByVal HIDHandle As UInteger)"

DATA "<<2>> NP_RunProgramVB Lib |npbscript32.dll| Alias |NP_RunProgramVB| (ByVal ModType As Integer, ByVal HIDHandle As UInteger, ByVal PrgBuffer As String, ByVal EFlag As Integer, ByVal lpEHook As UInteger, ByVal lpSHook As UInteger)"

DATA "<<2>> NP_StartPrgVB Lib |npbscript32.dll| Alias |NP_StartPrgVB| ()"

DATA "<<2>> NP_AddVB Lib |npbscript32.dll| Alias |NP_AddVB| (ByVal OneLineText As String)"

DATA "<<2>> NP_RunPrgVB Lib |npbscript32.dll| Alias |NP_RunPrgVB| (ByVal ModType As Integer, ByVal HIDHandle As UInteger, ByVal EFlag As Integer, ByVal lpEHook As UInteger, ByVal lpSHook As UInteger)"

DATA "Public Delegate Function CB_ESCHook() As UInteger"

DATA "Public Delegate Function CB_SendHook(ByVal HIDHandle As UInteger, ByVal CmdEvent As Integer, ByVal StoreMemID As Integer) As Integer"

DATA "<<2>> NP_RunProgramVB_Del Lib |npbscript32.dll| Alias |NP_RunProgramVB| (ByVal ModType As Integer, ByVal HIDHandle As UInteger, ByVal PrgBuffer As String, ByVal EFlag As Integer, ByVal lpEHook As CB_ESCHook, ByVal lpSHook As CB_SendHook)"

DATA "<<2>> NP_RunPrgVB_Del Lib |npbscript32.dll| Alias |NP_RunPrgVB| (ByVal ModType As Integer, ByVal HIDHandle As UInteger, ByVal EFlag As Integer, ByVal lpEHook As CB_ESCHook, ByVal lpSHook As CB_SendHook)"

DATA "<<1>> NP_GetLastBScriptVB Lib |npbscript32.dll| Alias |NP_GetLastBScriptVB| (ByVal Index As Integer) As String"

DATA "<<1>> NP_GetMemItemVB Lib |npbscript32.dll| Alias |NP_GetMemItemVB| (ByVal GStoreID As Integer) As String"

DATA ""

DATA ""

DATA "Public gNP_Escape As UInteger"

DATA ""

DATA "Public Function VB_CheckAbortKeyHook() As UInteger"

DATA "    VB_CheckAbortKeyHook = gNP_Escape"

DATA "End Function"

DATA ""

DATA "Public Sub NP_Escape(ByVal EState As UInteger)"

DATA "        gNP_Escape = EState"

DATA "End Sub"

DATA ""

DATA "Public Function VB_SendCommandHook(ByVal HIDHandle As UInteger, ByVal CmdEvent As Integer, ByVal StoreMemID As Integer) As Integer"

DATA ""

DATA "        ' ------------------------------------------------------------"

DATA "        ' Sample Code to demonstrate how to use this Callback Function"

DATA "        ' ------------------------------------------------------------"

DATA "        Dim TT As String, Cmd As String, Reply As String, ExReply As String, PassFail As String, FailCT As String"

DATA "        TT = |(| & CmdEvent.ToString & |)   |"

DATA "        Select Case CmdEvent"

DATA "            Case 1  ' start BScript"

DATA "                TT = TT & |Start BScript|"

DATA "            Case 2  ' BScript Pseudo commands"

DATA "                Cmd = NP_GetLastBScriptVB(1)"

DATA "                Reply = NP_GetLastBScriptVB(2)"

DATA "                ExReply = NP_GetLastBScriptVB(3)"

DATA "                TT = TT & Cmd & |           | & Reply"

DATA "                If ExReply <> || Then TT = TT & |   (| & ExReply & |)|"

DATA "            Case 3  ' Ascii Commands send to controller"

DATA "                Cmd = NP_GetLastBScriptVB(1)"

DATA "                Reply = NP_GetLastBScriptVB(2)"

DATA "                ExReply = NP_GetLastBScriptVB(3)"

DATA "                PassFail = NP_GetLastBScriptVB(4)"

DATA "                FailCT = NP_GetLastBScriptVB(5)"

DATA "                TT = TT & Cmd & |           | & Reply"

DATA "                If ExReply <> || Then TT = TT & |   (| & ExReply & |)|"

DATA "                If PassFail <> || Then TT = TT & |   | & PassFail"

DATA "                If Val(FailCT) <> 0 Then TT = TT & |   | & FailCT"

DATA "            Case 4  ' BScript show command"

DATA "                Cmd = NP_GetLastBScriptVB(1)"

DATA "                Reply = NP_GetLastBScriptVB(2)"

DATA "                ExReply = NP_GetLastBScriptVB(3)"

DATA "                TT = TT & Cmd & |           | & Reply"

DATA "                If ExReply <> || Then TT = TT & |   (| & ExReply & |)|"

DATA "            Case 5  ' BScript sshow command and global memory update"

DATA "                Cmd = NP_GetLastBScriptVB(1)"

DATA "                Reply = NP_GetLastBScriptVB(2)"

DATA "                ExReply = NP_GetLastBScriptVB(3)"

DATA "                TT = TT & Cmd & |           | & Reply"

DATA "                If ExReply <> || Then TT = TT & |   (| & ExReply & |)|"

DATA "            Case 0  ' end BScript"

DATA "                TT = TT & |End BScript|"

DATA "            Case -1 ' Abort script"

DATA "                TT = |(-)   Abort BScript|"

DATA "        End Select"

DATA "        TT = TT & Chr(13) & Chr(10)"

DATA "        If CmdEvent = 0 Then TT = TT & Chr(13) & Chr(10)"

DATA "        ' ------------------------------------------------------------"

DATA "        ' Controls referenced below do not exist yet so REMed out"

DATA "        ' ------------------------------------------------------------"

DATA "        ' Form1.TextBox1.Text = Form1.TextBox1.Text & TT"

DATA "        ' Form1.TextBox1.SelectionStart = Form1.TextBox1.TextLength"

DATA "        ' Form1.TextBox1.ScrollToCaret()"

DATA "        My.Application.DoEvents()"

DATA "        ' ------------------------------------------------------------"

DATA "        '                      End of Sample Code"

DATA "        ' ------------------------------------------------------------"

DATA ""

DATA "        VB_SendCommandHook = 0"

DATA "End Function"

DATA ""

DATA "Public Sub NP_RunProgramVBcb(ByVal ModType As Integer, ByVal HIDHandle As UInteger, ByVal PrgBuffer As String, ByVal EFlag As Integer)"

DATA "        Dim lpSendCommandHook As CB_SendHook"

DATA "        lpSendCommandHook = New CB_SendHook(AddressOf VB_SendCommandHook)"

DATA "        Dim lpCheckAbortKey As CB_ESCHook"

DATA "        lpCheckAbortKey = New CB_ESCHook(AddressOf VB_CheckAbortKeyHook)"

DATA "        gNP_Escape = 0"

DATA "        NP_RunProgramVB_Del(ModType, HIDHandle, PrgBuffer, EFlag, lpCheckAbortKey, lpSendCommandHook)"

DATA "End Sub"

DATA ""

DATA ""

DATA "Public Sub NP_RunPrgVBcb(ByVal ModType As Integer, ByVal HIDHandle As UInteger, ByVal EFlag As Integer)"

DATA "        Dim lpSendCommandHook As CB_SendHook"

DATA "        lpSendCommandHook = New CB_SendHook(AddressOf VB_SendCommandHook)"

DATA "        Dim lpCheckAbortKey As CB_ESCHook"

DATA "        lpCheckAbortKey = New CB_ESCHook(AddressOf VB_CheckAbortKeyHook)"

DATA "        gNP_Escape = 0"

DATA "        NP_RunPrgVB_Del(ModType, HIDHandle, EFlag, lpCheckAbortKey, lpSendCommandHook)"

DATA "End Sub"


     LOCAL M1$, M2$, M3$

     M1$ = "Public Declare Function"

     M2$ = "Public Declare Sub"

     M3$ = CHR$(34)

     LOCAL CT&, I&, T$, ClipData$

     CT& = DATACOUNT

     FOR I& = 1 TO CT&

          T$ = READ$(I&)

          REPLACE "<<1>>" WITH M1$ IN T$

          REPLACE "<<2>>" WITH M2$ IN T$

          REPLACE "|" WITH M3$ IN T$

          ClipData$ = ClipData$ + T$ + CHR$(13) + CHR$(10)

     NEXT I&

     NP_SetText %NPID_Clipboard, ClipData$


END SUB