Solved

Createfile and Sharing

Posted on 1997-04-16
1
780 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

947 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

24 Experts available now in Live!

Get 1:1 Help Now