永发信息网

如何使用GetLastError()?

答案:2  悬赏:80  手机版
解决时间 2021-04-23 11:03
在什么情况下使用GetLastError()?
最好能举个能编译的例子,说明在说明情况下使用?
别把MSDN的往上传啊

不好意思啊,我想问的是API中的
最佳答案
GetLastError返回的值通过在api函数中调用SetLastError或SetLastErrorEx设置。函数并无必要设置上一次错误信息,所以即使一次GetLastError调用返回的是零值,也不能担保函数已成功执行。只有在函数调用返回一个错误结果时,这个函数指出的错误结果才是有效的。通常,只有在函数返回一个错误结果,而且已知函数会设置GetLastError变量的前提下,才应访问GetLastError;这时能保证获得有效的结果。SetLastError函数主要在对api函数进行模拟的dll函数中使用,所以对vb应用程序来说是没有意义的

Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Const LANG_NEUTRAL = &H0
Const SUBLANG_DEFAULT = &H1
Const ERROR_BAD_USERNAME = 2202&
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Private Sub Form_Load()
Dim Buffer As String
Buffer = Space(200)
SetLastError ERROR_BAD_USERNAME
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
MsgBox Buffer
End Sub
全部回答
答案是getlasterror(error 2)==2   过程如下:   #include <   windows   .h> #include #include using namespace std; int main() { handle filehandle; dword bytewritten; if((filehandle=createfile("\\\\.\\c:\11.txt", generic_write|generic_read, file_share_read|file_share_write,null, create_always,file_attribute_normal,null)) ==invalid_handle_value); { printf("createfile failed**** with error %d\n",getlasterror()); return 1; } if(writefile(filehandle,"this is a tset",14,&bytewritten,null)==0) { printf("write failed with error %d\n",getlasterror()); return 2; } if(closehandle(filehandle)==0) { printf("closehandle failed with error %d\n",getlasterror()); cout<
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯