且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

禁止“由于缺少X.dll而导致程序无法启动”错误弹出

更新时间:2022-01-07 17:55:52

可以使用 SetErrorMode 。但是,您必须阅读 LoadLibrary 文档发现在加载时丢失DLL符合 SEM_FAILCRITICALERRORS $>涵盖的严重错误之一c $ c>。

The dialog box can be disabled for the calling process with SetErrorMode. However, you have to read the LoadLibrary documentation to discover that "missing DLL at load time" qualifies as one of the "critical errors" covered by SEM_FAILCRITICALERRORS.

只要不使用 CREATE_DEFAULT_ERROR_MODE ,它似乎CMD.EXE创建子进程时未设置该标志。因此,在我关心的情况下,在我的Python脚本中设置启动时的错误模式实际上会抑制对话框。

The error mode inherits to child processes, as long as they are not created with CREATE_DEFAULT_ERROR_MODE, and it appears that CMD.EXE does not set that flag when it creates subprocesses. So setting the error mode on startup in my Python script does in fact suppress the dialog box in the situation I care about...

if sys.platform == 'win32':
    try:
        import ctypes
        # SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX
        ctypes.windll.kernel32.SetErrorMode(0x0001|0x0002|0x8000)
    except:
        pass

这不是***解决方案:子流程以特定的错误代码终止(0xC0000135-实际上没有记录为缺少DLL,但是显然是从搜索该编号时出现的结果),但是细节(例如缺少DLL的细节)被丢在了地板上。我仍然希望找到一个可以使加载程序将详细信息报告给stderr的设置。

This is not an optimal solution: the subprocess terminates with a particular error code (0xC0000135 — not actually documented as "missing DLL", but evidently so from what comes up when you search on that number) but the details — like which DLL is missing — are dropped on the floor. I still hope to find a setting somewhere that makes the loader report the details to stderr.