locked
How to write a platform independent wrapper function in C++ RRS feed

  • Question

  • 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);
    
    ...
    }



    Sunday, May 31, 2020 9:30 AM

Answers

  • 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

    Sunday, May 31, 2020 5:23 PM
  • 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.

    Monday, June 1, 2020 2:43 AM

All replies

  • 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

    Sunday, May 31, 2020 5:23 PM
  • 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.

    Monday, June 1, 2020 2:43 AM