Solved

Createfile and Sharing

Posted on 1997-04-16
1
771 Views
Last Modified: 2011-10-03
I need to be able to reopen a file that already has handle open. I used Creatfile to open a text file and read it no problem....but I need to be able to reopen the file for writing without dropping the handling. if I drop the handle another function that is polling the file will grab it. I've tried to initially open the file with the FILE_SHARE_WRITE
attribute but this won't work. Is there anyway to Erase the file and move the file pointer back to the beginning without dropping the handle?
Here's a bit of the code that won't work

short int WriteFile(HANDLE *FHandle,char *buffer,CString                        ,*Filename,unsigned int *towrite)


      HANDLE newhandle;
      DWORD BytesWritten,faccess,fcreat,foattrib,fsmode;
      LPDWORD BWriteptr;
      BWriteptr=&BytesWritten;
      BOOL WriteSuccess,Closep,Closec;
      faccess=GENERIC_WRITE;
        fcreat=TRUNCATE_EXISTING;          
        foattrib=FILE_ATTRIBUTE_NORMAL;
        fsmode=FILE_SHARE_WRITE;
      //CloseHandle(*FHandle); in order for this to work I                                 have to close thishandle
      newhandle = CreateFile(*Filename,
                  faccess,//GENERIC_WRITE                                fsmode,//FILE_SHARE_WRITE,
                                   NULL,
                  fcreat,//TRUNCATE_EXISTING,
            foattrib,//FILE_ATTRIBUTE_NORMAL,
                                NULL);

    if ((newhandle) != (INVALID_HANDLE_VALUE))
      {
          WriteSuccess=
      WriteFile(newhandle,buffer,(*towrite),BWriteptr,NULL);            
          if (WriteSuccess)
          {
              //Closep=CloseHandle(*FHandle);
              Closec=CloseHandle(newhandle);
              if ((Closep) /*&& (Closec=)*/ )
                return 0;


Any ideas?

0
Comment
Question by:Brentg
1 Comment
 

Accepted Solution

by:
markusk earned 50 total points
ID: 1162755
If you first open the file, you should do this by:

LPCTSTR lpszFileName = "YourFile";
DWORD   fdwAccess = GENERIC_READ;     // | GENERIC_WRITE
DWORD   fdwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
DWORD   fdwCreate = OPEN_EXISTING;    // or what you want
DWORD   fdwAttrsAndFlags = FILE_ATTRIBUTE_NORMAL;
CreateFile (lpszName, fdwAccess, fdwShareMode, NULL, fdwCreate, fdwAttrsAndFlags, NULL);


The second call should look like the following code fragment:
LPCTSTR lpszFileName = "YourFile";
DWORD   fdwAccess = GENERIC_WRITE     // | GENERIC_READ
DWORD   fdwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; // you must specfiy the FILE_SHARE_READ flag because the fist call to CreateFile opened the file in GENERIC_READ mode. If not, the function call will fail.
DWORD   fdwCreate = OPEN_EXISTING;    // or what you want
DWORD   fdwAttrsAndFlags = FILE_ATTRIBUTE_NORMAL;
CreateFile (lpszName, fdwAccess, fdwShareMode, NULL, fdwCreate, fdwAttrsAndFlags, NULL);

hope this will help...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now