如何CopyMemory使用FileMapping复制struct?

时间:2014-11-06 21:40:08

标签: c winapi file-mapping

我有以下代码,在CopyMemory之后我的pBuf是空的,它不包含来自newCommand的数据,或者当我想在该结构中添加某些东西时它不起作用......我做错了什么?

struct StCommand
{
TCHAR sourcePath[MAX_PATH];
TCHAR sourceFile[MAX_PATH];
TCHAR fileName[MAX_PATH];
  bool endThread;
};

   handlesInstance.mapFileHandle = CreateFileMapping(
        INVALID_HANDLE_VALUE,    // use paging file
        NULL,                    // default security
        PAGE_READWRITE,          // read/write access
        0,                       // maximum object size (high-order DWORD)
        sizeof(StCommand),                // maximum object size (low-order DWORD)
        handlesInstance.valueBuffer);                 // name of mapping object

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

    handlesInstance.pBuf = (StCommand*) MapViewOfFile(
        handlesInstance.mapFileHandle,   // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,
        0,
        sizeof(StCommand));

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

    StCommand newCommand = { NULL, NULL, NULL, false };

    CopyMemory(handlesInstance.pBuf, &newCommand, sizeof(StCommand));

    CopyFiles(*handlesInstance.pBuf);

这里是CopyFiles函数,我发送(* handlesInstance.pBuf),我想在这里设置pBuf的值,创建路径,以后再复制文件

void CopyFiles(StCommand stCommand)
{
while (!stCommand.endThread)
{
    DWORD dwWaitEventResult;

    dwWaitEventResult = WaitForSingleObject(handlesInstance.dataInEvent, INFINITE);

    switch (dwWaitEventResult)
    {
        // Event object was signaled
    case WAIT_OBJECT_0:
    {
        //Command processing
        if (!stCommand.endThread)
        {
            GetSzSetting(pathValueName, REPOSITORY_PATH);
            TCHAR newFile[MAX_PATH];
            _tcscpy_s(newFile, handlesInstance.valueBuffer);
            _tcscat_s(newFile, _T("/"));
            TCHAR buffer[MAX_PATH];
            swprintf_s(buffer, _T("%d"), GetDwordSetting(indexValueName, 0));
            _tcscat_s(newFile, buffer);
            _tcscat_s(newFile, _T("."));
            _tcscat_s(newFile, stCommand.fileName);

            TCHAR oldFile[MAX_PATH];
            _tcscpy_s(oldFile, stCommand.sourcePath);
            _tcscat_s(oldFile, _T("/"));
            _tcscat_s(oldFile, stCommand.fileName);

            if (CopyFile(oldFile, newFile, FALSE))
            {
                _tprintf(_T("File %s copied successfully into repository.\n"), stCommand.fileName);
                SetDwordSetting(indexValueName, GetDwordSetting(indexValueName, 0) + 1);
            }
            else
            {
                _tprintf(_T("Could not copy file %s.\n"), stCommand.fileName);
                _tprintf(_T("Failed with error code: %d\n"), GetLastError());
            }
        }

        ResetEvent(handlesInstance.dataInEvent);

        if (!SetEvent(handlesInstance.dataOutEvent))
        {
            printf("SetEvent OutEvent failed (%d)\n", GetLastError());
        }
        break;
    }
        // An error occurred
    default:
        printf("Wait error (%d)\n", GetLastError());
        break;
    }
}
}

0 个答案:

没有答案