Hello,

I will have to build my application with a different compiler at some point.
The secure functions like sprintf_s are a nice feature of the visual c++
compiler. But they will probably not be supported on the target platform. If
I had the possibility to #define a macro which uses sprintf_s if the visual
c++ compiler is used and uses the standard sprintf if a different compiler is
used I would not have to switch off the deprecated warnings.
Can I define a macro with optional arguments as sprintf resp. sprintf_s use?

Thanks for your suggestions,

Fabian

Re: replacing sprintf_s in macro for compiler compatibility by Alex

Alex
Wed Apr 16 09:44:19 CDT 2008

"Fabian" wrote:
> Can I define a macro with optional arguments as sprintf resp.
> sprintf_s use?

Yes, VC++ supports variadic macros:

"Variadic Macros"
http://msdn2.microsoft.com/en-us/library/ms177415.aspx

Alex



Re: replacing sprintf_s in macro for compiler compatibility by adebaene

adebaene
Wed Apr 16 10:50:36 CDT 2008

On 16 avr, 15:37, Fabian <Fab...@discussions.microsoft.com> wrote:
> Hello,
>
> I will have to build my application with a different compiler at some point.
> The secure functions like sprintf_s are a nice feature of the visual c++
> compiler. But they will probably not be supported on the target platform. If
> I had the possibility to #define a macro which uses sprintf_s if the visual
> c++ compiler is used and uses the standard sprintf if a different compiler is
> used I would not have to switch off the deprecated warnings.
> Can I define a macro with optional arguments as sprintf resp. sprintf_s use?

Depending on your exact situation, you should do it another way :
Write your code using printf, and when compiling with Visual C++
#define, _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES = 1

See http://msdn2.microsoft.com/en-us/library/ms175759(VS.80).aspx

Arnaud


Re: replacing sprintf_s in macro for compiler compatibility by Fabian

Fabian
Wed Apr 16 14:02:02 CDT 2008

Hello Arnaud,

> and when compiling with Visual C++
> #define, _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES = 1

How can I find out automatically whether I am compiling with Visual C++? If
I try it with #ifdef _MSVC_ Visual Studio tells me (by greying out the
following code) that _MSVC_ is not defined.

Thanks,

Fabian



Re: replacing sprintf_s in macro for compiler compatibility by Igor

Igor
Wed Apr 16 14:13:00 CDT 2008

Fabian <Fabian@discussions.microsoft.com> wrote:
> How can I find out automatically whether I am compiling with Visual
> C++?

_MSC_VER
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: replacing sprintf_s in macro for compiler compatibility by Fabian

Fabian
Wed Apr 16 15:15:06 CDT 2008

Hello Igor,

now I've got a stupid question: I thought macros would only be defined once
when the header file is declared to be included once (either by #pragma once
or by the #ifdef... #define... #endif alternative). I've written the
following:

#ifdef _MSC_VER
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES = 1
#endif

What I'm getting now is:

warning C4005: '_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES' : macro
redefinition.

I'm including the header file across several dlls.

Thanks,

Fabian

Re: replacing sprintf_s in macro for compiler compatibility by Igor

Igor
Wed Apr 16 16:00:01 CDT 2008

Fabian <Fabian@discussions.microsoft.com> wrote:
> now I've got a stupid question: I thought macros would only be
> defined once when the header file is declared to be included once
> (either by #pragma once or by the #ifdef... #define... #endif
> alternative). I've written the following:
>
> #ifdef _MSC_VER
> #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES = 1
> #endif

Drop the equal sign. You want the value of the macro to be "1", not "=1"

> What I'm getting now is:
>
> warning C4005: '_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES' : macro
> redefinition.

Either some other header also defines this macro, or you have it defined
in your project settings.

If it's not in the project settings, putting it there will help you find
the first definition (as now the _first_ one will be a redefinition and
will produce the warning).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: replacing sprintf_s in macro for compiler compatibility by Tamas

Tamas
Wed Apr 16 19:22:44 CDT 2008

Fabian wrote:

> I had the possibility to #define a macro which uses sprintf_s if the visual
> c++ compiler is used and uses the standard sprintf if a different compiler is
> used

Try to avoid sprintf, it's inherently insecure. Use snprintf if
sprintf_s is not available. Here's how:

char output[OUTPUT_SIZE];
snprintf(output, _countof(output), "%s %d %g", "a", 1, 3.14);
output[_countof(output) - 1] = 0;

Yes, with snprintf you must manually 0 terminate your string, because if
the buffer runs out, snprintf doesn't do that. If the identifier
snprintf doesn't exist, try _snprintf.

With sprintf it can be very difficult to prevent accidental buffer overruns.

Tom

Re: replacing sprintf_s in macro for compiler compatibility by Anders

Anders
Wed Apr 16 19:51:20 CDT 2008

On Wed, 16 Apr 2008 13:15:06 -0700, Fabian
<Fabian@discussions.microsoft.com> wrote:

> #ifdef _MSC_VER
> #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES = 1
> #endif

or

#ifdef _MSC_VER
#ifndef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#endif
#endif
--
A: People bitching about top-posting

> Q: What's the most annoying thing on USENET?

Re: replacing sprintf_s in macro for compiler compatibility by Fabian

Fabian
Thu Apr 17 01:42:00 CDT 2008

Hello Anders,

> #ifdef _MSC_VER
> #ifndef _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
> #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
> #endif
> #endif

I've tried it out - the macro overload warning is gone. But if the macro
value was set to 0 before, it would not be set to 1, right?
Also, VS still produces the deprecated warning. Do I also have to switch it
off with _CRT_SECURE_NO_WARNINGS?

Thanks,

Fabian


Re: replacing sprintf_s in macro for compiler compatibility by Fabian

Fabian
Thu Apr 17 01:46:01 CDT 2008

Hello Tamas,

> Try to avoid sprintf, it's inherently insecure. Use snprintf if
> sprintf_s is not available.

Would you do this with a variadic macro, defining sprintf_s if _MSC_VER is
defined and snprintf otherwise?

Thx,

Fabian

Re: replacing sprintf_s in macro for compiler compatibility by Alex

Alex
Thu Apr 17 01:49:56 CDT 2008

"Tamas Demjen" wrote:
> char output[OUTPUT_SIZE];
> snprintf(output, _countof(output), "%s %d %g", "a", 1, 3.14);
> output[_countof(output) - 1] = 0;
>
> Yes, with snprintf you must manually 0 terminate your string,
> because if the buffer runs out, snprintf doesn't do that.

You can easily avoid it like this:

char output[OUTPUT_SIZE + 1] = { 0 };
snprintf(output, OUTPUT_SIZE, "%s %d %g", "a", 1, 3.14);


Alex