windows 如何使我的程序与 DEP 兼容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/350977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:50:01  来源:igfitidea点击:

How to make my program DEP-compatible?

windowswinforms

提问by P a u l

I have a windows forms (.net 3.0) project that won't run on my customer's vista computer due to a DEP error. It runs on my vista machine, and in a clean version of vista sp1 in a virtual machine. I am having trouble tracking down ways to make my program DEP, Data Execution Prevention compatible. I really can't do anything to end user machines, it just has to run. Is there any way out of this latest vista development nightmare? My program uses devexpress controls, sql express, and the .net ie web browser control. I've already jumpered out the ie control, but to no avail. I have other program that use devexpress and sql express on that same machine and they run ok. I am at a loss to debug this on the user's computer.

我有一个 Windows 窗体 (.net 3.0) 项目,由于 DEP 错误,该项目无法在我客户的 vista 计算机上运行。它在我的 vista 机器上运行,并在虚拟机中运行 vista sp1 的干净版本。我无法找到使我的程序 DEP、数据执行保护兼容的方法。我真的不能对最终用户机器做任何事情,它只能运行。有没有办法摆脱这个最新的vista开发噩梦?我的程序使用 devexpress 控件、sql express 和 .net ie web 浏览器控件。我已经跳出 ie 控件,但无济于事。我有其他程序在同一台机器上使用 devexpress 和 sql express,它们运行正常。我无法在用户的计算机上调试它。

回答by HTTP 410

DEP runs in one of two modes:

DEP 以以下两种模式之一运行:

1) Hardware DEP is for CPUs that can mark memory pages as non-executable. This helps to prevent certain exploits such as buffer overflows.

1) 硬件 DEP 适用于可以将内存页标记为不可执行的 CPU。这有助于防止某些漏洞,例如缓冲区溢出。

2) Software DEP is for CPUs that do not have hardware DEP support. It doesn't prevent execution of code in data pages, but instead stops SEH overwrite (another type of attack).

2) 软件 DEP 适用于不支持硬件 DEP 的 CPU。它不会阻止执行数据页中的代码,而是阻止 SEH 覆盖(另一种类型的攻击)。

On Windows XP with CPUs that support it, hardware DEP is enabled by default only for certain Windows system binaries, and also for programs that choose to "opt-in".

在 CPU 支持的 Windows XP 上,默认情况下仅对某些 Windows 系统二进制文件以及选择“选择加入”的程序启用硬件 DEP。

On Vista with CPUs that support it, hardware DEP is enabled by default for nearly all processes. This can occasionally be problematic, usually for older programs and drivers, and for ISVs that haven't done any Vista testing.

在具有支持它的 CPU 的 Vista 上,默认情况下几乎所有进程都启用了硬件 DEP。这偶尔会出现问题,通常对于较旧的程序和驱动程序,以及未进行任何 Vista 测试的 ISV。

So I suspect that the first step is to discover whether you're dealing with software or hardware DEP. Also, are you using C#/VB or Managed C++? And are you using any native code or components? If your application uses a native component or an ActiveX control that was built using the old ATL framework, then it's quite possible that your application will fail with hardware DEP.

所以我怀疑第一步是确定您是在处理软件还是硬件 DEP。另外,您使用的是 C#/VB 还是托管 C++?您是否使用任何本机代码或组件?如果您的应用程序使用本机组件或使用旧 ATL 框架构建的 ActiveX 控件,那么您的应用程序很可能会因硬件 DEP 而失败。

Since .NET Framework 2.0 SP1, I believe that the C# compiler emits managed code which is DEP-compatible. But if your application is generating DEP exceptions, then you can try clearing the IMAGE_DLLCHARACTERISTICS_NX_COMPAT flag for your executable. To do this you use EDITBIN.EXE from the VC toolset like so:

从 .NET Framework 2.0 SP1 开始,我相信 C# 编译器会发出与 DEP 兼容的托管代码。但是,如果您的应用程序正在生成 DEP 异常,那么您可以尝试清除可执行文件的 IMAGE_DLLCHARACTERISTICS_NX_COMPAT 标志。为此,您可以使用 VC 工具集中的 EDITBIN.EXE,如下所示:

editbin.exe /NXCOMPAT:NO <your binary>

If you're using Visual Studio, you can add a post-build step to your executable's project. You'll need to setup the environment so that EDITBIN's dependencies can be resolved. When I'm using native code as part of my app, the post-build step looks like this:

如果您使用的是 Visual Studio,则可以向可执行文件的项目添加构建后步骤。您需要设置环境,以便可以解决 EDITBIN 的依赖项。当我在应用程序中使用本机代码时,构建后步骤如下所示:

call $(DevEnvDir)..\tools\vsvars32.bat
editbin.exe /NXCOMPAT:NO $(TargetPath)  

回答by Hans Passant

The compilers that shipped with .NET 2.0 SP1 turn on the NXCOMPAT flag in the executable file header. You can turn that flag off in a Post Build step by running EditBin.exe with the /NXCOMPAT:NO option.

.NET 2.0 SP1 附带的编译器打开可执行文件头中的 NXCOMPAT 标志。您可以通过运行带有 /NXCOMPAT:NO 选项的 EditBin.exe 在 Post Build 步骤中关闭该标志。

回答by mackenir

Older versions of ATL are not DEP aware, so if you use any ActiveX controls that are built using ATL, and were built on that version of ATL (version 7.1 and below, I think), you'll get DEP errors.

旧版本的 ATL 不支持 DEP,因此如果您使用任何使用 ATL 构建的 ActiveX 控件,并且构建在该版本的 ATL(我认为是 7.1 及以下版本)上,您将收到 DEP 错误。

As a last resort, you can in fact disable DEP for the process by calling an API function: SetProcessDEPPolicy.

作为最后的手段,您实际上可以通过调用 API 函数来禁用进程的 DEP:SetProcessDEPPolicy

More information on SetProcessDEPPolicy

有关SetProcessDEPPolicy 的更多信息

回答by EricLaw

FWIW, it's worth explicitly mentioning that in many cases, applications aren't "incompatible with DEP" but rather were about to crash anyway and DEP "dove in to save the day." Very often, once you disable DEP, you'll find that you're hitting an "ordinary" AV.

FWIW,值得明确提及的是,在许多情况下,应用程序并非“与 DEP 不兼容”,而是无论如何都将崩溃,而 DEP“为挽救局面而介入”。很多时候,一旦您禁用 DEP,您会发现您正在击中“普通”AV。

If your project is written solely in .NET 3.0, this is almost certainly the case, because .NET doesn't do any of the "crazy" things that trigger DEP (e.g. function thunking, etc).

如果您的项目完全是用 .NET 3.0 编写的,那么几乎可以肯定是这种情况,因为 .NET 不会做任何触发 DEP 的“疯狂”事情(例如函数重块等)。

To debug, install a debugger or enable Watson to generate a .DMP file, then take that .DMP file to the developer's machine and figure out what went wrong.

要进行调试,请安装调试器或让 Watson 生成一个 .DMP 文件,然后将该 .DMP 文件带到开发人员的机器上并找出问题所在。

回答by Kris Kumler

Start by trying to figure out where and how your program is failing. Can you replicate the issue on your system? With enabling DEP for the application on your system? When you can replicate the issue and get the error (access violation), you can look to fixing your program.

首先尝试找出您的程序在哪里以及如何失败。你能在你的系统上复制这个问题吗?为系统上的应用程序启用 DEP?当您可以复制问题并获得错误(访问冲突)时,您可以考虑修复您的程序。

See the MSDN article for information on DEP.

有关DEP 的信息,请参阅MSDN 文章