Home > Archive > VC Language > November 2005 > Secure CRT questions
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Secure CRT questions
|
|
| Nollie 2005-11-18, 3:58 am |
| How does one create a reusable piece of code that uses the secure crt
functions and have that code compile on VC++ 2003 and 2005? My
understanding is that the secure crt functions are only available when
using the VC++ 2005 compiler. So when compiling in an older compiler,
it would be ideal if the secure crt functions would compile as the
deprecated versions. I could feasibly make macros around _MSC_VER, but
I'm hoping that's already been done. Please help.
The following function, as of now, only compiles on VC++ 2005. What
should I do to get it to compile on VC++ 2003?
#define BUFFER_SIZE 1024
void MyFunction()
{
TCHAR szBuffer1[ BUFFER_SIZE ];
TCHAR szBuffer2[ BUFFER_SIZE ] = TEXT("Example string");
strcpy_s( szBuffer1, BUFFER_SIZE, szBuffer2 );
}
Thanks,
Nollie
| |
| Simon Watson 2005-11-18, 3:58 am |
| Nollie
Take a look at _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES and
_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_
COUNT on MSDN, they should give you
what you want.
Simon
you write
"Nollie" <codepurist@gmail.com> wrote in message
news:1132306501.989992.312140@g47g2000cwa.googlegroups.com...
> How does one create a reusable piece of code that uses the secure crt
> functions and have that code compile on VC++ 2003 and 2005? My
> understanding is that the secure crt functions are only available when
> using the VC++ 2005 compiler. So when compiling in an older compiler,
> it would be ideal if the secure crt functions would compile as the
> deprecated versions. I could feasibly make macros around _MSC_VER, but
> I'm hoping that's already been done. Please help.
>
> The following function, as of now, only compiles on VC++ 2005. What
> should I do to get it to compile on VC++ 2003?
>
> #define BUFFER_SIZE 1024
>
> void MyFunction()
> {
> TCHAR szBuffer1[ BUFFER_SIZE ];
> TCHAR szBuffer2[ BUFFER_SIZE ] = TEXT("Example string");
> strcpy_s( szBuffer1, BUFFER_SIZE, szBuffer2 );
> }
>
> Thanks,
> Nollie
>
| |
| Nollie 2005-11-26, 3:59 am |
| On Fri, 18 Nov 2005 09:50:44 -0000, "Simon Watson" <simon dot watson
at sage dot com> wrote:
>Take a look at _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES and
> _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_
COUNT on MSDN, they should give you
>what you want.
Thanks for replying. These macros have no effect in Visual Studio.Net
2003. The secure CRT functions are defined in the file <string.h>.
This file doesn't seem to be included in the Platform SDK, but rather
is included with Visual Studio 8 itself. Btw, I'm using the Windows
Server 2003 SP1 Platform SDK.
Will the secure CRT functions be included in a future release of the
Platform SDK? Is there a simple way I can get access to them from
VS.NET 2003? What do people do who need a single codebase that
compiles on VS7 and VS8 and uses the secure CRT functions on VS8?
Thanks,
Nollie
| |
| Simon Watson 2005-11-28, 7:59 am |
|
"Nollie" <codepurist@gmail.com> wrote in message
news:q7tfo1taaa84nuiotdq9rbus69l4n8rbju@
4ax.com...
> On Fri, 18 Nov 2005 09:50:44 -0000, "Simon Watson" <simon dot watson
> at sage dot com> wrote:
you[color=darkred]
>
> Thanks for replying. These macros have no effect in Visual Studio.Net
> 2003. The secure CRT functions are defined in the file <string.h>.
> This file doesn't seem to be included in the Platform SDK, but rather
> is included with Visual Studio 8 itself. Btw, I'm using the Windows
> Server 2003 SP1 Platform SDK.
>
> Will the secure CRT functions be included in a future release of the
> Platform SDK? Is there a simple way I can get access to them from
> VS.NET 2003? What do people do who need a single codebase that
> compiles on VS7 and VS8 and uses the secure CRT functions on VS8?
>
> Thanks,
> Nollie
Nollie, from your original post, I understood that you wanted to be able to
have a single piece of code that compiles to the secure routines under VS8
and the deprecasted functions under VS7. To do this you'd need:
#define BUFFER_SIZE 1024
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_
COUNT 1
void MyFunction()
{
TCHAR szBuffer1[ BUFFER_SIZE ];
TCHAR szBuffer2[ BUFFER_SIZE ] = TEXT("Example string");
strcpy( szBuffer1, BUFFER_SIZE, szBuffer2 ); //replaced by secure
function under VS8
}
more detail here: http://msdn2.microsoft.com/en-us/library/ms175759.aspx
cheers
Simon
| |
| Nollie 2005-11-30, 4:02 am |
| On Mon, 28 Nov 2005 13:53:30 -0000, "Simon Watson" <simon dot watson
at sage dot com> wrote:
>Nollie, from your original post, I understood that you wanted to be able to
>have a single piece of code that compiles to the secure routines under VS8
>and the deprecasted functions under VS7. To do this you'd need:
>
>#define BUFFER_SIZE 1024
>#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_
COUNT 1
>
>void MyFunction()
>{
> TCHAR szBuffer1[ BUFFER_SIZE ];
> TCHAR szBuffer2[ BUFFER_SIZE ] = TEXT("Example string");
> strcpy( szBuffer1, BUFFER_SIZE, szBuffer2 ); //replaced by secure
>function under VS8
>}
>
>more detail here: http://msdn2.microsoft.com/en-us/library/ms175759.aspx
>
>cheers
>
>Simon
>
>
>
Thanks for your feedback. I did quite a bit of research on this topic,
and the easiest way to get a single codebase is exactly the way you
have demonstrated. The problem I have, though, is that some of my
functions require a dynamic buffer, so the deprecated functions do not
get overloaded. Actually, the buffer doesn't have to be dynamic, so
long as its size isn't known locally. For example:
void FillBuffer( char *pszBuffer, int cchBuffer )
{
// The overloaded secure functions don't
// know how to use cchBuffer.
strcpy( pszBuffer, "String of text" ); // Warning message
}
int main()
{
char szBuffer[128];
FillBuffer( szBuffer, 128 );
}
It would be nice, and completely possible, if Microsoft would release
a header file that older compilers could include that would work the
opposite way. Basically a macro for every function that would take the
size of the buffer and use it for the secure functions and discard it
for the deprecated ones.
On the other hand, if a programmer has to make all these code changes,
then he/she might as well make it so that all the buffers are static.
This is what I've chosen to do with my code. For example:
int main()
{
char szBuffer[128];
strcpy( szBuffer, "String of text" ); // No warning message
}
There's pros and cons either way.
Thanks again,
Nollie
|
|
|
|
|