首页 > 其他 > 详细

使用SendMessage进行进程间通信

时间:2014-04-04 17:52:53      阅读:528      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
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
bubuko.com,布布扣
bubuko.com,布布扣
Dim flag As Char = Chr(2)  正文开始

Dim msg As String = "100,1,15870727024," & Now.ToString & ",1," & flag & "success"

SendMsg(msg)  ID为1的数据发送成功
bubuko.com,布布扣
bubuko.com,布布扣
 接收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
bubuko.com,布布扣

使用SendMessage进行进程间通信,布布扣,bubuko.com

使用SendMessage进行进程间通信

原文:http://www.cnblogs.com/lbnnbs/p/3644471.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!