僒儞僾儖廤  >  VC  >  GetLastError
GetLastError
2016/04/08

GetLastError偱僄儔乕僐乕僪傪庢摼偟FormatMessage偱僄儔乕儊僢僙乕僕傪庢摼偟昞帵偟傑偡丅

仧娐嫬
OS Windows 7 Professional Service Pack 1 (64bit)
VC Microsoft Visual C++ 2005 77972-235-2482122-41662

[僼傽僀儖]-[怴婯嶌惉]-[僾儘僕僃僋僩]傪慖戰偟傑偡丅

乽僾儘僕僃僋僩偺庬椶乿棑偱[Visual C++]-[Win32]傪慖戰偟丄[Win32 僐儞僜乕儖傾僾儕働乕僔儑儞]傪慖戰偟傑偡丅 乽僾儘僕僃僋僩柤乿偼乽GetLastErrorTest乿偵偟傑偡丅

GetLastErrorTest.cpp
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
// GetLastErrorTest.cpp : 僐儞僜乕儖 傾僾儕働乕僔儑儞偺
// 僄儞僩儕 億僀儞僩傪掕媊偟傑偡丅
//

#include "stdafx.h"
#include "windows.h"

void dspErrorInfo ( void );

int _tmain( int argc, _TCHAR* argv[] )
{
    FILE* fp;

    fp = fopen( "\\hoge\\hoge.txt""a" );
    dspErrorInfo();

    fp = fopen( "hoge.txt""a" );
    fp = fopen( "hoge.txt""a" );
    dspErrorInfo();

    getchar();

    return 0;
}

void dspErrorInfo()
{
    DWORD err = GetLastError();
    printf( "err=%d\n", err );

    LPTSTR msg = NULL;
    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
                 | FORMAT_MESSAGE_FROM_SYSTEM
                 , NULL
                 , err
                 , LANG_USER_DEFAULT
                 , ( LPTSTR )&msg
                 , 0
                 , NULL
                 );
    printf( "msg=%s\n", msg );

    // FormatMessage撪晹偱妋曐偟偨椞堟傪夝曻
    LocalFree( msg );
}

幚峴偟偰傒傑偡丅

err=3
msg=c歔U0丒_0󘅨L0媺d0K0丒~0[0丒0
err=183
msg=鈋k0X[(WY0丒󤕘0丒丒\ObY0丒S0h0o0g0M0~0[0丒0

僄儔乕儊僢僙乕僕偑暥帤壔偗偟偰偄傑偡丅

僜儕儏乕僔儑儞 僄僋僗僾儘乕儔偵昞帵偝傟偰偄傞僾儘僕僃僋僩柤傪塃僋儕僢僋偟僾儘僷僥傿傪奐偒傑偡丅

[峔惉僾儘僷僥傿]-[慡斒]偺暥帤僙僢僩傪乽儅儖僠 僶僀僩暥帤僙僢僩傪巊梡偡傞乿傪慖戰偟傑偡丅

err=3
msg=巜掕偝傟偨僷僗偑尒偮偐傝傑偣傫丅

err=183
msg=婛偵懚嵼偡傞僼傽僀儖傪嶌惉偡傞偙偲偼偱偒傑偣傫丅

惓偟偔昞帵偝傟傑偟偨丅

fopen( "hoge.txt", "a" );偱偡偑丄幚嵺偼僼傽僀儖傪僆乕僾儞偱偒傑偡丅 偟偐偟丄僄儔乕僐乕僪偵偼183乮婛偵懚嵼偡傞僼傽僀儖傪嶌惉偡傞偙偲偼偱偒傑偣傫丅乯偑愝掕偝傟偰偟傑偄傑偡丅 偦偺偨傔丄僄儔乕偑敪惗偟偰偄傞売強偱僷儔儊乕僞偺撪梕側偳傪僼傽僀儖偵弌椡偟傛偆偲偟偨応崌丄 "a"偱Open偡傞偲僄儔乕僐乕僪偑忋彂偒偝傟偰偟傑偆偺偱拲堄偑昁梫偱偡丅

尵岅

庢摼偡傞儊僢僙乕僕偺尵岅傪LANG_ENGLISH偵曄峏偟偨偲偙傠丄僨僼僅儖僩偺暥帤僙僢僩乮Unicode 暥帤僙僢僩乯偱儊僢僙乕僕傪昞帵偱偒傑偟偨丅

GetLastErrorTest.cpp
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
// GetLastErrorTest.cpp : 僐儞僜乕儖 傾僾儕働乕僔儑儞偺
// 僄儞僩儕 億僀儞僩傪掕媊偟傑偡丅
//

#include "stdafx.h"
#include "windows.h"

void dspErrorInfo ( void );

int _tmain( int argc, _TCHAR* argv[] )
{
    FILE* fp;

    fp = fopen( "\\hoge\\hoge.txt""a" );
    dspErrorInfo();

    fp = fopen( "hoge.txt""a" );
    fp = fopen( "hoge.txt""a" );
    dspErrorInfo();

    getchar();

    return 0;
}

void dspErrorInfo()
{
    DWORD err = GetLastError();
    printf( "err=%d\n", err );

    LPTSTR msg = NULL;
    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER
                 | FORMAT_MESSAGE_FROM_SYSTEM
                 , NULL
                 , err
                 , MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US)
                 , ( LPTSTR )&msg
                 , 0
                 , NULL
                 );
    printf( "msg=%S\n", msg );

    // FormatMessage撪晹偱妋曐偟偨椞堟傪夝曻
    LocalFree( msg );
}

err=3
msg=The system cannot find the path specified.

err=183
msg=Cannot create a file when that file already exists.

仯 PageTop  仭 Home


Copyright (C) 2016 ymlib.com