我如何知道我的代码在执行什么模块? [英] How can I tell what module my code is executing in?

查看:195
本文介绍了我如何知道我的代码在执行什么模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在很长一段时间,当我有一个错误处理程序,我使它报告错误抛出的项目,模块和过程。我总是通过简单地通过常量存储他们的名称。我知道在一个类中,你通过TypeName(Me)以编程方式获得名称,但显然只有当我不在标准模块时才能从三个信息中获得一个。

For a very long time, when I have an error handler I make it report what Project, Module, and Procedure the error was thrown in. I have always accomplished this by simply storing their name via constants. I know that in a Class you get the name programmatically with TypeName(Me), but obviously that only gets me one out of three pieces of information and only when I'm not in a "Standard" module.


我使用常量没有一个非常大的问题,只是人们不总是保持最新的,或者更糟的是他们复制和粘贴,然后你有错误的名称被报告等等。所以我想做的是找出一个方法来摆脱示例中显示的常量,而不会丢失信息。

I don't have a really huge problem with using constants, it's just that people don't always keep them up to date, or worse they copy and paste and then you have the wrong name being reported, etc. So what I would like to do is figure out a way to get rid of the Constants shown in the example, without losing the information.

Option Compare Binary
Option Explicit
Option Base 0
Option Private Module

Private Const m_strModuleName_c As String = "MyModule"

Private Sub Example()
    Const strProcedureName_c As String = "Example"
    On Error GoTo Err_Hnd
Exit_Proc:
    On Error Resume Next
    Exit Sub
Err_Hnd:
    ErrorHandler.FormattedErrorMessage strProcedureName_c, m_strModuleName_c, _
        Err.Description, Err.Source, Err.Number, Erl
    Resume Exit_Proc
End Sub

有没有人知道如何让代码知道它在哪里?

Does anyone know ways to for the code to tell where it is? If you can conclusively show it can't be done, that's an answer too:)

编辑: 如果您可以确定地显示 br>我也知道项目名称是在Err.Source。我希望能够得到它没有异常为其他目的。如果你知道伟大的,如果不是我们可以定义为超出了问题的范围。

我也知道如何得到错误行,但该信息当然只有一些帮助,没有知道模块.Procedure。


I am also aware that the project name is in Err.Source. I was hoping to be able to get it without an exception for other purposes. If you know great, if not we can define that as outside the scope of the question.
I am also aware of how to get the error line, but that information is of course only somewhat helpful without knowing Module.Procedure.

推荐答案

对于项目名称,我唯一能想到的方法是故意在某处抛出错误Sub Main(),并在错误处理代码中,将生成的Err.Source保存到全局变量g_sProjectName中。否则,我似乎记得有一个名为TLBINF32.DLL的自由第三方DLL做COM反射 - 但似乎超越顶部你想做的,在任何情况下,公共和私人之间可能有区别类。最后,您可以使用二进制编辑器在您的EXE中搜索项目名称字符串,然后尝试从位置读取字符串。虽然令人沮丧的是,每个项目和代码模块的名称嵌入在EXE中,似乎没有可预见的方式这样做,因此不推荐。

For the project name, the only way I can think of doing this is by deliberately throwing an error somewhere in Sub Main(), and in the error handling code, save the resulting Err.Source into an global variable g_sProjectName. Otherwise, I seem to remember that there was a free 3rd party DLL called TLBINF32.DLL which did COM reflection - but that seems way over the top for what you want to do, and in any case there is probably a difference between public and private classes. And finally, you could use a binary editor to search for the project name string in your EXE, and then try to read the string from the position. Whilst it is frustrating that the names of every project and code module is embedded in the EXE, there seems to be no predictable way of doing this, so it is NOT recommended.

这篇关于我如何知道我的代码在执行什么模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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