CreateFileMapping

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

unsigned int getpnnum(HANDLE *phandle);
BOOL refresh(unsigned int num, HANDLE file);
unsigned int read_num(HANDLE file);
TCHAR p_inf[] = _T("NUMBER");


unsigned int getpnnum(HANDLE *phandle)
{
    unsigned int r = 0;
    HANDLE handle = NULL;
    handle = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, p_inf);
    if (handle != NULL)
    {
        r = read_num(handle);
        refresh(r + 1, handle);
        *phandle = handle;
        return r + 1;
    }
    else
    {
        handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
            0, sizeof(unsigned int), p_inf);
        if (handle != NULL)
            printf("内存映射文件建立成功!\n");
        refresh(1, handle);
        *phandle = handle;
        return 1;
    }
}


BOOL refresh(unsigned int num, HANDLE file)
{
    unsigned int*temp = NULL;
    if (file != NULL)
    {
        temp = (unsigned int*)MapViewOfFile(file,
            FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
        if (temp != NULL)
        {
            *temp = num;UnmapViewOfFile(temp);
        }
        else return FALSE;
        return TRUE;
    }
    else return FALSE;
}


/*读内存映射区*/
unsigned int read_num(HANDLE file)
{
    unsigned int*temp = NULL;
    unsigned int r = 0;
    if (file != NULL)
    {
        temp = (unsigned int*)MapViewOfFile(file,
            FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
        if (temp != NULL)
        {
            r = *temp;UnmapViewOfFile(temp);return r;
        }
        else return 0;
    }
    else
        return 0;
}


int main()
{
    HANDLE hPIN_num = NULL;             /*实例节点数目内存映射*/
    unsigned int num = 0, th;
    th = getpnnum(&hPIN_num);
    printf("这是第%d个PINode实例\n", th);
    while (1);
    th = read_num(hPIN_num);
    refresh(th - 1, hPIN_num);
    CloseHandle(hPIN_num);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值