我希望在同一个文件中写入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
...
}
P.S。第一个进程成功写入文件。到那时,第二个过程已经失败了。
答案 0 :(得分:2)
您可能正在使用可继承的文件句柄创建子进程,但实际上并没有告诉子进程该句柄的值是什么!孩子正在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继承副本而不是原始副本。