SetLastError问题 @@@@在线恭候!!!!!@@@@(200分)

  • 主题发起人 主题发起人 lb3141
  • 开始时间 开始时间
L

lb3141

Unregistered / Unconfirmed
GUEST, unregistred user!
如何通过SetLastError更改错误信息:<br>我定义了一个记载错误消息的结构,如下:<br><br>(*----------------------------------------------------<br>描述:消息引发异常的错误描述结构,<br>&nbsp; &nbsp; &nbsp; 用此结构来记载该错误所经过的每个对象的方法。<br>-----------------------------------------------------*)<br>type<br>&nbsp; PExcept = ^TExcept;<br>&nbsp; TExcept = packed record<br>&nbsp; &nbsp; Succeed: Boolean; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//是否成功<br>&nbsp; &nbsp; ParamsList: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//参数<br>&nbsp; &nbsp; ClassName: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //类名称<br>&nbsp; &nbsp; MethodName: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//方法名称<br>&nbsp; &nbsp; ExecuteTime: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //激发时间<br>&nbsp; &nbsp; ErrorTexts: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//错误日志<br>&nbsp; &nbsp; ErrorType: TErrorType; &nbsp; &nbsp; &nbsp; &nbsp; //错误等级<br>&nbsp; end;<br><br>(*----------------------------------------------------<br>描述:消息引发异常的错误描述结构,<br>&nbsp; &nbsp; &nbsp; 用此结构来查询错误结构<br>&nbsp; &nbsp; &nbsp; 参数DebugInfo相当于array of PExcept<br>-----------------------------------------------------*)<br>type<br>&nbsp; PMessageExcept = ^TMessageExcept;<br>&nbsp; TMessageExcept = packed record<br>&nbsp; &nbsp; Owner: TComponentName; &nbsp; &nbsp; &nbsp; &nbsp; //引发对象<br>&nbsp; &nbsp; MethodName: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//方法名称<br>&nbsp; &nbsp; MessageName: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //消息名称<br>&nbsp; &nbsp; DebugInfo: TList; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//调试的错误信息(详细)<br>&nbsp; end;<br>&nbsp; Arr_PMessageExcept = array of PMessageExcept;<br><br>我想在程序出错时将系统的错误消息在加上我自订义的错误消息解释合并为上述消息结构PMessageExcept ,<br>然后用SetLastError将PMessageExcept结构的地址存入,<br>最后通过FormatMessage将PMessageExcept结构的地址读出来,代码如下:<br>var<br>&nbsp; Buffer: PChar;<br>&nbsp; &nbsp; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or<br>&nbsp; &nbsp; &nbsp; &nbsp;FORMAT_MESSAGE_ALLOCATE_BUFFER, nil, GetLastError, 0, Buffer,<br>&nbsp; &nbsp; &nbsp; &nbsp;SizeOf(Buffer), nil);<br>但我试验了很多方法,读出的PMessageExcept结构总是nil,<br><br>那位大虾帮一下小第,分不够可在加!!!!!<br>在线恭候!!!!!<br>
 
只能来学习的。
 
各位千万不要谦虚,我救命的,在线恭候!!!!!
 
老兄呀,<br>看我下面的:<br>&nbsp; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ALLOCATE_BUFFER, nil, GetLastError, 0, @buffer, Sizeof(buffer)-1, nil); <br><br>还有什么问题吗??????<br>呵呵~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`<br>
 
这种方法我早用过,不行,我想读没错,关键是如何用SetLastError写入PMessageExcept结构的地址,以便能读出来
 
赋值给他不就OK,<br>分配 一个TMessageExcept内存,<br>以后记得Free就是咯,<br>你以前的那个什么FormatMessage语句是有问题的,<br>在要用得它时用我写的哦,<br>能否将整个源码给我看呀,<br>就你贴出那一点点真的难以说明,<br>妹妹:Netsofte_mail@163.com,<br>呵呵~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
 
//TO: Netsoft;<br>//整个源码太长,这是个简单例子,不过思路相同,不知道为何总是不行;<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>type<br>&nbsp;PExcept = ^TExcept;<br>&nbsp;TExcept = packed record<br>&nbsp; &nbsp;MethodName: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//方法名称<br>&nbsp; &nbsp;ErrorTexts: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//错误日志<br>&nbsp;end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; B: Boolean;<br>&nbsp; I: Integer;<br>&nbsp; P: PExcept;<br>&nbsp; Buffer: PChar;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; I :=StrToInt('');<br>&nbsp; except<br>&nbsp; &nbsp; On E: Exception do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; New(P);<br>&nbsp; &nbsp; &nbsp; P^.MethodName :='Button1Click';<br>&nbsp; &nbsp; &nbsp; P^.ErrorTexts :=E.Message;<br><br>&nbsp; &nbsp; &nbsp; //将地址写入<br>&nbsp; &nbsp; &nbsp; SetLastError(Integer(P));<br>&nbsp; &nbsp; &nbsp; B :=True;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>&nbsp; if B then<br>&nbsp; begin<br>&nbsp; &nbsp; //实际编码时以下代码是在别处写的<br>&nbsp; &nbsp; if FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FORMAT_MESSAGE_ALLOCATE_BUFFER,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetLastError, //将地址读出,不知道为何总是0<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@buffer,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sizeof(buffer)-1,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil) &gt; 0 then<br>&nbsp; &nbsp; if Buffer &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; ShowMessage(PExcept(Buffer)^.ErrorTexts);<br>&nbsp; &nbsp; Dispose(P);<br>&nbsp; end;<br>end;<br><br>end.<br>
 
&gt;&gt; SetLastError(Integer(P));<br>&nbsp; &nbsp;楼主,这样能行吗????SetLastError函数的意思你懂不的?????给他的值只能是 &nbsp; ERROR_XXXXXX &nbsp; 之类的错误常数呢,<br><br>你的意思是自己触发一个错误 StrToInt(''),然后捕捉到该错误,而后就是获得错误咯。这段程序我今晚帮你看过以后,明天中午告诉你我的分析结果吧,<br>如果你有什么看法贴出来,因为这不能实现你的目的呀。<br>呵呵~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><br>
 
P:=PExcept(GetLastError)
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
825
import
I
后退
顶部