C++ 从单独的库调用swprint失败
我面临一个奇怪的问题。我根据构建定义使用sprintf或swprintf,使用unicode或不使用unicode。我将这些函数包装在自己的函数中,如下所示:C++ 从单独的库调用swprint失败,c++,c,unicode,variadic-functions,printf,C++,C,Unicode,Variadic Functions,Printf,我面临一个奇怪的问题。我根据构建定义使用sprintf或swprintf,使用unicode或不使用unicode。我将这些函数包装在自己的函数中,如下所示: int mysprintf( MCHAR* str,size_t size, const MCHAR* format, ... ) { #ifdef MYUNICODE return swprintf( str, size, format); #else return snprintf( str, format); #en
int mysprintf( MCHAR* str,size_t size, const MCHAR* format, ... )
{
#ifdef MYUNICODE
return swprintf( str, size, format);
#else
return snprintf( str, format);
#endif
}
这些函数位于一个字符串类中,该类是一个单独的项目,并编译为库。我在另一个程序中使用它。现在如果我使用mysprintf()
我在字符串缓冲区中得到一些垃圾值。但是如果我直接从程序中调用swprintf函数,它就可以正常工作。我在构建中定义了UNICODE,并调用了swprintf函数,但它会填充一些垃圾值。我不明白出了什么问题
谢谢
Amit您需要通过。。。从mysprintf函数到它包含的prrintf函数的参数。要做到这一点,您应该使用vprintf()函数系列-有关详细信息,请参阅。问题确实在于您有自己的函数,其中包含可变数量的参数。您需要获取一个指向参数列表的指针,并将其传递给被调用方。va_start允许您这样做,它需要参数列表中指向函数的最后一个指针
int mysprintf( MCHAR* str, size_t size, const MCHAR* format, ... )
{
va_list args;
va_start(args, format);
int result;
#ifdef MYUNICODE
result = vswprintf( str, size, format, args);
#else
result = ..
#endif
va_end(args);
return result;
}
干杯 swprintf()不接受va_列表,但vswprintf()接受。谢谢,伙计。我只是复制并粘贴了上面的代码,但应该仔细阅读。现在编辑:)
int mysprintf( MCHAR* str, size_t size, const MCHAR* format, ... )
{
va_list args;
va_start(args, format);
int result;
#ifdef MYUNICODE
result = vswprintf( str, size, format, args);
#else
result = ..
#endif
va_end(args);
return result;
}