VB 进程间通讯

原创 2007年10月01日 19:44:00

对于进程间的 通讯有N多种方法,常见的有DDE,还有内存文件映射,管道,自定义消息,甚至WINSOCK等等很多种方法本文就内存文件映射来说下进程间的通讯.

建立程序A代码如下

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1
      Caption         =   "Command1"
      Height          =   555
      Left            =   1170
      TabIndex        =   0
      Top             =   960
      Width           =   1245
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, lpFileMappigAttributes As Any, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long
Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long
Private Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

Private Type myType
    hFile As Long
    strTmp(259) As Byte
End Type

Private Sub Command1_Click()
    Dim hMap As Long, pMap As Long, m As myType, j As Long, h As Long, n As myType, bytes(9) As Byte
    hMap = CreateFileMapping(GetCurrentProcess, ByVal 0, 983071, 0, 256, "myTest")
    If hMap <> 0 And hMap <> -1 Then
        m.hFile = 123
        CopyMemory m.strTmp(0), ByVal "123", LenB("123")

        pMap = MapViewOfFile(hMap, 983071, 0, 0, 256)
'       把信息写到共享的地址空间,这里也可以用下面的WriteProcessMemory是一样的道理
        CopyMemory ByVal pMap, m, Len(m)
        If pMap <> 0 Then
'            h = WriteProcessMemory(GetCurrentProcess, ByVal pMap, m, Len(m), j)
'            试着读数据,也可以用CopyMemory
'            h = ReadProcessMemory(GetCurrentProcess, ByVal pMap, n, Len(n), j)
        End If
    End If
End Sub

 

然后再建立程序B源码如下:

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command1
      Caption         =   "Command1"
      Height          =   795
      Left            =   630
      TabIndex        =   0
      Top             =   690
      Width           =   1425
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" (ByVal hFile As Long, lpFileMappigAttributes As Any, ByVal flProtect As Long, ByVal dwMaximumSizeHigh As Long, ByVal dwMaximumSizeLow As Long, ByVal lpName As String) As Long
Private Declare Function OpenFileMapping Lib "kernel32" Alias "OpenFileMappingA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long
Private Declare Function MapViewOfFile Lib "kernel32" (ByVal hFileMappingObject As Long, ByVal dwDesiredAccess As Long, ByVal dwFileOffsetHigh As Long, ByVal dwFileOffsetLow As Long, ByVal dwNumberOfBytesToMap As Long) As Long
Private Declare Function UnmapViewOfFile Lib "kernel32" (lpBaseAddress As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Type myType
    hFile As Long
    strTmp(259) As Byte
End Type

Private Sub Command1_Click()
    Dim hMap As Long, pMap As Long, m As myType, pm As Long
    Dim h As Long
    pm = VarPtr(m)
    hMap = OpenFileMapping983071, 0, "myTest")
    If hMap <> 0 And hMap <> -1 Then
        pMap = MapViewOfFile(hMap, 983071 0, 0, Len(m))
        If pMap <> 0 Then
            CopyMemory m, ByVal pMap, Len(m)

        End If
    End If
End Sub

相关文章推荐

用文件映射(File Mapping)实现进程间内存共享

    我们知道,在Windows中的每个进程都有自己独立的内存空间。该独立的内存空间包含了所有的可执行模块或DLL模块的代码和数据以及动态内存分配的空间。每个进程的内存空间只能被该进程访问,其他进程...
  • starlee
  • starlee
  • 2007年06月01日 09:33
  • 14354

VB进程间通讯演示

  • 2006年03月16日 00:00
  • 108KB
  • 下载

VB进程间通讯演示附源代码

  • 2006年01月18日 10:38
  • 51KB
  • 下载

vb 利用管道使进程间互相通信

Option   Explicit               Private   Declare   Function   CreatePipe   Lib   "kernel32"   (phRe...

进程间通讯

  • 2016年02月17日 22:38
  • 610KB
  • 下载

windows 进程间通讯大全

  • 2016年05月23日 16:25
  • 7KB
  • 下载

Android中实现不同应用进程间通讯

Android中实现不同应用进程间通讯,需要用到AIDL技术,以下为AIDL服务端和客户端实现步骤: 一、服务端: 1、在AndroidManifest.xml中定义的包路径下新建一个文件,扩展名...
  • wqmain
  • wqmain
  • 2014年01月14日 15:35
  • 4967

进程间通讯

  • 2012年03月13日 20:39
  • 1.8MB
  • 下载

Android进程间通讯——Messager(信使)

(一) 前言 Messager  n.信使,信差; 先驱; [军]通信兵,传令兵; 顾名思义,Messenger就像一个信使一样,可以负责双方的数据传递。当然如果只是单向传递的话一个Messager就...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB 进程间通讯
举报原因:
原因补充:

(最多只允许输入30个字)