简体   繁体   English

从Visual Studio 2008中的C#应用​​程序调用时如何调试C DLL

[英]How to debug C DLL when called from a C# application in Visual Studio 2008

I have a C# windowed application that needs to make use of a third-party API, which is offered only in C. To solve this problem, I've created three projects within VS2008: two C# projects and an empty C++ project. 我有一个C#窗口应用程序,该应用程序需要使用仅在C中提供的第三方API。为解决此问题,我在VS2008中创建了三个项目:两个C#项目和一个空的C ++项目。 One C# project is my "Wrapper" project, responsible only for producing managed code that calls the C DLL. 我的“包装器”项目是一个C#项目,仅负责产生调用C DLL的托管代码。 The other C# project is the windowed application, which makes use of the Wrapper project; 另一个C#项目是窗口应用程序,它使用Wrapper项目。 let's call this the GUI project. 我们称之为GUI项目。

Inside the C++ project, I've created several C files (*.c) that utilise the third-party API and export (dllexport) suitable functions. 在C ++项目中,我创建了几个C文件(* .c),这些文件利用了第三方API并导出(dllexport)合适的功能。 I can successfully compile this project into a DLL and have had no problems in calling these functions from my Wrapper project. 我可以成功地将该项目编译为DLL,并且从我的Wrapper项目中调用这些函数没有任何问题。 In case it's relevant, my Wrapper project uses DllImport attributes to reference these functions. 如果相关,我的Wrapper项目将使用DllImport属性引用这些函数。

My C++ project has a post-build event that copies the resulting DLL into the output directory of my GUI C# project so that it's picked up at execution time. 我的C ++项目具有一个生成后事件,该事件会将生成的DLL复制到我的GUI C#项目的输出目录中,以便在执行时将其提取。 This feels a bit grim, but it's the only way I've figured out how to do this. 这感觉有些严峻,但这是我弄清楚如何做到这一点的唯一方法。 My GUI project has a dependency on my Wrapper project, which has a dependency on the C++ project. 我的GUI项目依赖于我的Wrapper项目,而后者依赖于C ++项目。

What I'm struggling to do, however, is debug (ie step-through) my C project code. 但是,我要做的是调试(即逐步执行)我的C项目代码。 I've tried to set a breakpoint within the C code in the hope that it would be caught when my C# code executes the relevant function. 我试图在C代码中设置一个断点,希望当我的C#代码执行相关功能时会发现该断点。 Unfortunately, as soon as I run my C# application, the IDE warns me that the C breakpoints will never be executed: " No symbols have been loaded for this document ." 不幸的是,一旦我运行C#应用程序,IDE便警告我永远不会执行C断点:“ 尚未为该文档加载任何符号 。”

Any help with this would be greatly appreciated. 任何帮助,将不胜感激。 Here are some things I've played with, but to no avail: 这是我玩过的一些东西,但无济于事:

  • Ensuring the .pdb file has the same timestamp as the DLL file. 确保.pdb文件具有与DLL文件相同的时间戳。 This hint was followed after a random Google suggested the "No symbols" error could be caused by this. Google随机提示此错误可能导致“无符号”错误之后,才遵循此提示。

  • I've selected "Enabled unmanaged code debugging" in both my C# project properties. 我在两个C#项目属性中都选择了“启用非托管代码调试”。

  • I've tried setting a breakpoint in my C# call just prior to an invocation of one of the DLL methods and attempted to step into the DLL. 我曾尝试在调用DLL方法之一之前在C#调用中设置断点,然后尝试进入DLL。 This didn't work either, it simply stepped over the function. 这也不起作用,它只是跨过了功能。

You should check in the modules list (Normally found in the Debug menu in Visual Studio) to make sure that: 您应该检查模块列表(通常在Visual Studio的“调试”菜单中找到),以确保:

  • The module is loaded 模块已加载
  • It is being loaded from the right place 正在从正确的位置加载
  • The symbols have been correctly loaded 符号已正确加载

If the window lists the module, but indicates that symbols aren't loaded then you can force VS to load symbols by right clicking on the module and selecting "Load symbols". 如果窗口列出了模块,但是指示未加载符号,则可以通过右键单击模块并选择“加载符号”来强制VS加载符号。 If it can't find them automatically then it will prompt you for a path. 如果无法自动找到它们,则会提示您输入路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio 2008中创建.dll以在C#应用程序中使用? - How to create a .dll in Visual Studio 2008 for use in a C# App? 如何在Visual Studio 2008 Express C#中调试这种情况 - How to debug in Visual Studio 2008 express C# this scenario 如何调试vs vc6 / vba app调用的2008 c#express dll? - How to debug vs 2008 c# express dll which is called by a vb6/vba app? 在Visual Studio 2008中打开C#控制台应用程序时会自动创建Debug文件夹 - Debug folder is automatically created on opening C# console application in visual studio 2008 C#Visual Studio 2008,包括DLL到exe吗? - C# Visual Studio 2008, include DLL to exe? 如何使用 Visual Studio 在调用 C++ dll 的 C# 程序中进行调试? - How to use Visual Studio to debug across a C# program calling a C++ dll? 调试从 C++ 调用的 C# dll(嵌入 Z5D9B47BD3B65072E0D6ZDAF55F01DA08) - Debug a C# dll called from C++ (with Mono embedded) 如何在Visual Studio 2010中调试从另一个进程启动的C#.NET应用程序 - How to debug a C# .NET application in Visual Studio 2010 that is started from another process 如何从C#代码在Visual Studio中创建本机DLL? - How to create native DLL in Visual Studio from C# code? C#,Visual Studio 2008 - C#, Visual Studio 2008
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM