C++ DLL 获取 MSI Property

99封情书 提交于 2020-04-08 06:41:46

在MSI工程中,经常会遇到这样的情况:

MSI 工程需要调用DLL(C++)中的一个函数实现某些特殊或者复杂的功能,通常的做法是在Custom Action 中调用该DLL 。

那么在安装过程中,该Custom Action 被调用的时候,如何使用Visual Studio 逐行Debug DLL 的源代码呢?

答案是:使用VS2010 中的Attach to Process 就可以实现。

 

步骤如下:

1> VS 工程设置为Debug 模式,并设置断点。

在断点前,弹出一个 dialog ,用来标识debug的 开始位置。以 C++ DLL 获取 MSI Property 中的工程为例。


2> 以 Custom Action : dynamic link library 中的 MSI 工程为例。 编译MSI 工程,运行安装包。

 

3> 弹出设定的 dialog 时,在VS 工程中选择,Debug / Attach to Process…

Msiexec.exe 是windows installer 服务,选择运行MessageBox 的windows installer服务,点击 ”Attach” 。

 

在VS 工程中,将鼠标移到断点处,可以看到工程和 msiexec.exe 已经关联了起来。

  

 

4> 继续安装,在弹出的dialog 中 点击“OK”。

再看VS 工程,程序就停在了断点处,之后就可以逐行debug代码了。

 

5> 按下 F10,进入下一行代码,弹出如下对话框,会发现是乱码,原因待续。

补充一下:productName 参数接收的是 MSI 中 Property 表中的 ProductName 属性值。

详情请参见:Custom Action : dynamic link library   

 

---------------------------------------------------------------------------------------

Debug 过程中,可能会发现 Debug 断点没有被执行到,红色断点变成了空心的黄色圆圈,将鼠标停放到断点上,相关的提示信息就会显示出来。

可能会遇到的问题:

1> The breakpoint will not currently be hit. No symbols have been loaded for this document.

 

解决方案:

1)工程右键 Property -> Configuration Properties -> Linker / Debugging / Generate Debug Info 

设置为 : Yes (/DEBUG)

 

2)如果还不好用的话,试试下面的方法:

Tools -> Options... ->Debugging / Symbols 

a>手动添加 .pdb 文件的路径

b>勾选 “Microsoft Symbol Servers”

 

2>

The breakpoint will not currently be hit. The source code is different from the original version.

To allow the breakpoint to be hit when the source code is different, right-click on the breakpoint, choose 'Location...', and turn on 'Allow the source code to be different from the original version.'

To allow this for all breakpoint, disable the option 'Require source files to exactly match the original version' under Tools, Options, Debugging, General.

这种情况原因很明显,就是你修改了源代码,但是没有重新编译,MSI 工程中引用的 DLL 的源代码和工程里的不一致,就出现了断点没有被执行到的情况。

按照提示的信息照做就可以了。

 

相关链接参考:

Debug a custom action dll

How to: Attach to a Running Process

Attach to Process in Visual Studio

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!