gpt4 book ai didi

c++ - 宏不执行功能

转载 作者:行者123 更新时间:2023-11-28 02:57:04 25 4
gpt4 key购买 nike

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

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

函数GetErrorText返回一个char *,即属于错误码对应的errortext。

问题是,当我调用我的宏时,它不会调用 GetErrorText 函数。输出将是这样的:RegOpenKeyEx 失败。错误代码 = '5'。错误描述 = ''

宏是这样定义的:

#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

最佳答案

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

char Error[ SMALL ] = { 0 };

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

关于c++ - 宏不执行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21742115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com