Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C++
Article

Keeping It Clean - String Function Wrappers for VS2005

Rate me:
Please Sign up or sign in to vote.
4.28/5 (23 votes)
17 Nov 20062 min read 94.3K   11   48
A technique for making stdio functions compile clean regardless of the Microsoft compiler used

Introduction

I have to maintain code that was originally written during the heyday of Visual Studio 6.0, but that also needs to be used in newer projects that are compiled under Visual Studio 2005. The files in question are shared between two programming teams - one is writing VC6 code, and the other is writing VS2005 code - the projects are different but we're all about code re-use. I'm sure there are at least three other people out there in CP-land that have the same requirements.

Specifically, our code invariably includes the use of several standard I/O functions like sprintf, printf, et al, and we all know what happens when you try to use these functions in VS2005 - the compiler pukes out hundreds (or thousands) of warnings about the functions being deprecated. The quick fix is to include the appropriate preprocessor definition in the program settings, but that's just plain laziness.

How I Solved The Problem

My technique was to write a wrapper function that will use the appropriate version of the stdio function, depending on what version of the compiler you're using. This really cleaned up my code in terms of readability because the preprocessor code is all in one place instead of being scattered through the code (about 50 instances in just one file).

// Determine which compiler we're using.  For versions PRIOR to 2005, we use 
// the old ("unsecure" - guffaw) string functions that VS2005 pukes up
// warnings on.  This presents quite the pain in the butt when you're trying  
// to write code supported by multiple versions of Visual C.

#define _VS2005_
#if _MSC_VER < 1400
    #undef _VS2005_
#endif

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int sprintf_ex(char* sDest, int nSize, char* sFormat, ...) 
{
    va_list argList;
    va_start(argList, sFormat);
    int nCount = 0;

#ifdef _VS2005_
    nCount = _vsnprintf_s(sDest, nSize, nSize-1, sFormat, argList);
#else
    nCount = _vsnprintf(sDest, nSize, sFormat argList);
#endif
    va_end(argList);

    return nCount;
}

Implementation might look like this:

char sTest[1024];
int nCount = sprintf_ex(sTest, sizeof(sTest), "%d %s", 5, "Test");

It would be a simple exercise to wrap other functions in a similar fashion, but since we didn't have a need, I didn't write the code. This should be a simple exercise for most C++ programmers, given the example above.

What?! No Sample Code To Download?!!!

Oh please. As you can see, it's just a function wrapper that demonstrates a simple technique. I'm not going to go through the hassle of preparing a sample project for something that you simply have to copy/paste into an existing source file to try out. Besides, you're all supposed to be programmers - create a test project if you have to.

What?! No Screen Shots To Gawk At?!?!

You guys are funny. Now it's my turn to be funny.

Disclaimer

I do not code in managed C++, so I don't know if this will work in that environment.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralI really like this article. Pin
Shao Voon Wong4-Dec-06 15:13
mvaShao Voon Wong4-Dec-06 15:13 
GeneralWait a minute, use the defines to eliminate warnings... Pin
Jason.King.Work@gmail.com20-Nov-06 15:03
Jason.King.Work@gmail.com20-Nov-06 15:03 
GeneralRe: Wait a minute, use the defines to eliminate warnings... Pin
#realJSOP21-Nov-06 1:58
mve#realJSOP21-Nov-06 1:58 
GeneralRe: Wait a minute, use the defines to eliminate warnings... Pin
Colin Angus Mackay22-Nov-06 10:47
Colin Angus Mackay22-Nov-06 10:47 
GeneralRe: Wait a minute, use the defines to eliminate warnings... Pin
Jason.King.Work@gmail.com22-Nov-06 11:35
Jason.King.Work@gmail.com22-Nov-06 11:35 
GeneralRe: Wait a minute, use the defines to eliminate warnings... Pin
Colin Angus Mackay22-Nov-06 11:45
Colin Angus Mackay22-Nov-06 11:45 
GeneralRe: Wait a minute, use the defines to eliminate warnings... Pin
#realJSOP23-Nov-06 15:30
mve#realJSOP23-Nov-06 15:30 
GeneralNot shure abount the need Pin
Emilio Garavaglia19-Nov-06 22:21
Emilio Garavaglia19-Nov-06 22:21 
GeneralRe: Not shure abount the need Pin
#realJSOP20-Nov-06 0:19
mve#realJSOP20-Nov-06 0:19 
GeneralRe: Not shure abount the need Pin
Emilio Garavaglia20-Nov-06 20:56
Emilio Garavaglia20-Nov-06 20:56 
GeneralRe: Not shure abount the need Pin
#realJSOP21-Nov-06 1:56
mve#realJSOP21-Nov-06 1:56 
GeneralRe: Not shure abount the need Pin
Waldermort22-Nov-06 7:43
Waldermort22-Nov-06 7:43 
GeneralRe: Not shure abount the need Pin
Emilio Garavaglia23-Nov-06 2:09
Emilio Garavaglia23-Nov-06 2:09 
GeneralQuestion [modified] Pin
S Douglas19-Nov-06 18:18
professionalS Douglas19-Nov-06 18:18 
GeneralRe: Question Pin
#realJSOP20-Nov-06 0:12
mve#realJSOP20-Nov-06 0:12 
GeneralRe: Question Pin
S Douglas20-Nov-06 1:34
professionalS Douglas20-Nov-06 1:34 
GeneralSuggestions Pin
RedFraggle17-Nov-06 7:14
RedFraggle17-Nov-06 7:14 
GeneralRe: Suggestions Pin
#realJSOP17-Nov-06 9:52
mve#realJSOP17-Nov-06 9:52 
QuestionRe: Suggestions Pin
KarstenK19-Nov-06 21:33
mveKarstenK19-Nov-06 21:33 
AnswerRe: Suggestions Pin
#realJSOP20-Nov-06 0:09
mve#realJSOP20-Nov-06 0:09 
GeneralGot my 5 Pin
Jim Crafton17-Nov-06 5:03
Jim Crafton17-Nov-06 5:03 
GeneralWould be better to wrap it Unicode-compliantly Pin
Nish Nishant17-Nov-06 4:40
sitebuilderNish Nishant17-Nov-06 4:40 
GeneralRe: Would be better to wrap it Unicode-compliantly Pin
Nish Nishant17-Nov-06 4:40
sitebuilderNish Nishant17-Nov-06 4:40 
GeneralRe: Would be better to wrap it Unicode-compliantly Pin
#realJSOP17-Nov-06 5:02
mve#realJSOP17-Nov-06 5:02 
GeneralRe: Would be better to wrap it Unicode-compliantly Pin
#realJSOP17-Nov-06 5:01
mve#realJSOP17-Nov-06 5:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.