question

PraveerKumar-8208 avatar image
0 Votes"
PraveerKumar-8208 asked PraveerKumar-8208 answered

[VC++/MFC] How can a child process can access a file which is exclusively locked by the Parent Process?

Hi,
Is it possible for a child process to access a file which is exclusively locked by the ParentProcess.

This is how test.txt file has been given the permission
::CreateFile("C\\temp\\test.txt",
GENERIC_READ | GENERIC_WRITE,
0, // exclusive
NULL, // default security
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
By doing this other process can not open test.txt file. Now I want to create a childProcess and I want only the child process to read "test.txt" file

This is how I am creating the childProcess:
CreateProcess("FileReader.exe",
"C:\\temp\\test.txt",
&sa,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi)

but it is not able to read the file.

is there anyone who can help me on this ?


c++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

Another method is to create the file with an inheritable handle and pass that handle to the child process. A simple way to do this is to pass it as a command line parameter. Don't forget that you must pass a SECURITY_ATTRIBUTES structure that specifies TRUE for bInheritHandle to CreateFile and also pass TRUE to the bInheritHandles parameter of the call to CreateProcess.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @RLWA32-6355 I tried this but it did not work for me . let me know if I am doing something wrong here


m_sa.nLength = sizeof(SECURITY_ATTRIBUTES);
m_sa.lpSecurityDescriptor = NULL;
m_sa.bInheritHandle = TRUE;

// set permission
m_hWriterLockFile = ::CreateFile(m_sWriterLockFilePath,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive
&m_sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

// Create a child Process


CreateProcess("FileReader.exe",
"C:\\temp\\test.txt",
&m_sa,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi)

0 Votes 0 ·

The call to CreateProcess does not pass the inheritable file handle in the command line. Passing the SECURITY_ATTRIBUTES struct results in CreateProcess returning inheritable process and thread handles in the PROCESS_INFORMATION struct.

Because this site would not let me post sample code I have shared a VS2019 solution that demonstrates the suggested method at https://1drv.ms/u/s!AmnqrCFBv4nDgggoNK8X3MYKOkGp?e=yEbJhg


0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered

I think that one of the approaches is:

  • The second process is started using CREATE_SUSPENDED parameter.

  • The first process duplicates the handle:

  DuplicateHandle( GetCurrentProcess( ), h, pi.hProcess, &h2, 0, TRUE, DUPLICATE_SAME_ACCESS );

  • The first process calls ResumeThread( pi.hThread ) and passes the value of h2 to the second process using some inter-process communication mechanism. In the simplest example, the processes use an intermediate text or binary file that contains the numeric value of h2.

(Note that the current file position will be the same for both processes).


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PraveerKumar-8208 avatar image
0 Votes"
PraveerKumar-8208 answered

@RLWA32-6355 Thanks For the help. It works now.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.