Python |停止用户杀进程
这里提到了一个非常酷的功能:
有谁知道如何将此 C++ 代码转换为 Python (或重新编辑它,以便它至少在 C/C++ 中编译,假设这就是什么这是 在):
static const bool ProtectProcess()
{
HANDLE hProcess = GetCurrentProcess();
EXPLICIT_ACCESS denyAccess = {0};
DWORD dwAccessPermissions = GENERIC_WRITE|PROCESS_ALL_ACCESS|WRITE_DAC|DELETE|WRITE_OWNER|READ_CONTROL;
BuildExplicitAccessWithName( &denyAccess, _T("CURRENT_USER"), dwAccessPermissions, DENY_ACCESS, NO_INHERITANCE );
PACL pTempDacl = NULL;
DWORD dwErr = 0;
dwErr = SetEntriesInAcl( 1, &denyAccess, NULL, &pTempDacl );
// check dwErr...
dwErr = SetSecurityInfo( hProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pTempDacl, NULL );
// check dwErr...
LocalFree( pTempDacl );
CloseHandle( hProcess );
return dwErr == ERROR_SUCCESS;
}
A very cool function was mentioned here:
Prevent user process from being killed with "End Process" from Process Explorer
Does anyone know how to translate this C++ code to Python (or re-edit it so that it at least compiles in C/C++, assuming that is what it is in):
static const bool ProtectProcess()
{
HANDLE hProcess = GetCurrentProcess();
EXPLICIT_ACCESS denyAccess = {0};
DWORD dwAccessPermissions = GENERIC_WRITE|PROCESS_ALL_ACCESS|WRITE_DAC|DELETE|WRITE_OWNER|READ_CONTROL;
BuildExplicitAccessWithName( &denyAccess, _T("CURRENT_USER"), dwAccessPermissions, DENY_ACCESS, NO_INHERITANCE );
PACL pTempDacl = NULL;
DWORD dwErr = 0;
dwErr = SetEntriesInAcl( 1, &denyAccess, NULL, &pTempDacl );
// check dwErr...
dwErr = SetSecurityInfo( hProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pTempDacl, NULL );
// check dwErr...
LocalFree( pTempDacl );
CloseHandle( hProcess );
return dwErr == ERROR_SUCCESS;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您发布的代码的相当粗略的 ctypes 翻译。它甚至看起来有效!请注意,我删除了对 CloseHandle 的调用,这是完全错误的。您不应在伪句柄上调用
CloseHandle
,这就是GetCurrentProcess
返回。Here is a rather crude ctypes translation of the code you posted. It even appears to work! Note that I remove the call to
CloseHandle
which is simply wrong. You should not callCloseHandle
on a pseudo-handle, which is whatGetCurrentProcess
returns.使用 ctypes 怎么样?您也可以尝试 pywin32。您还可以尝试使用 IronPython。对于 ActivePython,有 win32api。
另外,我不知道你为什么想要实现这一目标背后的原因,这意味着可能有一些更优雅的解决方案可用。
How about using ctypes? You could also try pywin32. You could also try using IronPython. For ActivePython there is win32api.
Also, I don't know your reasoning behind why you want to achieve that, which means there is potentially some more elegant solution available.