Click here to Skip to main content
Click here to Skip to main content

Keeping It Clean - String Function Wrappers for VS2005

, 17 Nov 2006
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
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. PinmemberWong Shao Voon4-Dec-06 15:13 
GeneralWait a minute, use the defines to eliminate warnings... PinmemberJason King20-Nov-06 15:03 
GeneralRe: Wait a minute, use the defines to eliminate warnings... PinmemberJohn Simmons / outlaw programmer21-Nov-06 1:58 
GeneralRe: Wait a minute, use the defines to eliminate warnings... PinmemberColin Angus Mackay22-Nov-06 10:47 
GeneralRe: Wait a minute, use the defines to eliminate warnings... PinmemberJason King22-Nov-06 11:35 
GeneralRe: Wait a minute, use the defines to eliminate warnings... PinmemberColin Angus Mackay22-Nov-06 11:45 
GeneralRe: Wait a minute, use the defines to eliminate warnings... PinmemberJohn Simmons / outlaw programmer23-Nov-06 15:30 
GeneralNot shure abount the need Pinmemberemilio_grv19-Nov-06 22:21 
GeneralRe: Not shure abount the need PinmemberJohn Simmons / outlaw programmer20-Nov-06 0:19 
GeneralRe: Not shure abount the need Pinmemberemilio_grv20-Nov-06 20:56 
GeneralRe: Not shure abount the need PinmemberJohn Simmons / outlaw programmer21-Nov-06 1:56 
GeneralRe: Not shure abount the need PinmemberWalderMort22-Nov-06 7:43 
GeneralRe: Not shure abount the need Pinmemberemilio_grv23-Nov-06 2:09 
GeneralQuestion [modified] PinmemberS Douglas19-Nov-06 18:18 
GeneralRe: Question PinmemberJohn Simmons / outlaw programmer20-Nov-06 0:12 
GeneralRe: Question PinmemberS Douglas20-Nov-06 1:34 
GeneralSuggestions PinmemberRedFraggle17-Nov-06 7:14 
GeneralRe: Suggestions PinmemberJohn Simmons / outlaw programmer17-Nov-06 9:52 
QuestionRe: Suggestions PinmemberKarstenK19-Nov-06 21:33 
AnswerRe: Suggestions PinmemberJohn Simmons / outlaw programmer20-Nov-06 0:09 
GeneralGot my 5 PinmemberJim Crafton17-Nov-06 5:03 
GeneralWould be better to wrap it Unicode-compliantly PineditorNishant Sivakumar17-Nov-06 4:40 
GeneralRe: Would be better to wrap it Unicode-compliantly PineditorNishant Sivakumar17-Nov-06 4:40 
GeneralRe: Would be better to wrap it Unicode-compliantly PinmemberJohn Simmons / outlaw programmer17-Nov-06 5:02 
GeneralRe: Would be better to wrap it Unicode-compliantly PinmemberJohn Simmons / outlaw programmer17-Nov-06 5:01 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 17 Nov 2006
Article Copyright 2006 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid