0

我有以下代码,在 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;
    }
}
}
4

0 回答 0