简体   繁体   English

调试调用.Net(C#)dll的VB6项目

[英]Debugging VB6 project that calls a .Net(C#) dll

I have been stump in this problem for a few hours now. 我已经把这个问题留了几个小时了。 I hope someone has had a similar problem to this. 我希望有人遇到类似的问题。

We have developed a prototype .Net(C#) dll using VS2010, and would like to be able to call this dll in a both C# applications and VB6 application. 我们使用VS2010开发了一个原型.Net(C#)dll,并且希望能够在C#应用程序和VB6应用程序中调用这个dll。

My question is: 我的问题是:

Is it possible to debug a VB6 application that is calling a .Net dll? 是否可以调试调用.Net dll的VB6应用程序? I get an error message " Automation Error The system cannot find the file specified " 我收到错误消息“ 自动化错误系统找不到指定的文件

The error message suggests that there is something missing for my VB6 app to find the .Net dll. 错误消息表明我的VB6应用程序找不到.Net dll。

I am aware that if the VB6 application has been compiled, and the .exe has been created, the VB6 will successfully call the .Net dll functionality when using the .exe 我知道如果VB6应用程序已经编译,并且已经创建了.exe,VB6将在使用.exe时成功调用.Net dll功能

However it is important that we can debug through our VB6 application. 但是,重要的是我们可以通过VB6应用程序进行调试。 Unfortunately debugging does not allow you to step over the line of code instantiating the .Net DLL's class object. 不幸的是,调试不允许您跨越实例化.Net DLL的类对象的代码行。 I can't seem to do this. 我似乎无法做到这一点。

NOTE: I have looked around forums and MSDN documentation and I mostly find solution for calling a VB6 dll in .NET; 注意:我查看了论坛和MSDN文档,我主要找到在.NET中调用VB6 DLL的解决方案; which is unfortunately the opposite of what we need to do. 不幸的是,这与我们需要做的事情相反。

NOTE: I have already registered the compiled .Net(C#) assembly, and referenced it in the VB6 project. 注意:我已经注册了已编译的.Net(C#)程序集,并在VB6项目中引用了它。

I have however found these two pages, which seemed to be what we need, but its a solution for calling a .NET(c#) dll generated using VS2005. 然而,我发现这两个页面似乎是我们需要的,但它是一个用于调用使用VS2005生成的.NET(c#)dll的解决方案。 This doesnt seem to work when the .NET(C#) dll was generated using VS2010. 当使用VS2010生成.NET(C#)dll时,这似乎不起作用。

site1 site2 site1 site2

If someone could give any suggestions or direct me somewhere I can get one, that would be great. 如果有人可以提出任何建议或指导我某个地方,我可以得到一个,这将是伟大的。

Thanks 谢谢

SOLUTION Thanks to @HansPassant, I have found the solution. 解决方案感谢@HansPassant,我找到了解决方案。 To debug a VB6 project that contains a C# .NET assembly, you need to register the .NET dll through both " regasm " and " gacutil ", then make sure to close and reopen the VB6 application before you start debugging. 要调试包含C#.NET程序集的VB6项目,需要通过“ regasm ”和“ gacutil ”注册.NET dll,然后确保在开始调试之前关闭并重新打开VB6应用程序。

This is not a problem, VB6 uses its own debugger that doesn't get in the way of any other debugger, including the managed one for C# code. 这不是问题,VB6使用自己的调试器,不会妨碍任何其他调试器,包括C#代码的托管调试器。

You start from your C# class library project, ensure it is selected as the start project. 从C#类库项目开始,确保将其选为启动项目。 Project + Properties, Debug tab. Project + Properties,Debug选项卡。 Select the "Start external program" option and enter the path to the VB6 IDE. 选择“启动外部程序”选项,然后输入VB6 IDE的路径。 Typically c:\\program files\\microsoft visual studio\\vb98\\vb6.exe. 通常是c:\\ program files \\ microsoft visual studio \\ vb98 \\ vb6.exe。 Set a breakpoint on the method you want to debug. 在要调试的方法上设置断点。

Press F5 and the VB6 ide will start running. 按F5键,VB6 ide将开始运行。 Load or create your vb6 project. 加载或创建您的vb6项目。 Note how you can add the path to the .vbp project in the previous step so it will automatically load your project. 请注意如何在上一步中添加.vbp项目的路径,以便它自动加载项目。

Start debugging your vb6 project as usual. 像往常一样开始调试vb6项目。 As soon as it starts using your [ComVisible] C# class then your C# assembly gets loaded. 一旦它开始使用你的[ComVisible] C#类,你的C#程序集就会被加载。 And the breakpoint will hit when your vb6 code calls the method. 当您的vb6代码调用该方法时,断点将会出现。 Switch back and forth as needed. 根据需要来回切换。 Note that you cannot single-step from vb6 to C# code, you have to set breakpoints to get the debugger to stop. 请注意,您不能单步从vb6到C#代码,您必须设置断点以使调试器停止。

Ah, the wonders of using .NET from VB6 in a debuggable manner. 啊,以可调试的方式从VB6使用.NET的奇迹。

  • in the VB6 project compile options (reached using the Options button on the Make Project dialog window), choose the Compile to Native Code , No Optimization , and Create Symbolic Debug Info options. 在VB6项目编译选项中(使用Make Project对话框窗口中的Options按钮到达),选择Compile to Native CodeNo OptimizationCreate Symbolic Debug Info选项。 Then compile your VB6 project. 然后编译你的VB6项目。 These options allow proper VB6 binary-to-source mapping. 这些选项允许适当的VB6二进制到源映射。
  • Go to the Configuration Properties...Debugging property page of your solution and change the Start Action to launch your VB6 executable. 转到解决方案的Configuration Properties ... Debugging属性页,然后更改Start Action以启动VB6可执行文件。
  • In VS Solution Explorer, go to File...Add Existing Item and navigate to the folder containing the VB6 source code you want to debug. 在VS Solution Explorer中,转到File ... Add Existing Item并导航到包含要调试的VB6源代码的文件夹。 Double-click on the VB6 source code file you want to debug, and a source window should open in VS that allows you to set breakpoints in the VB6 source code. 双击要调试的VB6源代码文件,在VS中打开一个源窗口,允许您在VB6源代码中设置断点。
  • Make sure that your .NET library has a public default constructor. 确保您的.NET库具有公共默认构造函数。 This is essential. 这很重要。
  • Now also set any C# breakpoints that you need. 现在还设置您需要的任何C#断点。 Do not step into the .NET code - this doesn't work. 不要踏入.NET代码-这是行不通的。
  • When you start debugging with VS, your VB6 and C# breakpoints should be hit normally. 当您使用VS开始调试时,应正常命中VB6和C#断点。

One approach is to debug each individually: 一种方法是单独调试每个:

  • Debugging the VB6 code can be done in the IDE after compiling the C# DLL and adding it as a reference to the VB6 project. 在编译C#DLL并将其添加为VB6项目的引用之后,可以在IDE中完成VB6代码的调试。

  • Debugging the DLL with the VB6 host is possible in Visual Studio by compiling the VB6 project and using it in the project properties as the executable to run. 通过编译VB6项目并在项目属性中将其用作要运行的可执行文件,可以在Visual Studio中使用VB6主机调试DLL。

In some cases this is simpler/quicker than setting up the environment to debug both simultaneously. 在某些情况下,这比设置环境以同时调试两者更简单/更快。

This approach will require having at least the framework of each working beforehand. 这种方法需要预先至少具有每个工作的框架。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM