本文介绍了GetLastWin32Error问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetLastWin32Error从

Kernel32.DLL公开Win32 GetLastError API方法。存在此方法是因为直接调用GetLastError来获取此信息是不安全的。如果你想要

来访问这个错误代码,你必须调用GetLastWin32Error而不是

为GetLastError编写你自己的平台调用定义并调用它。

公共语言运行库可以对API进行内部调用,覆盖操作系统维护的GetLastError




只有使用此方法才能获取错误代码您将

System.Runtime.InteropServices.DllImportAttribute应用于方法签名

并将SetLastError字段设置为true。根据所使用的源语言,此过程会有所不同:C#和C ++默认为false,

但Visual Basic中的Declare语句为true。有关GetLastError和SetLastError Win32 API方法的其他

信息,请参阅

MSDN Library。


最后一段是什么是什么意思?


特别是...只...如果你申请......部分


他们正在采取什么声明?


我只想在出现错误时才想留言。


如何测试成功/失败?


非常感谢


解决方案



以下是最后一段所说的 的例子:


< DllImport(" ole32.dll",EntryPoint:=" CLSIDFromString",_

SetLastError:= True,CharSet:= CharSet.Auto,_

CallingConvention:= CallingConvention.StdCall)_

公共共享函数CLSIDFromString(_

< MarshalAs(UnmanagedType.LPWStr)ByVal pwcsName As String,_

ByRef pclsid As Guid)作为整数

结束函数


"活性" < ac ********** @ a-znet.comwrote in message

news:O2 ************* @ TK2MSFTNGP05.phx。 gbl ...





以下是最后一段所说的 的例子:


< DllImport(" ole32.dll",EntryPoint:=" CLSIDFromString",_

SetLastError:= True,CharSet:= CharSet.Auto,_

CallingConvention:= CallingConvention.StdCall)_

公共共享函数CLSIDFromString(_

< MarshalAs(UnmanagedType.LPWStr)ByVal pwcsName As String,_

ByRef pclsid As Guid)作为整数

结束函数


"活性" < ac ********** @ a-znet.comwrote in message

news:O2 ************* @ TK2MSFTNGP05.phx。 gbl ...






声明Sub bla Lib" kernel32.dll" ()

该段落说如果使用DllImportAttribute声明调用的

API函数,则调用GetLastWin32Error只有makse意义,而

指定SetLastError = True 。这取决于语言如何完成。在VB中,您有两种方式来声明外部函数:


- 使用DllImport属性

- 使用Declare语句(如上所示)


它说,使用Declare语句,SetLastError字段默认设置为

True,所以你不必(或者可以''' t)明确地将其设置为true。



如果你使用Declare语句来声明被调用的函数,那么之后只需要调用GetLastWin32Error。没有其他事情要做。


如果您使用DllImportAtribte声明该功能,您必须在声明中设置

SetLastError = True:


< DllImportAttribute(" kernel32.dll",setlasterror:= True)_

共享子bla()

结束子

Armin


GetLastWin32Error exposes the Win32 GetLastError API method from
Kernel32.DLL. This method exists because it is not safe to make a direct
platform invoke call to GetLastError to obtain this information. If you want
to access this error code, you must call GetLastWin32Error rather than
writing your own platform invoke definition for GetLastError and calling it.
The common language runtime can make internal calls to APIs that overwrite
the operating system maintained GetLastError.

You can only use this method to obtain error codes if you apply the
System.Runtime.InteropServices.DllImportAttribute to the method signature
and set the SetLastError field to true. The process for this varies
depending upon the source language used: C# and C++ are false by default,
but the Declare statement in Visual Basic is true. For additional
information about the GetLastError and SetLastError Win32 API methods, see
the MSDN Library.

What does the last paragraph mean?

Especially the "...only...if you apply the ..." part

What Declare are they taking about?

I''d like to messagebox only if there is an error.

How can I test for success/failure?

Thanks a lot


解决方案

Below is an example of what the last paragraph is talking about:

<DllImport("ole32.dll", EntryPoint:="CLSIDFromString", _
SetLastError:=True, CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)_
Public Shared Function CLSIDFromString( _
<MarshalAs(UnmanagedType.LPWStr)ByVal pwcsName As String, _
ByRef pclsid As Guid) As Integer
End Function

" active" <ac**********@a-znet.comwrote in message
news:O2*************@TK2MSFTNGP05.phx.gbl...




Below is an example of what the last paragraph is talking about:

<DllImport("ole32.dll", EntryPoint:="CLSIDFromString", _
SetLastError:=True, CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)_
Public Shared Function CLSIDFromString( _
<MarshalAs(UnmanagedType.LPWStr)ByVal pwcsName As String, _
ByRef pclsid As Guid) As Integer
End Function

" active" <ac**********@a-znet.comwrote in message
news:O2*************@TK2MSFTNGP05.phx.gbl...




Declare Sub bla Lib "kernel32.dll" ()
The paragraph says that calling GetLastWin32Error only makse sense if the
API function called is declared using the DllImportAttribute while
specifying SetLastError=True. It depends on the language how this can be
done. In VB, you have two ways to declare an external function:

- Using the DllImport attribute
- Using the Declare statement (as shown above)

It says that, using the Declare statment, the SetLastError field is set to
True by default, so you don''t have to (or can''t) explicitly set it to true.

If you used the Declare statement for declaring the function called, simply
call GetLastWin32Error afterwards. Nothing else has to be done.

If you used the DllImportAtribte for declaring the function, you must set
SetLastError = True in the declaration:

<DllImportAttribute("kernel32.dll", setlasterror:=True)_
Shared Sub bla()
End Sub
Armin


这篇关于GetLastWin32Error问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:45