命名共享内存 Windows api c++

Named shared memory Windows api c++

我正在尝试在 2 个进程之间共享一些数据。第一个将数据写入映射文件,第二个读取它。

到目前为止,这是我的代码:

第一个过程:

    #include "stdafx.h"
    #include <Windows.h>
    #include <tlhelp32.h>
    #include <tchar.h>
    #include<stdio.h>

    #define BUF_SIZE 256

    int _tmain(int argc, _TCHAR* argv[]) {
    TCHAR szName[] = TEXT("Global\MyFileMappingObject");
    LPTSTR szMsg = TEXT("MESS");

    HANDLE tokenH;
    TOKEN_PRIVILEGES tp;
    LUID luid;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenH)) {
        printf("OpenProcessToken error: %u\n", GetLastError());
        return FALSE;
    }
    if (!LookupPrivilegeValue(NULL, SE_CREATE_GLOBAL_NAME, &luid)) {
        printf("LookupPrivilegeValue error: %u\n", GetLastError());
        return FALSE;
    }
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (!AdjustTokenPrivileges(tokenH, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),(PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
        printf("AdjustTokenPrivileges error: %u\n", GetLastError());
        return FALSE;
    }
    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    {
        printf("The token does not have the specified privilege. \n");
        return FALSE;
    }
    CloseHandle(tokenH);

    HANDLE hMapFile;
    LPCTSTR pBuf;
    hMapFile = CreateFileMapping(
        INVALID_HANDLE_VALUE,  
        NULL,                    
        PAGE_READWRITE,        
        0,                      
        BUF_SIZE,                
        szName);
    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
        GetLastError());
        return 1;
    }
    pBuf = (LPTSTR)MapViewOfFile(hMapFile,   
        FILE_MAP_ALL_ACCESS,
        0,
        0,
        BUF_SIZE);

    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
        GetLastError());

        CloseHandle(hMapFile);

        return 1;
    }
    CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
    UnmapViewOfFile(pBuf);
    printf("Done\n");
    CloseHandle(hMapFile);
    return 0;
}

第二个过程:

#include "stdafx.h"
#include <Windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

#define BUF_SIZE 256

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR szName[] = TEXT("Global\MyFileMappingObject");
    HANDLE tokenH;
    TOKEN_PRIVILEGES tp;
    LUID luid;
    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenH);
    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) {
        printf("LookupPrivilegeValue error: %u\n", GetLastError());
        return FALSE;
    }
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if (!AdjustTokenPrivileges(tokenH, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) {
        printf("AdjustTokenPrivileges error: %u\n", GetLastError());
        return FALSE;
    }
    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
    {
        printf("The token does not have the specified privilege. \n");
        return FALSE;
    }
    CloseHandle(tokenH);

    HANDLE hMapFile;
    LPCTSTR pBuf;
    hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,   // read/write access
        FALSE,                 // do not inherit the name
        szName);               // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not open file mapping object (%d).\n"),
            GetLastError());
        return 1;
    }
    pBuf = (LPTSTR)MapViewOfFile(hMapFile, // handle to map object
        FILE_MAP_ALL_ACCESS,  // read/write permission
        0,
        0,
        BUF_SIZE);

    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
            GetLastError());
        CloseHandle(hMapFile);
        return 1;
    }
    MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
    UnmapViewOfFile(pBuf);
    CloseHandle(hMapFile);
    return 0;
}

第一个进程设法写入其数据(我没有收到任何错误消息并收到 "Done" 消息),但问题出在第二个进程上。 在 "OpenFileMapping" 之后,我从 getLastError 得到代码 2,它表示不存在的文件。我 运行 这两个进程都是管理员。

错误 2 是 ERROR_FILE_NOT_FOUND,这意味着调用 OpenFileMapping() 时命名映射对象不存在。

为了在进程之间共享命名内核对象,两个进程需要 运行 同时 。与其他命名的内核对象(事件、互斥体等)一样,映射对象有一个与之关联的引用计数,其中每个打开的句柄都会增加引用计数。所有句柄关闭后,对象被销毁。

因此,当第一个应用取消映射其视图并关闭其对映射对象的句柄时,如果第二个应用尚未将其自己的句柄打开到同一映射对象,则该对象将被销毁。因此,当第二个应用程序试图打开它时,该对象将不存在。