调试com可见的dll托管代码

发布于 2024-09-17 17:34:53 字数 316 浏览 3 评论 0原文

我编写了一个 COM 可见的 dll,它将从本机 Win32 程序中调用。出于调试目的,我向包含 dll 的解决方案添加了一个简单的 WinForms 客户端。

现在,当我在 dll 中设置断点时,会命中断点,但我无法单步执行代码:调试器总是跳转到 dll 中的下一个断点,或者在调用后客户端中的第一行代码DLL。

如何让调试器单步执行 dll 代码?

我认为这可能是“仅启用我的代码”选项,但未设置。

更新 jdv 建议在项目属性中设置“启用非托管代码调试”,但这并没有达到预期的效果。

谢谢,米尔。

I have written a COM visible dll, which will be called from a native Win32 program. For debugging purposes I added a simple WinForms client to the solution containing the dll.

Now when I set a breakpoint in the dll, that breakpoint is hit, but I can't step through the code: the debugger always jumps to the next breakpoint in the dll, or the first line of code in the client after the call to the dll.

How can I get the debugger to step through the dll code?

I thought it might be the "Enable Just My Code" option, but that is not set.

Update
jdv suggested setting "enable unmanaged code debugging" in the project properties, but that didn't have the desired effect.

Thanks, Miel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街九尾狐 2024-09-24 17:34:53

以下是我执行的步骤,这些步骤使我能够成功调试作为 COM 组件公开的 .NET 程序集:

首先创建一个类库,其中包含将作为 COM 对象公开的类

namespace COMTest
{
    using System;
    using System.Runtime.InteropServices;

    public interface IFoo
    {
        void Bar();
    }

    [ComVisible(true)]
    public class Foo : IFoo
    {
        public void Bar()
        {
            Console.WriteLine("Bar");
        }
    }
}

: microsoft.com/en-us/library/ms247123.aspx" rel="nofollow noreferrer">使用强密钥签署程序集并注册为 COM 对象:

regasm.exe /codebase COMTest.dll

注册 COM 对象后,您可以创建一个新控制台在新的 Visual Studio 实例中运行应用程序来测试 COM 对象:

class Program
{
    static void Main()
    {
        var type = Type.GetTypeFromProgID("COMTest.Foo");
        var instance = Activator.CreateInstance(type);
        type.InvokeMember("Bar", BindingFlags.InvokeMethod, null, instance, new object[0]);
    }
}

InvokeMember 行上放置一个断点并运行该应用程序。一旦断点被​​击中,打开 模块窗口 (Ctrl +DM)并确保为 COM 程序集加载了符号:

alt text

现在,如果您按F11 可以单步进入COM类进行调试。

备注:您也可以直接打开包含 Foo 类的 .cs 文件并直接在其中放置断点。再次重要的是为程序集加载符号,或者当您放置断点时,Visual Studio 会告诉您不会命中该断点。

Here are the steps I performed and which allowed me to successfully debug a .NET assembly exposed as COM component:

Start by creating a class library containing a class that will be exposed as COM object:

namespace COMTest
{
    using System;
    using System.Runtime.InteropServices;

    public interface IFoo
    {
        void Bar();
    }

    [ComVisible(true)]
    public class Foo : IFoo
    {
        public void Bar()
        {
            Console.WriteLine("Bar");
        }
    }
}

Sign the assembly with a strong key and register as COM object:

regasm.exe /codebase COMTest.dll

Once the COM object is registered you may create a new console application in a new Visual Studio instance to test the COM object:

class Program
{
    static void Main()
    {
        var type = Type.GetTypeFromProgID("COMTest.Foo");
        var instance = Activator.CreateInstance(type);
        type.InvokeMember("Bar", BindingFlags.InvokeMethod, null, instance, new object[0]);
    }
}

Place a breakpoint on the InvokeMember line and run the application. Once the breakpoint is hit open the Modules Window (Ctrl+D M) and make sure that symbols are loaded for the COM assembly:

alt text

Now if if you press F11 you can step into the COM class to debug.

Remark: You can also directly open the .cs file containing the Foo class and directly place a breakpoint there. Once again the important thing is to have the symbols loaded for the assembly or when you place your breakpoint Visual Studio will tell you that this breakpoint won't be hit.

倾`听者〃 2024-09-24 17:34:53

VS2008 SP1 之后发布了一个修补程序,解决了许多调试问题。知识库文章在这里,修补程序下载此处

There was a post VS2008 SP1 hotfix released that solves a number of debugging problems. The KB article is here, the hotfix download is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文