Chrome Native Messaging Host VB.Net

时间:2018-09-20 04:12:11

标签: vb.net stdout stdin chrome-native-messaging

在经历了多次尝试和错误之后,我很难找到在VB.Net中对Chrome Native Messaging Hosts的任何支持,我整理了一次可以处理一条消息的内容。但是我无法获得

While OpenStandardStreamIn() IsNot Nothing

参与工作。如果有人有任何建议...

对于其他像我一样寻找VB.Net本机消息主机的人,此代码非常有用:

此外,它使用Newtonsoft.Json Lib进行JSON序列化...

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Diagnostics
Imports System.Runtime.InteropServices


Module Module1

Public Sub Main(ByVal args As String())

    'create response as an array so it can be easily converted JSON
    Dim ResponseString() As String = {""}

    ' get input from chrome extension
    Dim InputString As String = OpenStandardStreamIn()

    ' Do whatever you want with Input, then prepare Response
    ResponseString(0) = DO_SOMETHING(InputString)

    ' Send Response to Chrome
    OpenStandardStreamOut(ResponseString)

End Sub

Public Function OpenStandardStreamIn() As String
    Dim MsgLength As Integer = 0
    Dim InputData As String = ""
    Dim LenBytes As Byte() = New Byte(3) {} 'first 4 bytes are length

    Dim StdIn As System.IO.Stream = Console.OpenStandardInput() 'open the stream
    StdIn.Read(LenBytes, 0, 4) 'length
    MsgLength = System.BitConverter.ToInt32(LenBytes, 0) 'convert length to Int

    Dim Buffer As Char() = New Char(MsgLength - 1) {} 'create Char array for remaining bytes

    Using Reader As System.IO.StreamReader = New System.IO.StreamReader(StdIn) 'Using to auto dispose of stream reader
        While Reader.Peek() >= 0 'while the next byte is not Null
            Reader.Read(Buffer, 0, Buffer.Length) 'add to the buffer

        End While
    End Using

    InputData = New String(Buffer) 'convert buffer to string

    Return InputData
End Function

Private Sub OpenStandardStreamOut(ByVal ResponseData() As String) 'fit the response in an array so it can be JSON'd

    Dim OutputData As String = ""
    Dim LenBytes As Byte() = New Byte(3) {} 'byte array for length
    Dim Buffer As Byte() 'byte array for msg

    OutputData = Newtonsoft.Json.JsonConvert.SerializeObject(ResponseData) 'convert the array to JSON

    Buffer = System.Text.Encoding.UTF8.GetBytes(OutputData) 'convert the response to byte array
    LenBytes = System.BitConverter.GetBytes(Buffer.Length) 'convert the length of response to byte array

    Using StdOut As System.IO.Stream = Console.OpenStandardOutput() 'Using for easy disposal

        StdOut.WriteByte(LenBytes(0)) 'send the length 1 byte at a time
        StdOut.WriteByte(LenBytes(1))
        StdOut.WriteByte(LenBytes(2))
        StdOut.WriteByte(LenBytes(3))

        For i As Integer = 0 To Buffer.Length - 1 'loop the response out byte at a time
            StdOut.WriteByte(Buffer(i))
        Next

    End Using
End Sub
End Module

从Chrome扩展程序中打开一个连接,发送一条消息,一旦收到响应,主机将关闭。检查我们的Chrome开发人员页面以获取示例应用/扩展名:https://developer.chrome.com/apps/nativeMessaging

希望对您有所帮助,如果您有任何改进,请告诉我!

1 个答案:

答案 0 :(得分:0)

;WITH CTE
AS
(
    SELECT DISTINCT
        T1.GroupID ,
        T1.Value
    FROM table1 T1
        LEFT JOIN table1 T2
            ON T1.GroupID = T2.GroupID
    WHERE T1.Value='V94'
    UNION ALL
    SELECT
        C.GroupID,
        T.Value
    FROM dbo.table1 T
        JOIN CTE C
            ON T.Value = C.Value
)
SELECT * FROM CTE