使用SendMessage进行进程间通信

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

转载于:https://www.cnblogs.com/lbnnbs/p/3644471.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值