0

I have following code, after CopyMemory my pBuf is empty, it contains no data from newCommand, or when I want to add something in that struct it doesnt work... What am I doing wrong?

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);

Here is CopyFiles function, where I send (*handlesInstance.pBuf) and I want to set values of pBuf here, to create path, to copy file later

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;
    }
}
}
Jakub
  • 285
  • 4
  • 21
  • Any chance you could describe what "doesn't work" and how what it does is different from what you expect? – Mats Petersson Nov 06 '14 at 21:51
  • I wanted to have pointer to StCommand struct in pBuf. But when I try to acces some data using (*handlesInstance.pBuf) I get nothing. Even if I change these NULLs - StCommand newCommand = { NULL, NULL, NULL, false } for some text... – Jakub Nov 06 '14 at 21:54
  • What does "get nothing" mean? And how, where are you "using" these commands - it doesn't appear to be in the code you have posted so far. – Mats Petersson Nov 06 '14 at 21:57
  • I send (*handlesInstance.pBuf) to function called CopyFiles... where I want to set FileName, SourcePath, SourceFile,... and build the path to copy file. And when I want to print FileName, SourcePath or SourceFile I get NULL... also if I change newCommand like I said in comment above. Is it explained better now? – Jakub Nov 06 '14 at 22:02
  • Presumably `StCommand` has some pointers in it (e.g. `LPCWSTR`) - using `CopyMemory` to copy it won't copy the data (strings) they point to, it will only copy the pointers which will be meaningless out of context. – Jonathan Potter Nov 06 '14 at 22:09
  • But when it will copy the pointers, why I cant set the data they are pointing to? – Jakub Nov 06 '14 at 22:13
  • Given just this code, you're using the paging file for backing. A successful initial mapping into the paging file on inception is zero-filled. If you're wondering why all your "pointers" are NULL, there's your reason. From the [Remarks section of the documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537(v=vs.85).aspx), "The initial contents of the pages in a file mapping object backed by the operating system paging file are 0 (zero)." – WhozCraig Nov 06 '14 at 22:27
  • OK, but again, when I try to set some of the values of struct, why it is still NULL? I still dont get it – Jakub Nov 06 '14 at 22:29
  • In the code you've shown there's no reason for the data to be anything other than NULL, since `newCommand` is initialised to all NULLs. Show your real code if you want us to help. – Jonathan Potter Nov 06 '14 at 22:38
  • So I have added CopyFiles function for you to understand what I am doing. I want to set values in pBuf and then use them to build the path to file which I want to copy – Jakub Nov 06 '14 at 22:47
  • Ok, if I understand correctly, all you should have to do is change the signature of the `CopyFiles` function to `void CopyFiles(StCommand& stCommand)`. – Jonathan Potter Nov 06 '14 at 23:10
  • I tried and still nothing – Jakub Nov 08 '14 at 12:07

0 Answers0