Rettifica questo errore: getLasterror 0x13D
-
13-11-2019 - |
Domanda
In questo campione, il dwerror lo è 10045L
.ma questo codice restituisce il valore 0x13d come errore. Come ricevere il messaggio di formato? Per favore dai un'occhiata.
TCHAR lpMsgBuf[512];
if(!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL ))
{
wprintf(L"Format message failed with 0x%x\n", GetLastError());
return;
}
Soluzione
0x13d == 317 == ERROR_MR_MID_NOT_FOUND
. Il messaggio per l'errore che stai cercando di trovare non esiste nel sistema. Forse il tuo errore è originato da uno specifico dll o autista. Se sai quale dll driver prova a seguirlo e specificare FORMAT_MESSAGE_FROM_HMODULE
invece di FORMAT_MESSAGE_FROM_SYSTEM
e fornire la maniglia come fonte nella chiamata a FormatMessage
.
E oltre a quello se usi FORMAT_MESSAGE_ALLOCATE_BUFFER
Dovresti dichiarare una variabile di tipo LPTSTR
piace LPTSTR pMsg;
e passalo a formatMessage come (LPTSTR)&pMsg
E quando hai finito con esso usa LocalFree(pMsg)
per rilasciare la memoria allocata.
Altri suggerimenti
Prima di tutto, quando dici format_message_allocate_buffer, non è necessario allocare più di un puntatore. Quindi passi un puntatore a quel puntatore in LPBuffer. Quindi prova questo:
TCHAR* lpMsgBuf;
if(!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL ))
{
wprintf(L"Format message failed with 0x%x\n", GetLastError());
return;
}
E non dimenticare di chiamare locale
Oppure allochi tu stesso il buffer:
TCHAR lpMsgBuf[512];
if(!FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) lpMsgBuf,
512, NULL ))
{
wprintf(L"Format message failed with 0x%x\n", GetLastError());
return;
}
Inoltre, prova questo:
#include <cstdio>
#include <cstdlib>
int alloc(char** pbuff,unsigned int n)
{
*pbuff=(char*)malloc(n*sizeof(char));
}
int main()
{
char buffer[512];
printf("Address of buffer before: %p\n",&buffer);
// GCC sais: "cannot convert char (*)[512] to char** ... "
// alloc(&buffer,128);
// if i try to cast:
alloc((char**)&buffer,128);
printf("Address of buffer after: %p\n",&buffer);
// if i do it the right way:
char* p_buffer;
alloc(&p_buffer,128);
printf("Address of buffer after: %p\n",p_buffer);
return 0;
}
Non ha senso provare a cambiare l'indirizzo di una variabile. Questo è probabilmente il motivo per cui il tuo codice non funziona.