Prev: stack overflow problem
Next: Unicode compile question
From: DanB on 25 Apr 2010 14:52 RB wrote: > > CHAR szRem[32]; > hResult = StringCchCopy(szRem, 32/sizeof(TCHAR), "PM_REMOVE"); Things have been said already. So in addition, if you are programing with MFC, simply: CString strRem( _T("PM_REMOVE") ); And get in the habit of using the _T macro. TCHAR without it doesn't make any sense. I said it again as this will pay off later. My fingers automatically do it any more. > LOGFONT NewFontLogStruct; > if ( StringCchCopy( ( TCHAR* )&NewFontLogStruct.lfFaceName, sizeof ( NewFontLogStruct.lfFaceName), "Courier New" ) == S_OK); > { //rest of program execution if the above StringCchCopy went ok } > > You should have gotten a warning about that semicolon at the end of your if(...); Compile with warnings cranked all the way up. You should very rarely, and only if you really know better: #pragma warning(disable : xxxx) The call can simply be: StringCchCopy( lf.lfFaceName, LF_FACESIZE, _T("Courier New") ); lfFaceName will be sized according to build as there is a LOGFONTW and a LOGFONTA.
From: RB on 25 Apr 2010 17:08 Thanks to all replies (Giovanni, Oliver, Joe and DanB) this thread. They were all excellent and very informative. And yes my (TCHAR*) was an novice attempt to compile without an error rather than realize my code was incorrectly implementing the address of the array[x]. All of you have given me everything I asked for and I feel that I can better understand the concept now, which will help me implement the corrections from a conceptual viewpoint as opposed to memorizing your examples. Now if my Cad vocation and other home chores can allow me some time to spend on improving my weak coding ability I can enjoy writing some more code. Very much appreciated, RB
From: RB on 25 Apr 2010 18:45 > Note that you should not be dividing by sizeof(TCHAR), because you have > declared the array as CHAR. It should be > sizeof(szRem) / sizeof(szRem[0]) ---- I think the above did more to move me in the right direction of ramifications of the scale/size factor. I felt I understood this as soon as I saw it though I will have to play with both non unicode and unicode compiles in the debugger to fully visualize the concept. I also read your reply to Oliver which understandably was a bit above my understanding in whole, but I was able in part to get the direction of the context. I have changed my implementation to the below. if ( StringCchCopy ( NewFontLogStruct.lfFaceName, sizeof ( NewFontLogStruct.lfFaceName) / sizeof(NewFontLogStruct.lfFaceName[0]), _T("Courier New") ) == S_OK) { ..........} ------------ I know from experience that if I have still missed the mark you will let me know. Thanks either way.
From: Giovanni Dicanio on 26 Apr 2010 05:06 "Joseph M. Newcomer" <newcomer(a)flounder.com> ha scritto nel messaggio news:ls19t5tepreli46eo8aa1d6tghngvfjr9p(a)4ax.com... > It should be > > sizeof(szRem) / sizeof(szRem[0]) > and this could be done in a template, which is how StringCchCopy does it. Joe: I think that StringCchCopy does not use templates. Reading <strsafe.h> that comes with VS2010, there is the usual UNICODE "trick": #ifdef UNICODE #define StringCchCopy StringCchCopyW #else #define StringCchCopy StringCchCopyA #endif // !UNICODE and the signature of StringCchCopyW is: STRSAFEAPI StringCchCopyW( __out_ecount(cchDest) STRSAFE_LPWSTR pszDest, __in size_t cchDest, __in STRSAFE_LPCWSTR pszSrc) (similar for StringCchCopyA). So, there are no templates; and I think it is OK, because these functions are exported with a pure-C interface, and templates are a C++-only concept. Probably you had in mind that _countof() can be safely implemented using C++ templates. In fact, for example, in VS2010's <atldef.h> header file, it can be read: <code> #if !defined(__cplusplus) #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) #else extern "C++" { template <typename _CountofType, size_t _SizeOfArray> char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray]; #define _countof(_Array) sizeof(*__countof_helper(_Array)) } #endif </code> So, in case of pure C (i.e. __cplusplus not defined), _countof() is implemented in the usual (sizeof(array)/sizeof(array[0])) way. But in case of C++ builds, they use the power of C++ templates in defining _countof. Thanks to this template "magic" (template argument deduction), _countof causes compile-time error if it is (mis)used with something different from a static array (e.g. a pointer). I mean, this code (which is wrong) compiles fine in C builds, but fails at compile-time in C++ builds (VS2010): TCHAR * pszBuffer; int cchBuffer = _countof(pszBuffer); Giovanni
From: Pete Delgado on 26 Apr 2010 13:20
"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message news:jb29t5hvlb45496p1tb2540hgr67htltcj(a)4ax.com... > THe CORRECT code would be > > CString Rem(_T("PM_REMOVE")); > > or possibly > > CStrin Rem = _T("PM_REMOVE")); Skitt's Law: "Any post correcting an error in another post will contain at least one error itself". http://www.telegraph.co.uk/technology/news/6408927/Internet-rules-and-laws-the-top-10-from-Godwin-to-Poe.html Sorry Joe, I couldn't resist! The obvious typo reminded me of the top 10 internet rules! ;-) The above article is funny and interesting reading IMHO! -Pete |