宏不执行功能

时间:2014-02-12 23:24:39

标签: c++ windows macros

我有一个宏,我这样使用它:

int GL = 0;
GL = GetLastError();
DEBUG_MESSAGE( ERR, "RegOpenKeyEx failed. Error code = '%u'. Error description = '%s'", GL, GetErrorText( GL ) );

函数GetErrorText返回char *,它是属于错误代码的相应错误文本。

问题是,当我调用我的宏时,它不会调用GetErrorText函数。 输出将是:RegOpenKeyEx failed. Error code = '5'. Error description = ''

宏以这种方式定义:

#define DEBUG_MESSAGE( Type, debug_message, ... ) { _debugLog.message( Type, debug_message, ##__VA_ARGS__ ); }

这是宏调用的函数:

void log::message( int Type, const char * message, ... )
    {
        char MessageExpanded[ 2048 ] = { 0 };
        va_list args;
        int len;

        write_indentation();

        memset( Message, 0, sizeof( Message ) );
        memset( MessageExpanded, 0, sizeof( MessageExpanded ) );

        va_start( args, message );

        len = _vscprintf( message, args ) + 1; // _vscprintf doesn't count terminating '\0'

        vsprintf_s( Message, len, message, args );

        va_end( args );

        sprintf( MessageExpanded, "%s %s", Spaces, Message );
        LOG( MessageExpanded, context.c_str(), "", Type, CurrentFileName );

    }//log::message

有办法以某种方式解决这个问题吗?

提前致谢!

更新

char * GetErrorText( DWORD dwLastError )
{
DEBUG_METHOD( INFO );
DEBUG_MESSAGE( INFO, "Argument1 = '%d'", dwLastError );

HMODULE hModule = NULL; // default to system source
LPSTR MessageBuffer;
DWORD dwBufferLength;
char Error[ SMALL ] = { 0 };
char * ErrorMsg = NULL;

DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM;

// If dwLastError is in the network range, load the message source.
if ( dwLastError >= NERR_BASE && dwLastError <= MAX_NERR )
{
    hModule = LoadLibraryEx( TEXT( "netmsg.dll" ), NULL, LOAD_LIBRARY_AS_DATAFILE );
    if ( hModule != NULL ) dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
}

// Call FormatMessage() to allow for message text to be acquired from the system or from the supplied module handle.
if ( dwBufferLength = FormatMessageA(
                                      dwFormatFlags,
                                      hModule,                                     // module to get message from (NULL == system)
                                      dwLastError,
                                      MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), // default language
                                      ( LPSTR )&MessageBuffer,
                                      0,
                                      NULL
                                    )
    )
{
    memset( Error, 0, sizeof( Error ) );
    //printf( "\n%s", MessageBuffer );
    sprintf( Error, "%s", MessageBuffer );
    ErrorMsg = Error;

    // Free the buffer allocated by the system.
    LocalFree( MessageBuffer );
}

// If we loaded a message source, unload it.
if ( hModule != NULL ) FreeLibrary( hModule );

return ErrorMsg;

}//GetErrorText

1 个答案:

答案 0 :(得分:2)

GetErrorText()正在返回一个指向本地缓冲区的指针:

char Error[ SMALL ] = { 0 };

因此,在调用_debugLog.message()时,它返回的指针无效。