将DWORD *传递给映射文件

时间:2014-03-18 18:55:57

标签: c++ winapi memory-mapped-files dword

此示例http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx传递TCHAR,但如何让它传递给DWORD?我在下面试过,但我得不能将参数1从'DWORD *'转换为'const wchar_t *'。

DWORD* pid=new DWORD[20];

    HANDLE hMapFile;
    DWORD pBuf;
    TCHAR szName[]=TEXT("Global\\mapFile");

    //il creez
    hMapFile = CreateFileMapping(
        INVALID_HANDLE_VALUE,    // use paging file
        NULL,                    // default security 
        PAGE_READWRITE,          // read/write access
        0,                       // maximum object size (high-order DWORD) 
        256,                      // maximum object size (low-order DWORD)  
        szName);                 // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError()); 
        return 1;
    }

    pBuf = (DWORD) MapViewOfFile(hMapFile,   // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,                   
        0,                   
        256);     

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

        CloseHandle(hMapFile);
        return 1;

    }

CopyMemory((LPVOID)pBuf, pid, (_tcslen(pid) * sizeof(TCHAR)));

1 个答案:

答案 0 :(得分:0)

Mapviewoffile返回LPVOID。 下一步它将起作用:

DWORD*  pBuf = (DWORD*) MapViewOfFile(hMapFile,   // handle to map object
    FILE_MAP_ALL_ACCESS, // read/write permission
    0,                   
    0,                   
    256);     

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

    CloseHandle(hMapFile);
    return 1;

}

CopyMemory((LPVOID)pBuf, pid, (20* sizeof(DWORD)));