Advertisement
Guest User

Untitled

a guest
Oct 12th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.45 KB | None | 0 0
  1. Imports System.Text
  2.  
  3. Public Class BuildString
  4.     Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" ( _
  5.         ByVal hwnd As Integer, _
  6.         ByVal wMsg As Integer, _
  7.         ByVal wParam As Integer, _
  8.         ByVal lParam As Integer _
  9.     ) As Integer
  10.  
  11.     Public Event StringOK(ByVal Result As String)
  12.     Private hwnd As Integer = 0
  13.     Private wMsg As Integer = 0
  14.     Private wParam As Integer = 0
  15.     Private lParam As String = ""
  16.     Private tempA() As Byte = New Byte() {}
  17.     Private enc As Encoding = Encoding.UTF8
  18.  
  19.     Public Property Encode() As Encoding
  20.         Get
  21.             Return enc
  22.         End Get
  23.         Set(ByVal value As Encoding)
  24.             enc = value
  25.         End Set
  26.     End Property
  27.  
  28.     Public Sub BuildString(ByVal b As IntPtr)
  29.         If b <> 0 Then
  30.             'build temp array
  31.             Dim tempB(tempA.Length) As Byte
  32.             tempA.CopyTo(tempB, 0)
  33.             tempB(tempA.Length) = b
  34.             ReDim tempA(tempB.Length - 1)
  35.             tempB.CopyTo(tempA, 0)
  36.         Else
  37.             'decode byte array to string
  38.             Dim s As String
  39.             If enc Is Encoding.UTF8 Then
  40.                 s = Encoding.UTF8.GetString(tempA)
  41.             ElseIf enc Is Encoding.Unicode Then
  42.                 s = Encoding.Unicode.GetString(tempA)
  43.             ElseIf enc Is Encoding.ASCII Then
  44.                 s = Encoding.ASCII.GetString(tempA)
  45.             Else
  46.                 s = Encoding.Default.GetString(tempA)
  47.             End If
  48.             'send out result string via event
  49.             RaiseEvent StringOK(s)
  50.             tempA = New Byte() {} ' Versuch 1, die letzte Nachricht zu löschen ... Fehlgeschlagen
  51.         End If
  52.     End Sub
  53.  
  54.     Public Sub PostString( _
  55.         ByVal hwnd As Integer, _
  56.         ByVal wMsg As Integer, _
  57.         ByVal wParam As Integer, _
  58.         ByVal lParam As String _
  59.     )
  60.         Dim ba() As Byte
  61.         If enc Is Encoding.UTF8 Then
  62.             ba = Encoding.UTF8.GetBytes(lParam)
  63.         ElseIf enc Is Encoding.Unicode Then
  64.             ba = Encoding.Unicode.GetBytes(lParam)
  65.         ElseIf enc Is Encoding.ASCII Then
  66.             ba = Encoding.ASCII.GetBytes(lParam)
  67.         Else
  68.             ba = Encoding.Default.GetBytes(lParam)
  69.         End If
  70.         Dim i As Integer
  71.         For i = 0 To ba.Length - 1
  72.             PostMessage(hwnd, wMsg, wParam, ba(i))
  73.         Next
  74.         PostMessage(hwnd, wMsg, wParam, 0)
  75.     End Sub
  76. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement