Solved

Createfile and Sharing

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ mouse_event mouse look 7 83
Why isn't object file created? 6 59
Copy output image from TWindowsMediaPlayer 6 41
Android development question 2 37
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 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