EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Create unrelated file from OnCreateFileC / OnOpenFileC

Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.
Posted: 09/06/2013 07:28:54
by Franck Legardien (Basic support level)
Joined: 09/06/2013
Posts: 2

What i am trying to achieve is this:
- everytime a file "toto.txt" is created, i want to create another file "toto.txt.bak"
- everytime a file "toto.txt" is opened, i want to open simultaneously "toto.txt.bak" as well
- everytime a file is written to, i want to save the modifications in both "toto.txt" and "toto.txt.bak"

I succeed to create "toto.txt.bak", but i fail to write to this file.

To do that, i have the following code:

void OnCreateFileC (CallbackFilter* io_CallbackFilterPtr, LPWSTR FileName) {
HANDLE HisHandle = io_CallbackFilterPtr->CreateNonCbFile (PeerFilename.c_str(), GENERIC_READ | GENERIC_WRITE, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL);
//HANDLE HisHandle = CreateFileW (PeerFilename.c_str(), *DesiredAccess, *CreateDisposition, *FileAttributes);
//HANDLE HisHandle = io_CallbackFilterPtr->CreateFileW (PeerFilename.c_str());
if (NULL == HisHandle) {
WCHAR text[MAX_PATH * 2];
DWORD LastError = GetLastError ();
wsprintfW(text, L"+++ Error: CreateFileW failed [File=%s|GetLastError=%d]", i_Filename, LastError);
AddToLog (text);
else {
CloseHandle (HisHandle);
HisHandle = io_CallbackFilterPtr->OpenFile (PeerFilename.c_str());
if (NULL == HisHandle) {
WCHAR text[MAX_PATH * 2];
DWORD LastError = GetLastError ();
wsprintfW(text, L"+++ Error: OpenFile failed [File=%s|GetLastError=%d]", i_Filename, LastError);
AddToLog (text);
else {
DWORD NumberOfBytesWritten;
std::string FakeString = "toto";
if (! WriteFile (HisHandle, FakeString.c_str(), FakeString.size(), &NumberOfBytesWritten, NULL)) {
//if (! io_CallbackFilterPtr->WriteFile (HisHandle, FakeString.c_str(), FakeString.size(), &NumberOfBytesWritten, NULL)) {
DWORD Error = GetLastError();
WCHAR text[MAX_PATH * 2];
wsprintfW(text, L"+++ Error: CreatePeerEmptyFile: WriteFile failed [GetLastError=%d]", Error);
CloseHandle (HisHandle);


i try to write to the file directly after it's creation for test purpose.
As you can notice, i tried with several different methods:
- io_CallbackFilterPtr->CreateNonCbFile
- CreateFileW
- io_CallbackFilterPtr->CreateFileW
and for the write :
- WriteFile
- io_CallbackFilterPtr->WriteFile

But all these methods give : an empty file, and impossible to write into it.
What is the method to use to achieve this ?
Posted: 09/06/2013 07:48:59
by Vladimir Cherniga (EldoS Corp.)

CreateFileW should work in you case, as well as CreateNonCbFile(will check this method for the possible problems). But you should specify DesiredAccess with a proper flags, so that writing is possible to the target file. The requested flags in OnCreateCallbackC may not satisfy write access.



Topic viewed 1440 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!