DllImportAttribute.SetLastError Campo

Definizione

Indica se il chiamato imposta un errore (SetLastError su Windows o errno in altre piattaforme) prima di tornare dal metodo con attributi.

public: bool SetLastError;
public bool SetLastError;
val mutable SetLastError : bool
Public SetLastError As Boolean 

Valore del campo

Esempio

In alcuni casi, Visual Basic sviluppatori usano il DllImportAttribute, anziché usare l'istruzione Declare, per definire una funzione DLL nel codice gestito. L'impostazione del SetLastError campo è uno di questi casi.

[DllImport("user32.dll", SetLastError = true)]
int MessageBoxA(IntPtr hWnd, String^ Text,
    String^ Caption, unsigned int Type);
internal static class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern int MessageBoxA(
        IntPtr hWnd, string lpText, string lpCaption, uint uType);
}
Friend Class NativeMethods
    <DllImport("user32.dll", SetLastError:=True)>
    Friend Shared Function MessageBoxA(hWnd As IntPtr, lpText As String,
        lpCaption As String, uType As UInteger) As Integer
    End Function
End Class

Commenti

true per indicare che il chiamato imposta un errore tramite SetLastError in Windows o errno in altre piattaforme; in caso contrario, false. Il valore predefinito è false.

Se questo campo è impostato su true, il gestore di marshalling di runtime chiama GetLastError o errno memorizza nella cache il valore restituito per evitare che venga sovrascritto da altre chiamate API. È possibile recuperare il codice di errore chiamando GetLastPInvokeError in .NET 6.0 e versioni successive o GetLastWin32Error in .NET 5 e versioni successive o .NET Framework.

In .NET, le informazioni sull'errore vengono cancellate (impostate su 0) prima di richiamare il chiamato quando questo campo è impostato su true. In .NET Framework le informazioni sull'errore non vengono cancellate. Ciò significa che le informazioni sull'errore restituite da GetLastPInvokeError e GetLastWin32Error in .NET rappresentano solo le informazioni sull'errore dell'ultimo p/invoke con DllImportAttribute.SetLastError impostato su true. In .NET Framework le informazioni sull'errore possono essere mantenute da una p/invoke alla successiva.

Si applica a

Vedi anche