I have an FILE_NOTIFY_INFORMATION struct which i have filled like this:
FILE_NOTIFY_INFORMATION* fni = new FILE_NOTIFY_INFORMATION;
fni->Action = 1;
wcscpy_s(fni->FileName,12, L"example.txt");
fni->FileNameLength = 12;
fni->NextEntryOffset = 0;
I have then castet this Struct to an std::byte*.
auto fni_as_byte = reinterpret_cast<std::byte*>(fni);
Now i want to put this fni_as_byte into an vector of std::vector<std::byte>.
Because i need this for testing purpose.
Normally you receive the FILE_NOTIFY_INFORMATION for example from the ReadDirectoryChangesW function.
And it's called like this:
std::vector<std::byte> notifyBuffer(1024);
res = ReadDirectoryChangesW(handle, notifyBuffer.data(), static_cast<DWORD>(notifyBuffer.size()), false, FILE_NOTIFY_CHANGE_FILE_NAME, nullptr, &overlapped, nullptr);
So how can i manually copy the castet FILE_NOTIFY_INFORMATION into the std::vector<std::byte>?
Your code is corrupting memory, as the actual size of
FILE_NOTIFY_INFORMATIONis variable length. See Why do some structures end with an array of size 1?. You are not allocating enough memory to hold the string you are copying into theFileName. To allocate yourFILE_NOTIFY_INFORMATIONcorrectly, you would need to do something more like this instead:That being said, to copy the
FILE_NOTIFY_INFORMATIONinto astd::vector, you can do it like this:Alternatively, just use the
std::vectorup front as the memory for the originalFILE_NOTIFY_INFORMATION: