Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更正此错误:GetLastError 0x13d_C++_C_Winapi_Getlasterror_Formatmessage - Fatal编程技术网

C++ 更正此错误:GetLastError 0x13d

C++ 更正此错误:GetLastError 0x13d,c++,c,winapi,getlasterror,formatmessage,C++,C,Winapi,Getlasterror,Formatmessage,在此示例中,dwerror为10045L。但此代码返回0x13d值作为错误。 如何获取格式消息?请仔细查看 TCHAR lpMsgBuf[512]; if(!FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &am

在此示例中,dwerror为
10045L
。但此代码返回0x13d值作为错误。 如何获取格式消息?请仔细查看

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;
}

首先,当您说FORMAT\u MESSAGE\u ALLOCATE\u BUFFER时,您不需要分配超过一个指针。然后在lpBuffer中传递指向该指针的指针。所以试试这个:

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;
}
#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;
}
别忘了打电话给LocalFree

或者您自己分配缓冲区:

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;
}
此外,请尝试以下方法:

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;
}
#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;
}
#包括
#包括
int alloc(字符**pbuff,无符号int n)
{
*pbuff=(char*)malloc(n*sizeof(char));
}
int main()
{
字符缓冲区[512];
printf(“缓冲区地址:%p\n”之前,&buffer);
//GCC sais:“无法将字符(*)[512]转换为字符**…”
//alloc(和缓冲区,128);
//如果我试着投:
alloc((字符**)和缓冲区,128);
printf(“在%p\n之后的缓冲区地址,&buffer”);
//如果我做得对:
char*p_缓冲区;
alloc(&p_缓冲区,128);
printf(“在%p\n之后的缓冲区地址”,p\u buffer);
返回0;
}

试图更改变量的地址是没有意义的。这可能就是代码不起作用的原因。

<0x13d==317==
错误\u找不到MR\u MID\u
。 您试图查找的错误消息在系统中不存在。 可能您的错误源于特定的dll驱动程序。 如果您知道哪个dll\驱动程序尝试获取它的句柄,并从_HMODULE指定
格式化_消息_,而不是从_SYSTEM
指定
格式化_消息_,并在调用
格式化消息
时提供句柄作为源


除此之外,如果您使用
FORMAT\u MESSAGE\u ALLOCATE\u BUFFER
,您应该声明
LPTSTR
类型的变量,如
LPTSTR pMsg并将其作为
(LPTSTR)&pMsg
传递给FormatMessage,完成后使用
LocalFree(pMsg)
释放分配的内存。

例如,我建议您查看错误代码
0x13d
的含义