当前位置: 网学 > 编程文档 > VB > 正文

在VB中用文件映射来进行进程通讯

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/16
下载{$ArticleTitle}原创论文样式
FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
#End If
#If Sampling Then
Declare Function CreateFileMapping Lib "kernel32" Alias "CreateFileMappingA" _
(ByVal hFile As Long, _
ByVal lpFileMappingAttributes As Long, _
ByVal flProtect As Long, _
ByVal dwMaximumSizeHigh As Long, _
ByVal dwMaximumSizeLow As Long, _
ByVal lpName As String) As Long
#Else
Declare Function OpenFileMapping Lib "kernel32" Alias "OpenFileMappingA" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal lpName As String) As Long
#End If
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
Declare Function UnmapViewOfFile Lib "kernel32" _
(lpBaseAddress As Any) As Long
''
Public Sub InitVar()
DiskFileName = "D:\Article\Mapping\Sample"
MapFileName = DiskFileName & "Map"
Pub_LenDT = Len(Pub_FormatDT)
Pub_LenV = Len(Pub_FormatV)
LenBuffer = 1 + Pub_LenDT + (Pub_LenV + 1) * Pub_LoopN
strBuffer = String(LenBuffer + 1, "*")
FileHandle = 0
MapHandle = 0
MapAddress = 0
End Sub ''InitVar
   
Public Sub CopyToMap(S As String)
If MapAddress <> 0 Then
Call lstrcpyn(ByVal MapAddress, ByVal S, LenBuffer + 1)
End If
End Sub
   
Public Sub GetFromMap(S As String)
If MapAddress <> 0 Then
Call lstrcpyn(ByVal S, ByVal MapAddress, LenBuffer + 1)
End If
End Sub
   
Public Sub CloseMap()
If MapAddress <> 0 Then
Call UnmapViewOfFile(ByVal MapAddress)
MapAddress = 0
End If
If MapHandle <> 0 Then
Call CloseHandle(MapHandle)
MapHandle = 0
End If
If FileHandle <> 0 Then
Call CloseHandle(FileHandle)
FileHandle = 0
End If
End Sub ''CloseMap
   
#If Sampling Then
   
Public Sub CreateMap()
Dim w As Long
   
Call InitVar
FileHandle = CreateFile(DiskFileName, _
GENERIC_WRITE Or GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
0, _
CREATE_ALWAYS, _
FILE_ATTRIBUTE_NORMAL, _
0)
Call WriteFile(FileHandle, ByVal strBuffer, LenBuffer + 1, w, 0)
Call FlushFileBuffers(FileHandle)
   
MapHandle = CreateFileMapping(FileHandle, _
0, _
PAGE_READWRITE, _
0, _
0, _
MapFileName)
MapAddress = MapViewOfFile(MapHandle, FILE_MAP_WRITE, 0, 0, 0)
End Sub ''CreateMap
   
#Else
   
Public Function OpenMap() As Long
Call InitVar
OpenMap = 0
MapHandle = OpenFileMapping(FILE_MAP_WRITE, False, MapFileName)
If MapHandle = 0 Then Exit Function
MapAddress = MapViewOfFile(MapHandle, FILE_MAP_WRITE, 0, 0, 0)
If MapAddress = 0 Then
Call CloseHandle(MapHandle)
MapHandle = 0
End If
OpenMap = MapAddress
End Function ''OpenMap
   
#End If ''Sampling
   
Manage.vbp也包含两个文件:Form1.frm,Module1.bas。清单如下:
Form1.frm:
VERSION 5.00
Begin VB.Form Form1  
Caption = "Manage&quo
  • 下一篇资讯: 用VB编写登录程序
  • 网学推荐

    免费论文

    原创论文

    浏览:
    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
    湘ICP备09003080号