Click here to Skip to main content
15,868,016 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: using static library for C code in Visual studio 2005 Pin
sunny_vc12-Sep-12 2:42
sunny_vc12-Sep-12 2:42 
AnswerRe: using static library for C code in Visual studio 2005 Pin
jschell12-Sep-12 8:04
jschell12-Sep-12 8:04 
GeneralRe: using static library for C code in Visual studio 2005 Pin
Malli_S12-Sep-12 20:01
Malli_S12-Sep-12 20:01 
GeneralRe: using static library for C code in Visual studio 2005 Pin
sunny_vc12-Sep-12 23:55
sunny_vc12-Sep-12 23:55 
GeneralRe: using static library for C code in Visual studio 2005 Pin
jschell14-Sep-12 8:38
jschell14-Sep-12 8:38 
QuestionHow to lock a file when it is in use? Pin
tagopi12-Sep-12 0:46
tagopi12-Sep-12 0:46 
AnswerRe: How to lock a file when it is in use? Pin
Malli_S12-Sep-12 1:56
Malli_S12-Sep-12 1:56 
AnswerRe: How to lock a file when it is in use? Pin
Software_Developer13-Sep-12 5:43
Software_Developer13-Sep-12 5:43 
[LockFile] locks the second file to prevent another process from accessing it while writing to it.


Sample code:

C#
#include <windows.h>
#include <stdio.h>

void main()
{
	HANDLE hFile;
	HANDLE hAppend;
	DWORD  dwBytesRead, dwBytesWritten, dwPos;
	BYTE   buff[4096];
	char DataBuffer[] = "This is some test data to write to the file.";
	DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
 

	BOOL bErrorFlag = FALSE;

  // Open the existing file.

  hFile = CreateFile(TEXT("one.txt"), // open One.txt
            GENERIC_READ,             // open for reading
            0,                        // do not share
            NULL,                     // no security
            CREATE_ALWAYS,            // existing file only
            FILE_ATTRIBUTE_NORMAL,    // normal file
            NULL);                    // no attr. template

  if (hFile == INVALID_HANDLE_VALUE)
  {

     printf("Could not open One.txt."); 
     return;
  }
    bErrorFlag = WriteFile( 
                    hFile,           // open file handle
                    DataBuffer,      // start of data to write
                    dwBytesToWrite,  // number of bytes to write
                    &dwBytesWritten, // number of bytes that were written
                    NULL);            // no overlapped structure

  // Open the existing file, or if the file does not exist,
  // create a new file.

  hAppend = CreateFile(TEXT("two.txt"), // open Two.txt
              FILE_APPEND_DATA,         // open for writing
              FILE_SHARE_READ,          // allow multiple readers
              NULL,                     // no security
              CREATE_ALWAYS,              // open or create
              FILE_ATTRIBUTE_NORMAL,    // normal file
              NULL);                    // no attr. template

  if (hAppend == INVALID_HANDLE_VALUE)
  {
     printf("Could not open Two.txt."); 
     return;
  }

  // Append the first file to the end of the second file.
  // Lock the second file to prevent another process from
  // accessing it while writing to it. Unlock the
  // file when writing is complete.

  while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)
      && dwBytesRead > 0)
    {
    dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
    LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
    WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NULL);
    UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
    }

  // Close both files.

  CloseHandle(hFile);
  CloseHandle(hAppend);
}

QuestionGetting List of Specific Installed Programs Pin
AmbiguousName11-Sep-12 6:02
AmbiguousName11-Sep-12 6:02 
AnswerRe: Getting List of Specific Installed Programs Pin
Michael_Lu11-Sep-12 8:21
Michael_Lu11-Sep-12 8:21 
QuestionCan't select HBRUSH into device context Pin
Alan Balkany11-Sep-12 5:38
Alan Balkany11-Sep-12 5:38 
AnswerRe: Can't select HBRUSH into device context Pin
Jochen Arndt11-Sep-12 6:04
professionalJochen Arndt11-Sep-12 6:04 
GeneralRe: Can't select HBRUSH into device context Pin
Alan Balkany11-Sep-12 6:10
Alan Balkany11-Sep-12 6:10 
GeneralRe: Can't select HBRUSH into device context Pin
Jochen Arndt11-Sep-12 7:07
professionalJochen Arndt11-Sep-12 7:07 
GeneralRe: Can't select HBRUSH into device context Pin
Alan Balkany11-Sep-12 7:08
Alan Balkany11-Sep-12 7:08 
AnswerRe: Can't select HBRUSH into device context Pin
enhzflep11-Sep-12 10:20
enhzflep11-Sep-12 10:20 
GeneralRe: Can't select HBRUSH into device context Pin
Alan Balkany11-Sep-12 10:23
Alan Balkany11-Sep-12 10:23 
GeneralRe: Can't select HBRUSH into device context Pin
enhzflep11-Sep-12 10:30
enhzflep11-Sep-12 10:30 
GeneralRe: Can't select HBRUSH into device context Pin
Alan Balkany13-Sep-12 10:47
Alan Balkany13-Sep-12 10:47 
QuestionFile download help Pin
Sunil P V10-Sep-12 1:04
Sunil P V10-Sep-12 1:04 
QuestionRe: File download help Pin
David Crow10-Sep-12 2:38
David Crow10-Sep-12 2:38 
AnswerRe: File download help Pin
Sunil P V10-Sep-12 19:04
Sunil P V10-Sep-12 19:04 
GeneralRe: File download help Pin
_Flaviu10-Sep-12 21:01
_Flaviu10-Sep-12 21:01 
QuestionRe: File download help Pin
David Crow11-Sep-12 3:06
David Crow11-Sep-12 3:06 
QuestionHow to set Image' Document Size ? Pin
002comp9-Sep-12 22:18
002comp9-Sep-12 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.