高级调试技巧 [英] Advanced Debugging Tips

查看:98
本文介绍了高级调试技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近通过托管代码对advapi32.dll的本机函数进行Pinvoke调用时遇到了一些问题,但我还没有找到一种使之正常工作的方法,面临着与经典"A调用...使堆栈不平衡"和试图读取/写入受保护的内存".

我的问题是关于如何正确调试这些情况,因为我可能没有调试经验,因此遇到无法获得足够信息以了解错误在哪里的情况.

有什么方法可以调试/进入我不知道的VS2010调用,或者有一种方法可以读取正在分配的内存并尝试自己发现它?

举个例子,我有一个PTR到一个内存地址,该内存地址应该保存一个结构,这个结构很大,并且由很多其他子结构组成,我相信问题可能与那些结构的定义有关.没有使用正确的数据类型.
因此,例如,我如何从PTR读取特定的内存块,并查看在那里分配了哪些数据,以查看它是否适合正确字段中的结构?

很抱歉,如果这是一个令人困惑的问题,但我更想在此寻找一些指导原则和提示...希望得到一些评论.

I have been facing some issues lately with Pinvoke calls to native functions of advapi32.dll from managed code and i haven''t found yet a way to make it work properly, facing multiple problems related to the classic "A call to ... has unbalanced the stack" and "attempt to read/write protected memory".

My question here is regarding on how to debug those situations properly, as i may not have experience debugging this i ran into the situation where i cannot get information enough to know where the error is.

Is there any way to debug/step into those calls from VS2010 that im not aware of, or maybe a way to read the memory that is being allocated and try to discover it by myself?

To give an example, i have a PTR to a memory address which is supposed to hold a structure, which is pretty big and consist of a lot of another sub-structures and i believe the problem is related to the definition of those as i might not be using the correct data types.
So, for instance, how i can read a specific memory block from a PTR and see which data is allocated there to see if it fits into the structure in the correct fields?

I''m sorry if this is a confusion question, but I''m more like looking for some guidelines and tips into this... hope to get some comments.

推荐答案

调用...已使堆栈不平衡"的最大罪魁祸首是您将类型声明为Long而不是Integer.如果您使用的声明是在Internet上找到的,并且属于VB6代码,则这是很常见的.

VB6中的Long是32位有符号整数. VB.NET中的Long是一个64位有符号整数.由于字段大小与API预期的32位字段没有不同,因此您只是使堆栈不平衡.

例如,您试图调用期望带符号的32位整数的API函数,因此(在VB6中)将该参数声明为Long.您的VB.NET代码使用相同的Declare,只是向堆栈中写入了一个64位带符号整数(8个字节).现在,期望32位数字的API函数从堆栈中弹出4个字节.当调用返回时,VB.NET希望堆栈指针指向某个位置,并且现在指向4个字节.哦!
The biggest culprit of "A call to ... has unbalanced the stack" is you declared a type as Long instead of Integer. This is very common if you used a Declare you found on the internet and it belongs to VB6 code.

A Long in VB6 is a 32-bit signed integer. A Long in VB.NET is a 64-bit signed integer. Since the field sizes are not different than the 32-bit field the API expected, you just unbalanced the stack.

For example, you''re trying to call an API function that expects a signed 32-bit integer, so (in VB6) you Declare that parameter a Long. Your VB.NET code, using the same Declare, just wrote a 64-bit signed integer (8 bytes) to the stack. Now, the API function, expecting a 32-bit number, pops 4 bytes off the stack. When the call returns, VB.NET expects the stack pointer to point to a certain location, and its now pointing 4 bytes away. Uh Oh!


这篇关于高级调试技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆