NO

Author Topic: Does use of Strsafe functions not allow a console application compile?  (Read 10987 times)

EdPellesC99

  • Guest
Re: Does use of Strsafe functions not allow a console application compile?
« Reply #15 on: April 26, 2010, 05:31:29 PM »
John,

  Yes it compiles without the define (I find this morning).

I thought I commented that define, and compile failed yesterday !

Guess it was a brain cramp instead :)
Thanks.

EdPellesC99

  • Guest
Re: Does use of Strsafe functions not allow a console application compile?
« Reply #16 on: April 26, 2010, 06:10:30 PM »
Winding this thread up, I want to emphasize the fact that all examples I had found (everywhere) using safe string functions - use the TCHAR datatype.
  Yesterday I found:
http://msdn.microsoft.com/en-us/library/7dzey6h6(VS.80).aspx

To simplify code development for various international markets, the Microsoft run-time library provides Microsoft-specific
"generic-text" mappings for many data types, routines, and other objects. These mappings are defined in TCHAR.H.
You can use these name mappings to write generic code that can be compiled for any of the three kinds of character sets: ASCII (SBCS), MBCS, or Unicode,
depending on a manifest constant you define using a #define statement.

Generic-text mappings are Microsoft extensions that are not ANSI compatible.


'' ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •
So this is why, using the TCHAR data type, you must compile with the -Ze option.
  I see now that using this data type has it's purpose ..... it is just a bit more involved.

'' ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •

So, as a variation here is my complete file that is based on the CHAR data type, you still must compile with the -Ze option.
This reiterates what Timo said earlier, that when using the windows.h header you should enable Microsoft extentions.

Code: [Select]
#include <windows.h>
#define arraysize 75
//#define strsafe_no_deprecate
// apparently the above define is the default
#pragma comment (lib, "strsafe.lib")
#include <strsafe.h>
// // ~ This now compiles with Pelles C / Simple Console App, ! Required: -Ze option on compiler
// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •

int main()
{
char pszDest[arraysize];
int cchDest = sizeof(pszDest)-1;

char *pszFormat = ("%s %s %s %s");   // // ~ NOTE no commas between specifiers !
char *pszTxt = "Hello World.";
char *pszTxt1 = "I am struggling,";
char *pszTxt2 = "with";
char *pszTxt3 = "my simple Console App compile.";

StringCchPrintf(pszDest, cchDest, pszFormat, pszTxt, pszTxt1, pszTxt2, pszTxt3);

printf("pszDest is:\n\n\"%s\"", pszDest);
getchar();

return 0;
}


Thanks everyone for your much appreciated help !

----------------------------Ed
 :)

« Last Edit: April 27, 2010, 12:23:30 AM by EdPellesC99 »