| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1395 人关注过本帖, 1 人收藏
标题:请教多个进程如何读写共享内存?
只看楼主 加入收藏
sych
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:300
专家分:436
注 册:2019-10-11
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:6 
请教多个进程如何读写共享内存?
DECLARE INTEGER GetLastError IN win32api
Declare Long OpenFileMapping In WIN32API Long, Long, String
Declare Long CreateFileMapping In WIN32API Long, Long, Long, Long, Long, String
DECLARE INTEGER CloseHandle IN kernel32.DLL INTEGER hObject

DECLARE integer GetProcessHeap IN WIN32API
DECLARE integer HeapAlloc IN WIN32API Long,Long,Long
DECLARE integer HeapFree IN WIN32API integer,integer,integer

Declare long MapViewOfFile IN WIN32API long hFileMappingObject,Long dwDesiredAccess, Long dwFileOffsetHigh, Long dwFileOffsetLow, Long dwNumberOfBytesToMap
Declare Long UnmapViewOfFile in win32api long lpBaseAddress
**申明可能有错
Declare Long WriteProcessMemory in win32api long @hProcess, Long lpBaseAddress ,long  @lpBuffer ,long  nSize , Long @lpNumberOfBytesWritten
Declare long ReadProcessMemory in WIN32API long hProcess , Long lpBaseAddress ,long lpBuffer,long nSize ,Long @lpNumberOfBytesWritten

cPID = "争王"
hmap = OpenFileMapping(983071, 0, cPID)  &&读
If 0 == hmap  &&没有创建
    hmap = CreateFileMapping(-1, 0,4, 0, 40, cPID)  &&读写
    pmap=MapViewOfFile(hMap, 0xF0000+1+2+4+8+0x10, 0, 0, 4)
    hProcHeap=GetProcessHeap()
    lhMemPtr=HeapAlloc(hProcHeap,0,4)
    SYS(2600,lhMemPtr,4,"1234")
    lpnum=0
SYS(2600,pmap,0,,"123")  &&也出错
**写内存,出错,句柄无效
*    ?hmap
*    ?WriteProcessMemory(@pmap,0,@lhMemPtr,4,@lpnum)
*    ?lpnum
    ?GetLastError()
    HeapFree(hProcHeap,0,lhMemPtr)
    UnmapViewOfFile(pmap)
ELSE
**读内存
    pmap=MapViewOfFile(hMap, 983071, 0, 0, 0)
    ?SYS(2600,pmap,4)
    UnmapViewOfFile(pmap)
    =CloseHandle(hmap)
Endif
retu
搜索更多相关主题的帖子: Long 读写 内存 INTEGER 共享 
2022-06-08 16:40
my2318
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:14
帖 子:258
专家分:640
注 册:2014-3-18
收藏
得分:0 
可以调用windows的api实现,具体可以看说明文档。
2022-06-08 16:50
laowan001
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:66
帖 子:1088
专家分:2677
注 册:2015-12-30
收藏
得分:0 
这么底层的操作不是VFP的强项
2022-06-08 17:40
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:451
帖 子:10607
专家分:43182
注 册:2014-5-20
收藏(2)
得分:20 
给个示例参考
MemShare.h
程序代码:
** MemShare.h
#DEFINE    INVALID_HANDLE_VALUE  0xFFFFFFFF
#DEFINE    PAGE_READWRITE        4
#DEFINE    FILE_MAP_ALL_ACCESS   0x000F001F


MemShare.prg
程序代码:
**    创建的进程调用  _CreateMap()   和 _CloseMap()
**    使用的进程调用  _OpenMapView() 和 _CloseMapView()

#INCLUDE MemShare.h

FUNCTION _LoadMemShareApi()
    DECLARE LONG CreateFileMapping IN Kernel32 LONG,STRING@,LONG,LONG,LONG,STRING@
    DECLARE LONG CloseHandle IN Kernel32 LONG
    DECLARE LONG OpenFileMapping IN Kernel32 LONG,LONG,STRING@
    DECLARE LONG MapViewOfFile IN Kernel32 LONG,LONG,LONG,LONG,LONG
    DECLARE LONG UnmapViewOfFile IN Kernel32 LONG
ENDFUNC

**创建共享内存文件
FUNCTION _CreateFileMap(cShareName, nSize)
    RETURN CreateFileMapping(INVALID_HANDLE_VALUE,;  &&在页面文件中创建一个可共享的文件映射
                             NULL,;                  &&默认安全级别
                             PAGE_READWRITE,;        &&可读可写
                             0,;                     &&高位文件大小
                             nSize,;                 &&低位文件大小
                             cShareName)             &&共享内存名称
ENDFUNC

**关闭共享内存文件
FUNCTION _CloseFileMap(hFileMap)
    IF hFileMap != 0
        CloseHandle(hFileMap)
        hFileMap = 0
    ENDIF
ENDFUNC

**创建内存映射文件
** hFileMap = 0
** pMapView = _CreateMap(cShareName, nSize, @hFileMap)
** IF hFileMap==0 OR pMapView==0
**     RETURN
** ENDIF
FUNCTION _CreateMap(cShareName, nSize, hFileMap)
    hFileMap = _CreateFileMap(cShareName, nSize)
    IF hFileMap == 0
        RETURN 0
    ENDIF
    LOCAL pMapView
    pMapView = MapViewOfFile(hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, nSize)
    IF pMapView == 0
        _CloseFileMap(hFileMap)
        hFileMap = 0
        RETURN 0
    ENDIF
    RETURN pMapView
ENDFUNC

**关闭内存映射文件
FUNCTION _CloseMap(hFileMap, pMapView)
    IF pMapView != 0
        UnmapViewOfFile(pMapView)
        pMapView = 0
    ENDIF
    IF hFileMap != 0
        _CloseFileMap(hFileMap)
    ENDIF
ENDFUNC

** 打开共享内存映射
**    hMapView = 0 
**    pMapView2 = _OpenMapView("myMemShare", nSize, @hMapView)
**    IF hMapView==0 OR pMapView2==0
**        RETURN
**    ENDIF
FUNCTION _OpenMapView(cShareName, nSize, hMapView)
    ** 打开共享内存,存取权限 FILE_MAP_ALL_ACCESS
    hMapView = OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, @cShareName)
    IF hMapView == 0
        RETURN 0
    ENDIF
    **映射缓存区视图, 得到指向共享内存的指针
    LOCAL pMapView
    pMapView = MapViewOfFile(hMapView, FILE_MAP_ALL_ACCESS, 0, 0, nSize)
    IF pMapView == 0
        CloseHandle(hMapView)
        hMapView = 0
        RETURN 0
    ENDIF
    RETURN pMapView
ENDFUNC

** 关闭共享内存映射
FUNCTION _CloseMapView(hMapView, pMapView)
    IF pMapView != 0
        UnmapViewOfFile(pMapView)   &&解除共享内存映射
        pMapView = 0
    ENDIF
    IF hMapView != 0
        CloseHandle(hMapView)       &&关闭共享内存
        hMapView = 0
    ENDIF
ENDFUNC


示例
程序代码:
**    载入模块
SET PROCEDURE TO MemShare.prg ADDITIVE
_LoadMemShareApi() 
hFileMap = 0
pMapView = _CreateMap("myMemShare", 1024, @hFileMap)
IF hFileMap==0 OR pMapView==0
    RETURN
ENDIF
s = "写共享内存"
SYS(2600, pMapView, LEN(s), s)
hMapView = 0 
pMapView2 = _OpenMapView("myMemShare", 1024, @hMapView)
IF hMapView==0 OR pMapView2==0
    RETURN
ENDIF
? SYS(2600, pMapView2, LEN(s))
_CloseMapView(hMapView, pMapView2)
_CloseMap(hFileMap, pMapView)


[此贴子已经被作者于2022-6-8 21:06编辑过]

2022-06-08 21:03
schtg
Rank: 12Rank: 12Rank: 12
来 自:Usa
等 级:贵宾
威 望:67
帖 子:1710
专家分:3318
注 册:2012-2-29
收藏
得分:0 
@吹版,谢谢!
2022-06-09 05:32
sych
Rank: 6Rank: 6
等 级:侠之大者
威 望:7
帖 子:300
专家分:436
注 册:2019-10-11
收藏
得分:0 
谢谢!
Declare Long OpenFileMapping In WIN32API Long, Long, String
Declare Long CreateFileMapping In WIN32API Long, Long, Long, Long, Long, String
Declare long MapViewOfFile IN WIN32API long hFileMappingObject,Long dwDesiredAccess, Long dwFileOffsetHigh, Long dwFileOffsetLow, Long dwNumberOfBytesToMap
Declare Long UnmapViewOfFile in win32api long lpBaseAddress
DECLARE INTEGER CloseHandle IN kernel32.DLL INTEGER hObject

cPID = "争王"
hmap = OpenFileMapping(4, 0, cPID)
If 0 = hmap
    hmap = CreateFileMapping(-1, 0,4, 0, 40, cPID)
    pmap=MapViewOfFile(hMap, 2, 0, 0, 0)
    cWrite="1234567测试"
    SYS(2600,pmap,LEN(cWrite),cWrite)
    UnmapViewOfFile(pmap)
ELSE
    pmap=MapViewOfFile(hMap, 4, 0, 0, 0)
    cWrite=SYS(2600,pmap,40)
?cWrite
    UnmapViewOfFile(pmap)
    =CloseHandle(hmap)
Endif
retu
2022-06-09 08:19
ljb8864
Rank: 2
等 级:论坛游民
帖 子:171
专家分:14
注 册:2023-6-4
收藏
得分:0 
有何用处,刚建了,删不掉吗

[此贴子已经被作者于2023-7-2 18:46编辑过]

2023-07-02 18:28
快速回复:请教多个进程如何读写共享内存?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041483 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved