Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:36:11+00:00 2026-05-24T04:36:11+00:00

i have locked my file from 0 to 5 bytes, and i write some

  • 0

i have locked my file from 0 to 5 bytes, and i write some character byte by byte but it does not stop writing even if the file is locked, i dont know what is wrong, i have tried every type of flag in createFile function, but no success, please help.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <stdlib.h>

void __cdecl _tmain(int argc, TCHAR *argv[])
{
DWORD dwBytesWritten;
HANDLE hFile;
BOOL filesize= FALSE;
LARGE_INTEGER pl_int;
PLARGE_INTEGER pli_int = &pl_int;
char * Buffer = (char *)malloc (1);
Buffer[0]='0';
printf("Detect Lock Process Started\n");
hFile = CreateFile(argv[1],         // open One.txt
          GENERIC_WRITE|GENERIC_READ,             // open for reading
          FILE_SHARE_READ | FILE_SHARE_WRITE,           // do not share
          NULL,                     // no security
          OPEN_EXISTING,            // existing file only
          FILE_ATTRIBUTE_NORMAL,    // normal file
          NULL);                    // no attr. template

if (hFile == INVALID_HANDLE_VALUE)
{
   printf("Could not open %s\n",argv[1]); 
   return;
}
filesize = GetFileSizeEx(hFile, pli_int);

__int64 fsizes= (__int64)(pli_int->QuadPart);

if (filesize == FALSE)
    printf("Could not get file size\n");
else
    _tprintf(TEXT("%s (%d bytes) \n"), argv[1], fsizes);

bool lock = LockFile(hFile,5,0,10,0);
if (lock == FALSE)
    printf("lockfailed\n");
for(int x=0; x < fsizes; x++)
{
    if (!WriteFile(hFile,Buffer,1,&dwBytesWritten,NULL))
    //if (!ReadFile(hFile,Buffer,1,&dwBytesWritten,NULL))
    {
        printf("File Locked-could not Write file at byte %d\n",x);

    }
    else
        printf("File Written at byte %d\n",x);
}
free(Buffer);
CloseHandle(hFile);

}

THE MAIN PROCESS THAT CALLS THIS PROCESS IS BELOW, i didnt mention it earlier, sorry abt that.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <stdlib.h>

void DisplayError(LPTSTR lpszFunction);

void __cdecl _tmain(int argc, TCHAR *argv[])
{
HANDLE hFile;


if(argc < 4 || argc%2 != 0) //check if low count or odd count
{
    printf("Wrong set of parameters \n");
    printf("Parameters=>  <lockfile> <File Name> (<Lock region Start value> <Lock        reigion Stop value>......)\n");
    return;
}
if (argc > 12)
{
    printf("Too many arguments\n");
    return;
}
// Open the existing file.

hFile = CreateFile(argv[1],         // open One.txt
          GENERIC_READ,             // open for reading
          FILE_SHARE_READ | FILE_SHARE_WRITE,           // do not share
          NULL,                     // no security
          OPEN_EXISTING,            // existing file only
          FILE_ATTRIBUTE_NORMAL,    // normal file
          NULL);                    // no attr. template

if (hFile == INVALID_HANDLE_VALUE)
{
   printf("Could not open %s\n",argv[1]); 
   return;
}


int i=argc-2;
int count[10];
for (int j=0; j<i; j++)
{
    count[j]=_ttoi(argv[2+j]);
}


for(int z=0; z<i; z=z+2)
{
    if(!LockFile(hFile,count[z],0,count[z+1]-count[z],0))
        DisplayError(TEXT("LockFile"));
    else
        printf("file locked from %d to %d bytes\n",count[z],count[z+1]);
} 
STARTUPINFO si;
PROCESS_INFORMATION pi;
printf("Calling Detect Lock\n");
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

LPTSTR szCmdline = _tcsdup(TEXT("C:\\detectlock\\Debug\\detectlock.exe ass23.txt"));

if( !CreateProcess( NULL,   // No module name (use command line)
    szCmdline,        // Command line
    NULL,           // Process handle not inheritable
    NULL,           // Thread handle not inheritable
    FALSE,          // Set handle inheritance to FALSE
    0,              // No creation flags
    NULL,           // Use parent's environment block
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
) 
{
    printf( "CreateProcess failed (%d)\n", GetLastError() );
    return;
}

// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
CloseHandle(hFile);
//CloseHandle(hAppend);



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:36:12+00:00Added an answer on May 24, 2026 at 4:36 am

    From the MSDN article for LockFile():

    Locks the specified file for exclusive access by the calling process.

    Well, that surely worked. Your test is not verifying if another process is locked out of the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am reading the bytes from binary file and now i have to get
I have a zip file whose contents are presented as byte[] but the original
I have a file on a Windows 2003 server which is locked by a
Does anyone have sample code to copy open (in-use and locked by another program)
I have a text file which was created using some Microsoft reporting tool. The
I have some strings in Java (originally from an Excel sheet) that I presume
I have this structure which I want to write to a file: typedef struct
I have an application where i sometimes need to read from file being written
the entries cannot be reordered because one or more of them have been locked
I have looked over the Repository pattern and I recognized some ideas that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.