C# 为什么VS2017调试不会在dllmain断点处停止?

C# 为什么VS2017调试不会在dllmain断点处停止?,c#,visual-studio,winapi,dll,visual-studio-debugging,C#,Visual Studio,Winapi,Dll,Visual Studio Debugging,我第一次构建了一个dll,一切都按照预期进行,但我无法调试它 我制作了一个最简单的示例,只使用了一个dllmain()并使用windows DLL向导(非MFC)设置了项目 活动生成配置:x86调试 右键单击项目->设置为启动项目 禁用的预编译头 项目属性->调试->命令->输入的应用程序可执行文件路径 生成调试信息是“是” 这是整个DLL,没有其他源文件或头文件。当我按F5时,应用程序运行并加载dll,messagebox将触发。如果未注释debugbreak,则不会触发messagebox,

我第一次构建了一个dll,一切都按照预期进行,但我无法调试它

我制作了一个最简单的示例,只使用了一个dllmain()并使用windows DLL向导(非MFC)设置了项目

活动生成配置:x86调试

右键单击项目->设置为启动项目

禁用的预编译头

项目属性->调试->命令->输入的应用程序可执行文件路径

生成调试信息是“是”

这是整个DLL,没有其他源文件或头文件。当我按F5时,应用程序运行并加载dll,messagebox将触发。如果未注释debugbreak,则不会触发messagebox,而DLL只会成功加载。调试器不会中断。如果使用断点,它不会中断

应用程序肯定正在加载我正在构建的DLL

.pdb文件已经存在并正在生成

我尝试将调试器更改为混合、自动、本机,但仍然无法工作。调用应用程序可能是用C#编写的,但它不是我的

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files
#include <windows.h>

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{

//  __debugbreak();

    MessageBoxA(NULL, "Hit DLLMAIN", "test", MB_OK);

    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

这不是一个明确的答案,但我一直在调试本机DLL,我有几个建议:

1) 如果使用DLL的应用程序仍然处于活动状态,并且您知道如何使其再次尝试访问所述DLL,您也可以尝试“调试->附加到进程”


2) 当您点击F5进行调试时,请确保DLL没有重新编译。我曾经有一个静态库依赖项,它导致DLL在每次调试时重新编译。还有一个移动步骤,在编译时将DLL移动到目标应用程序可执行文件夹中。这导致调试器认为我没有使用完全相同版本的DLL。这可能是个时间问题。如果有移动步骤,则DLL版本必须与编译完全相同。

使用MessageBox调试代码是个坏主意。在DllMain()中使用它在形式上是非法的,如果user32.dll尚未加载,则程序将死锁。使用NULL作为第一个参数很麻烦,对话框要求所有者窗口位于顶部,如果找不到所有者窗口,则桌面将成为所有者。使您可能看不到它,因为它被另一个窗口重叠。当debugbreak()引起焦点改变时,可以随机看到它。您确实需要让调试器断点工作才能取得真正的进展,这方面存在很多问题。@HansPassant我通过编写一个程序加载dll并使用它来让调试器工作。我同意MB()。我开始在freopen stdout中使用alloconsole,但试图理顺DLL入口/出口,这样我就不会重复分配或打开。您认为什么会阻止VS通过第三方应用程序进行调试?@HansPassant-如果尚未加载user32.dll,则您的程序将死锁。-即使user32.dll尚未加载,程序也不会死锁。加载程序锁中的加载dll从不死锁,因为我们的线程已经拥有这个加载程序锁。死锁只能在我们开始等待另一个线程,并且这个线程尝试进入加载器锁时发生。@user10530562有趣的是,我创建了一个程序试图重现您的情况。我不知道我是否理解这个错误。如果
debugbreak
未注释,
debugbreak
将工作并触发断点提醒,这似乎与您遇到的情况不同。然后我继续,程序可以执行到
消息框
@StriveSun MSFT嘿,谢谢您的查看。我今天又看了一眼,似乎调试器在启动后立即分离,但程序仍在运行。请看我的最新帖子。
'Program.exe' (Win32): Loaded 'C:\Users\Tech1\AppData\Local\ProgramDir\Program.exe'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\win32u.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32full.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp_win.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbase.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\Private_API.dll'. Module was built without symbols.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\apphelp.dll'. Cannot find or open the PDB file.
'Program.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file.
The thread 0x4610 has exited with code 0 (0x0).
The thread 0x2614 has exited with code 0 (0x0).
The thread 0x45c0 has exited with code 0 (0x0).
The program '[21792] Program.exe' has exited with code 0 (0x0).