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

VB两个程序(进程)间通信

预备知识:api,消息        《感悟Visual Basic 细水长流话API》,《VB 与 Windows API 讲座》/王国荣 这两个文章是比较好的API入门材料。通过这两篇教材学习最基...
  • bigcarp
  • bigcarp
  • 2005年07月20日 11:36
  • 2865

VB 进程间同步通讯

例子的来源是MSDN,很早在MSDN上面看到的,后来找不到了,这次是从MSDN中VB例程cofee例程上摘录的,原理相同,都是用active,自定义事件.1创建一个Active Exe工程,我叫pro...
  • lbuskeep
  • lbuskeep
  • 2011年04月22日 13:49
  • 793

Android 进程间通讯四种方式

android系统中应用程序之间不能共享内存,在android SDK中提供了4种用于跨进程通讯的方式。这4种方式正好对应于android系统中4种应用程序组件:Activity、Content Pr...
  • hedong316
  • hedong316
  • 2016年01月20日 14:43
  • 9420

进程间通讯方式以及各个方式的优缺点

进程间通讯方式以及各个方式的优缺点 2014-7-19阅读99 评论0 进程通信的含义 进程是转入内存并准备执行的程序,每个程序都有私有的虚拟地址空间,由代码,数据以及它可利用的系统资源(如文...
  • u014774781
  • u014774781
  • 2015年05月04日 20:46
  • 11475

VB与三菱PLC通讯

一. 前言 随着生产自动化程度的不断提高,可编程序控制器PLC得到了广泛的应用,特别是小型PLC具有编程简单、性能可靠和价格低的特点,被大量应用于单机控制系统中,如啤酒的稀释控制、离心机控制等。作为...
  • zcj331
  • zcj331
  • 2014年04月11日 13:27
  • 3630

linux进程间通讯的几种方式的特点和优缺点,和适用场合

linux进程间通讯的几种方式的特点和优缺点,和适用场合 由于不同的进程运行在各自不同的内存空间中.一方对于变量的修改另一方是无法感知的.因此.进程之间的信息传递不可能通过变量或其它数据结构直接进行...
  • SHEDONG1011
  • SHEDONG1011
  • 2015年09月20日 11:52
  • 5375

一个进程间同步和通讯的 C# 框架

一个进程间同步和通讯的 C# 框架
  • itsoftchenfei
  • itsoftchenfei
  • 2014年04月19日 19:14
  • 1257

python 进程间通信

python multiprocessing multiprocessing 在2.6才开始使用 multiprocessing 是一个使用方法类似threading模块的进程模块。允许...
  • meeasyhappy
  • meeasyhappy
  • 2013年01月29日 12:13
  • 20298

一个进程间同步和通讯的 C# 框架

0.背景简介 微软在 .NET 框架中提供了多种实用的线程同步手段,其中包括 monitor 类及 reader-writer锁。但跨进程的同步方法还是非常欠缺。另外,目前也没有方便的线程间及进...
  • Comman1999
  • Comman1999
  • 2013年12月31日 14:05
  • 3020

线程通信和进程通信区别(线程进程区别)

前言:腾讯笔试中多道选择题考到这个问题,这里总结一下。学习Java的童鞋可能对于线程的理解要比学php好很多。本文参考于线程通信与进程通信的区别 。 进程和线程的区别:对于进程来说,子进程是父进程的...
  • Return_True_hang
  • Return_True_hang
  • 2017年04月08日 10:37
  • 731
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:VB 进程间通讯
举报原因:
原因补充:

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