我正在尝试将一个互斥句柄传递给子进程通过命令行或任何其他方式。如何将句柄传递给子进程
我该怎么做? 如何从孩子接入互斥体?
这是我如何创建子进程:
HANDLE ghMutex;
if(!CreateProcess(_T("C:\\Users\\Kumppler\\Documents\\Visual Studio 2010\\Projects\\teste3\\Debug\\teste3.exe"), // No module name (use command line)
aux2, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
TRUE, // Set handle inheritance to TRUE
STARTF_USESTDHANDLES, // inherit the standard input, standard output, and standard error handles
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si[j], // Pointer to STARTUPINFO structure
&pi[j]) // Pointer to PROCESS_INFORMATION structure
)
编辑:
我需要使用互斥超过一个子进程,可以吗?
因此,这里是我在做什么现在:
HANDLE ghMutex;
int mutex;
char mutexstring[7];
mutex=(int)ghMutex;
itoa(mutexValue,mutexString,10);
我会通过mutexString槽命令行,然后将其转换回子过程:
mutexValue=atoi(argv[2]);
Mutex=(HANDLE)mutexValue;
我的问题,(HANDLE)铸造可以吗?
对于已命名的互斥锁。他们是明显的解决方案。通过命令行传递一个GUID以避免碰撞是矫枉过正的:只需在每个程序中对其进行硬编码:无论如何它们都是唯一的。 –
“子进程然后可以将命令行字符串转换回一个值,并简单地开始使用它”您的意思是将其转换为字符串? – Caio
@Serge - appTranslator:对名称进行硬编码有两个问题。 (1)它将您限制为您的应用程序的单个实例。 (2)如果攻击者在您之前创建了互斥体(如我的答案中所述),则拒绝服务漏洞。 –