GetLastError()数字_转换为_文字

1、具体参数 可参看 http://blog.csdn.net/hongweigg/article/details/6821536 或 其它文章 或 MSDN

2、VC6 测试代码:

#include <stdio.h>
#include <windows.h>

void main()
{
    LPTSTR lpMsgBuf;
    //DWORD nErrno = GetLastError();
    DWORD nErrno = 10060;
    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        nErrno,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR)&lpMsgBuf,
        0,
        NULL);

    MessageBox(NULL, (LPCTSTR)lpMsgBuf, TEXT("NamedPipe Error"), MB_OK | MB_ICONINFORMATION );
    LocalFree(lpMsgBuf);
}

3、Delphi7 测试代码:

//#define MAKELANGID(p, s)       ((((WORD  )(s)) << 10) | (WORD  )(p))
function MAKELANGID(_p, _s :word) :DWORD;
begin
  Result := (_s shl 10) or (_p);
end;

//#define LANG_NEUTRAL                     0x00
//#define SUBLANG_DEFAULT                  0x01    // user default
{$O-}
function ErrorNo2Str(_dwErrNo :DWORD):string;
const
  LANG_NEUTRAL = $0;
  SUBLANG_DEFAULT = $01;
var pcMsgBuf :PChar;
    buf :array[0..255] of Char;
begin
//function FormatMessage(
//  dwFlags: DWORD;
//  lpSource: Pointer;
//  dwMessageId: DWORD;
//  dwLanguageId: DWORD;
//  lpBuffer: PChar;
//  nSize: DWORD;
//  Arguments: Pointer): DWORD; stdcall;
//{$EXTERNALSYM FormatMessage}
{
  Windows.FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS,
    0,
    _dwErrNo,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    pcMsgBuf,
    0,
    nil);

    Result := pcMsgBuf;
    
    LocalFree(DWORD(pcMsgBuf));
}
// *** 使用上面的参数方式(OS帮我们申请字符串缓冲区空间),始终不对...连断点都下不了...于是,使用下面的方式... ***
  ZeroMemory(@buf[0], Length(buf));
  Windows.FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS,
    0,
    _dwErrNo,     
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    buf,
    Length(buf),
    nil);

  Result := buf;
end;
{$O+}

procedure TForm1.Button1Click(Sender: TObject);
var dwErrNo :DWORD;
    str :string;
begin
  dwErrNo := 10060;

  str := ErrorNo2Str(dwErrNo);
  Memo1.Lines.Add(str);
end;

4、

5、

 

posted @ 2016-11-29 13:48  CppSkill  阅读(1629)  评论(0编辑  收藏  举报