Public Declare Function GetLastError Lib "kernel32" () As Long
Public 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 Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Public Function LastSystemError() As String ' ' piu' errori di sistema ' Dim sError As String * 500 Dim lErrNum As Long Dim lErrMsg As Long ' lErrNum = GetLastError lErrMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, _ ByVal 0&, lErrNum, 0, sError, Len(sError), 0) LastSystemError = Trim(sError) ' End Function Ora poni un command button su un form e chiama la funzione LastSystemError Private Sub Command1_Click() ' MsgBox LastSystemError ' End Sub 'Se non si registra nessun errore vedrai apparire un messaggio 'che dice " Operazione completata con successo." 'Qundo usi questa funzione, tieni in mente questi punti: '1. Molte chiamate API ripristinano il valore di GetLastError 'mentre succedono, cosi' la funzione deve essere chiamata 'immediatamente dopo la chiamata API che ha fallito. '2. L'ultimo valore d'errore e' mantenuto su un thread base, 'percio' la funzione deve essere chiamata dallo stesso thread 'della chiamata API fallita. La maggior parte delle chiamate all'API Win32 ritornano _ parecchie informazioni di errore quando esse falliscono. Per ottenere queste informazioni in un formato adatto, devi usare le API GetLastError e FormatMessage. |