Imports System.Runtime.InteropServices
Public Class Monitor
<DllImport("user32.dll", CharSet:=CharSet.Unicode, entrypoint:="SendMessageW")> _
Private Shared Function SendMessage(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As Integer) As Integer
End Function
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
'发送方的代码
Public Shared Sub SendMsg(ByVal message As String)
Dim hWnd As Integer
hWnd = FindWindow(vbNullString, "短信发送服务器")
If hWnd Then ' 有这个窗口
Dim ptr As IntPtr = Marshal.StringToHGlobalUni(message)
SendMessage(hWnd, &H1397, ptr, 0) ' 给类名为SmsServerMonitor的窗口发消息,消息标示为&H1397
Marshal.FreeHGlobal(ptr)
Else
Log.Write("未能找到窗口")
End If
End Sub
End Class
Dim flag As Char = Chr(2) ' 正文开始
Dim msg As String = "100,1,15870727024," & Now.ToString & ",1," & flag & "success"
SendMsg(msg) ' ID为1的数据发送成功
' 接收SmsService发过来的消息消息标示为&H1397
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = &H1397 Then
Dim msg As String = System.Runtime.InteropServices.Marshal.PtrToStringUni(m.WParam)
' 消息格式:
'id,发送状态,手机号码,时间,优先级,内容
Dim flag As Char = Chr(2) ' 正文开始
Dim body() As String = msg.Split(flag)
Dim header() As String = body(0).Split(",")
Dim Id As String = header(0)
Dim Status As String = header(1)
Dim Phone As String = header(2)
Dim SendTime As String = header(3)
Dim Priority As String = header(4)
Dim Content As String = body(1)
Dim idxRow As Integer = dgvSmsLog.Rows.Add()
dgvSmsLog.Rows(idxRow).Cells(0).Value = Id
dgvSmsLog.Rows(idxRow).Cells(1).Value = Status
dgvSmsLog.Rows(idxRow).Cells(2).Value = Phone
dgvSmsLog.Rows(idxRow).Cells(3).Value = SendTime
dgvSmsLog.Rows(idxRow).Cells(4).Value = Priority
dgvSmsLog.Rows(idxRow).Cells(5).Value = Content
Else
MyBase.WndProc(m)
End If
End Sub