The author has 2 posts.
Font size: Small - 100% (Default)  Content converter: No conversion
 
Clicks Replies
1181 1
【程序】两个进程通过文件映射共享一块内存
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 1 Posted at: 3/26/16 11:52
【程序1】
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>

void wait(char *str)
{
    char ch;
    do
    {
        while (*str != '\0')
            putchar(*str++);
        ch = getchar();
        fflush(stdin);
    } while (ch != 'y');
}

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 100, TEXT("Hello World"));
    LPSTR lpStr = (LPSTR)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 100);
    puts("请输入一段文字: ");
    gets_s(lpStr, 100);

    wait("请输入y显示内存内容: ");
    puts(lpStr);

    wait("请输入y关闭映射: ");
    UnmapViewOfFile(lpStr);
    CloseHandle(hMap);
    return 0;
}
巨大八爪鱼
武林盟主 二十一级
Reply
Floor 2 Posted at: 3/26/16 11:52
【程序2】
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>

void wait(char *str)
{
    char ch;
    do
    {
        while (*str != '\0')
            putchar(*str++);
        ch = getchar();
        fflush(stdin);
    } while (ch != 'y');
}

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hMapFile = OpenFileMapping(PAGE_READONLY, FALSE, TEXT("Hello World"));
    if (hMapFile == NULL)
    {
        puts("文件映射不存在");
        return 0;
    }
    LPSTR lpStr = (LPSTR)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 100);
    puts(lpStr);

    puts("将内容修改为: ");
    gets_s(lpStr, 100);

    UnmapViewOfFile(lpStr);
    CloseHandle(hMapFile);
    return 0;
}
Reply the post
Content:
User: You are currently anonymous.
Captcha:
Unclear? Try another one.
(Shortcut key: Ctrl+Enter)
Post Information
Clicks: 1181 Replies: 1
Author: 巨大八爪鱼
Last reply: 巨大八爪鱼
Last reply time: 3/26/16 11:52
Bar Good Posts
Announcements