gpt4 book ai didi

子进程从其父进程继承句柄但无法写入其中

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:16 25 4
gpt4 key购买 nike

我想让 2 个进程写入同一个文件。我在第一个进程(父进程)中创建了一个文件并调用了第二个进程(子进程)。我已将文件设为可继承,第二个进程将句柄作为命令行参数继承。

虽然句柄不等于 INVALID_HANDLE_VALUE,但第二个进程无法对该文件做任何事情。 WriteFile() 以错误 6(句柄无效)结束。 LockFile(),Unlockfile()也是一样。

代码有什么问题,我该如何解决?我想让 2 个进程使用同一个文件。

过程 1:

#include <Windows.h>
#include <process.h>

int _tmain(int argc, _TCHAR* argv[])
{
_SECURITY_ATTRIBUTES sec_attr;
sec_attr.nLength = sizeof(_SECURITY_ATTRIBUTES);
sec_attr.bInheritHandle = true;
sec_attr.lpSecurityDescriptor = 0;
//argv[1] - the name of the file I want to create
HANDLE h = CreateFile(argv[1],GENERIC_READ | GENERIC_WRITE,FILE_SHARE_WRITE |
FILE_SHARE_READ,&sec_attr,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);

STARTUPINFO sinfo;
ZeroMemory(&sinfo,sizeof(sinfo));
sinfo.cb = sizeof(sinfo);

PROCESS_INFORMATION pinfo;

char cmd[300] = "PATH to process 2";
CreateProcess(0,cmd,0,0,true,NORMAL_PRIORITY_CLASS,0,0,&sinfo,&pinfo);
char c = '1';
DWORD w;
WriteFile(h,&c,1,&w,0); //works fine
...
}

过程 2:

#include <Windows.h>
#include <process.h>

int _tmain(int argc, _TCHAR* argv[])
{
HANDLE h = argv[1];
if(h == INVALID_HANDLE_VALUE) {
//it's okay, h is NOT equal to INVALID_HANDLE_VALUE
}
char c = '2';
DWORD w;
WriteFile(h,&c,1,&w,0); //error 6
...
}

附言第一个进程成功写入文件。到那时第二个过程已经失败。

最佳答案

您可能正在创建具有可继承文件句柄的子进程,但实际上并没有告诉子进程该句柄的值是什么! child 正在 argv[1] 中寻找该值,但您显示的代码未在该参数中传递任何值!即使有,您也无法正确检索该值。

命令行参数始终是字符串,因此 argv[1] 是指向以 null 结尾的字符串的指针,而不是句柄。当你把它放在命令行上时,你必须将句柄指针转换为字符串表示,然后你必须将该字符串解析回指针命令行。

尝试更像这样的东西:

过程 1:

#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
SECURITY_ATTRIBUTES sec_attr = {};
sec_attr.nLength = sizeof(sec_attr);
sec_attr.bInheritHandle = TRUE;
sec_attr.lpSecurityDescriptor = 0;

//argv[1] - the name of the file I want to create
HANDLE h = CreateFile(argv[1], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, &sec_attr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (h == INVALID_HANDLE_VALUE)
{
// handle the error ...
return 1;
}

STARTUPINFO sinfo;
ZeroMemory(&sinfo, sizeof(sinfo));
sinfo.cb = sizeof(sinfo);

PROCESS_INFORMATION pinfo;
ZeroMemory(&pinfo, sizeof(pinfo));

_TCHAR cmd[MAX_PATH + 16] = {};
_tsprintf(cmd, _T("\"%s\" %p"), _T("PATH to process 2"), h);

// TODO: consider using STARTUPINFOEX instead so the child inherits
// ONLY the file handle and not ALL inheritable handles it doesn't
// care about:
//
// Programmatically controlling which handles are inherited by new processes in Win32
// https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873
//
if (!CreateProcess(0, cmd, 0, 0, TRUE, NORMAL_PRIORITY_CLASS, 0, 0, &sinfo, &pinfo))
{
// handle the error ...
return 1;
}

CloseHandle(pinfo.hThread);
CloseHandle(pinfo.hProcess);

char c = '1';
DWORD w;
WriteFile(h, &c, 1, &w, 0);
...

CloseHandle(h);
return 0;
}

过程 2:

#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 2)
{
// ...
return 1;
}

//argv[1] - string representation of a handle to an open file
HANDLE h = INVALID_HANDLE_VALUE;
_stscanf(argv[1], _T("%p"), &h);

char c = '2';
DWORD w;
WriteFile(h, &c, 1, &w, 0);
...

CloseHandle(h);
return 0;
}

也就是说,让两个线程共享一个文件句柄并不是一个好主意,更不用说两个进程了。给定的文件句柄中只有 1 个读/写位置,因此共享该句柄会使线程/进程冒着相互踩踏的风险,除非您在它们之间提供某种同步,以便只有一个人可以同时访问该文件时间。

考虑将目标文件名传递给子进程,让它打开自己唯一的文件句柄,例如:

过程 1:

#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 2)
{
// ...
return 1;
}

//argv[1] - the name of the file I want to create
HANDLE h = CreateFile(argv[1], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (h == INVALID_HANDLE_VALUE)
{
// handle the error ...
return 1;
}

STARTUPINFO sinfo;
ZeroMemory(&sinfo, sizeof(sinfo));
sinfo.cb = sizeof(sinfo);

PROCESS_INFORMATION pinfo;
ZeroMemory(&pinfo, sizeof(pinfo));

_TCHAR cmd[(MAX_PATH *2) + 10] = {};
_tsprintf(cmd, _T("\"%s\" \"%s\""), _T("PATH to process 2"), argv[1]);

if (!CreateProcess(0, cmd, 0, 0, TRUE, NORMAL_PRIORITY_CLASS, 0, 0, &sinfo, &pinfo))
{
// handle the error ...
return 1;
}

char c = '1';
DWORD w;
WriteFile(h, &c, 1, &w, 0);
...

CloseHandle(h);
return 0;
}

过程 2:

#include <Windows.h>
#include <process.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 2)
{
// ...
return 1;
}

//argv[1] - the name of the file I want to open
HANDLE h = CreateFile(argv[1], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (h == INVALID_HANDLE_VALUE)
{
// handle the error ...
return 1;
}

char c = '2';
DWORD w;
WriteFile(h, &c, 1, &w, 0);
...

CloseHandle(h);
return 0;
}

否则,请考虑让进程 1 复制文件句柄,然后让进程 2 继承副本而不是原始文件句柄。

关于子进程从其父进程继承句柄但无法写入其中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47703225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com