Usuário com melhor resposta
How to write a platform independent wrapper function in C++

Pergunta
-
Hello all,
I am using snprintf to send the output to the buffer.
As of now I am doing it only for windows. But from now onwards it has to support for different platforms (Windows, Linux and Mac)
To support for multiple platforms, I am planning to write a wrapper function with #if tags.
but here the challenge I am facing is, when invoking WrapperSprintf from different places of the project the number of parameters are different.
How to write a common wrapper that can be used from different places with different no of parameters passed to WrapperSprintf function?
I tried the wrapper function like as shown below. Please help me how to proceed with this:
void WrapperSprintf( char buffer, const char *format, ... ) { #if defined(_WIN32) _snprintf_s(buffer, sizeof(buffer), "%s\n", format,...); #else snprintf(buffer, sizeof(buffer), format, ...); #endif }
Calling WrapperSprintf function1:
char m_systemTime[20];
char* CUPSManager ::getSystemTime() { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); WrapperSprintf(m_systemTime,"%d-%d-%d :%d:%d:%d" , timeinfo ->tm_year +1900, timeinfo ->tm_mon +1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); return m_systemTime; }
Calling WrapperSprintf function2:
void getDevicePath() { wstring strDevPath; strDevPath = (LPCWSTR)cDevicePath; char cDevPath[2048]; WrapperSprintf(cDevPath,"%ls", strDevPath.c_str()); int nPathLength = strlen(cDevPath); ... }
- Editado John paul coder domingo, 31 de maio de 2020 14:52
domingo, 31 de maio de 2020 09:30
Respostas
-
You could use _vsnprintf_s and vsnprintf - functions that take va_list parameter.
Note however that sizeof(buffer) returns the size of the pointer (4 or 8 bytes), not the size of the block of memory that pointer points to.
Igor Tandetnik
- Marcado como Resposta John paul coder segunda-feira, 1 de junho de 2020 16:19
domingo, 31 de maio de 2020 17:23 -
Hi,
Thank you for posting here.
>>but here the challenge I am facing is, when invoking WrapperSprintf from different places of the project the number of parameters are different.
As far as I'm concerned, you could try to use Variadic macros.
To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.
And then you could use vprintf().
Best Regards,
Jeanine Zhang
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- Marcado como Resposta John paul coder segunda-feira, 1 de junho de 2020 16:19
segunda-feira, 1 de junho de 2020 02:43
Todas as Respostas
-
You could use _vsnprintf_s and vsnprintf - functions that take va_list parameter.
Note however that sizeof(buffer) returns the size of the pointer (4 or 8 bytes), not the size of the block of memory that pointer points to.
Igor Tandetnik
- Marcado como Resposta John paul coder segunda-feira, 1 de junho de 2020 16:19
domingo, 31 de maio de 2020 17:23 -
Hi,
Thank you for posting here.
>>but here the challenge I am facing is, when invoking WrapperSprintf from different places of the project the number of parameters are different.
As far as I'm concerned, you could try to use Variadic macros.
To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.
And then you could use vprintf().
Best Regards,
Jeanine Zhang
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- Marcado como Resposta John paul coder segunda-feira, 1 de junho de 2020 16:19
segunda-feira, 1 de junho de 2020 02:43