Código
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern int xxx();
Código
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function xxx() As Integer End Function
Basicamente necesito comprobar el valor de SetLastError en tiempo de ejecución, para determinar si es True o es False, pero parece un misterio sin resolver.
¿Alguien sabe de alguna referencia oficial de MSDN donde expliquen este detalle tan específico?, ¿cómo obtener los metadatos del método?.
Es para mejorar la lógica de este método genérico, ya que sin poder determinar ese detalle que me falta, no sirve para nada realizar un checkeo:
Código
''' <summary> ''' Invokes the encapsulated function, Tthen checks the exit code returned by the function that was called using platform invoke ''' that has the <see cref="DllImportAttribute.SetLastError"/> flag set. ''' </summary> ''' ---------------------------------------------------------------------------------------------------- ''' <exception cref="Win32Exception"> ''' Function 'X' thrown an unmanaged Win32 exception with error code 'X'. ''' </exception> ''' ---------------------------------------------------------------------------------------------------- Private Shared Sub SafePInvoke(Of T)(ByRef refResult As T, ByVal expr As expression(Of Func(Of T))) refResult = expr.Compile.Invoke() Dim exitCode As Integer = Marshal.GetLastWin32Error If exitCode <> 0 Then Throw New Win32Exception([error]:=exitCode, message:=String.Format("Function '{0}' thrown an unmanaged Win32 exception with error code '{1}'.", CType(expr.Body, MethodCallExpression).Method.Name, CStr(exitCode))) End If End Sub
Saludos!