wcscpy_s issues [modified]
-
Hi all :) Please help, I have next code : I am building in Unicode
TCHAR array[100];
CString strText = _T("Cool");
_tcscpy_s(array,//Location of destination string buffer
sizeof(array),// Size of the destination string buffer.
strText//Null-terminated source string buffer.
)But the program halts and hangs, when performs
_tcscpy_s
method. thank you -- modified at 11:16 Friday 2nd June, 2006Since
_tcscpy_s
is expanded towcscpy_s
call in Unicode version, you must specify the size of destination buffer in 2-byte unicode characters, not in bytes. Universal solution can look like this:_tcscpy_s(array,
sizeof(array) / sizeof(TCHAR),
strText);You got halts even if the source string was short enought, because -- in Debug version -- this function fills the buffer with
0xFD
value. -- modified at 11:33 Friday 2nd June, 2006 -
Hi all :) Please help, I have next code : I am building in Unicode
TCHAR array[100];
CString strText = _T("Cool");
_tcscpy_s(array,//Location of destination string buffer
sizeof(array),// Size of the destination string buffer.
strText//Null-terminated source string buffer.
)But the program halts and hangs, when performs
_tcscpy_s
method. thank you -- modified at 11:16 Friday 2nd June, 2006Try explicitly casting the CString object:
TCHAR array[100]; CString strText = _T("Cool"); _tcscpy_s(array,//Location of destination string buffer sizeof(array),// Size of the destination string buffer. (LPCTSTR)strText//Null-terminated source string buffer. );
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -
Hi all :) Please help, I have next code : I am building in Unicode
TCHAR array[100];
CString strText = _T("Cool");
_tcscpy_s(array,//Location of destination string buffer
sizeof(array),// Size of the destination string buffer.
strText//Null-terminated source string buffer.
)But the program halts and hangs, when performs
_tcscpy_s
method. thank you -- modified at 11:16 Friday 2nd June, 2006big_denny_200 wrote:
But the program halts and hangs, when performs _tcscpy_s method.
(Offtopic sarcasm) Glad to see these "safe" functions in action.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Try explicitly casting the CString object:
TCHAR array[100]; CString strText = _T("Cool"); _tcscpy_s(array,//Location of destination string buffer sizeof(array),// Size of the destination string buffer. (LPCTSTR)strText//Null-terminated source string buffer. );
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -
(Actually, explicit casting from
CString
toLPCTSTR
is not required even inprintf
-like calls -- at least in VS 6.0).It shouldn't be, but I've run into problems with it converting (implicitly) to
char*
instead ofwchar_t*
when I didn't explicitly cast it. Looking back at the code again, I think he will run into another problem though. In non-UNICODE builds, he shouldn't notice anything, but in UNICODE builds, the sizeof(array) will actually return twice the size of the actual buffer. I believe the wstcpy_s function requires array size in elements, not bytes (I'll have to double-check that though). If I'm correct, he would just need to change that line fromsizeof(array)
tosizeof(array) / sizeof(TCHAR)
. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac -
Since
_tcscpy_s
is expanded towcscpy_s
call in Unicode version, you must specify the size of destination buffer in 2-byte unicode characters, not in bytes. Universal solution can look like this:_tcscpy_s(array,
sizeof(array) / sizeof(TCHAR),
strText);You got halts even if the source string was short enought, because -- in Debug version -- this function fills the buffer with
0xFD
value. -- modified at 11:33 Friday 2nd June, 2006Could you be more explicit ? MSDN says, that second parameter of wcscpy_s must specify size of destination buffer in bytes, therefore in Unicode build destination size will be 100 * sizeof(TCHAR) (which is equal to
sizeof(array)
), but in you case it will be 100(which is not the size of destination buffer) I am little confused. thanks -- modified at 12:02 Friday 2nd June, 2006 -
Since
_tcscpy_s
is expanded towcscpy_s
call in Unicode version, you must specify the size of destination buffer in 2-byte unicode characters, not in bytes. Universal solution can look like this:_tcscpy_s(array,
sizeof(array) / sizeof(TCHAR),
strText);You got halts even if the source string was short enought, because -- in Debug version -- this function fills the buffer with
0xFD
value. -- modified at 11:33 Friday 2nd June, 2006What is happening ? I am replying to Viorel Bejan and this post gooes in reply to Zac Howland's post :confused:
Viorel Bejan wrote:
Since _tcscpy_s is expanded to wcscpy_s call in Unicode version, you must specify the size of destination buffer in 2-byte unicode characters, not in bytes. Universal solution can look like this: _tcscpy_s(array, sizeof(array) / sizeof(TCHAR), strText); You got halts even if the source string was short enought, because -- in Debug version -- this function fills the buffer with 0xFD value.
Could you be more explicit ? MSDN says, that second parameter of wcscpy_s must specify size of destination buffer in bytes, therefore in Unicode build destination size will be 100 * sizeof(TCHAR) (which is equal to
sizeof(array)
), but in you case it will be 100(which is not the size of destination buffer) I am little confused. thanks -- modified at 12:02 Friday 2nd June, 2006 -- modified at 12:04 Friday 2nd June, 2006 -
big_denny_200 wrote:
But the program halts and hangs, when performs _tcscpy_s method.
(Offtopic sarcasm) Glad to see these "safe" functions in action.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Nemanja Trifunovic wrote:
(Offtopic sarcasm) Glad to see these "safe" functions in action.
:-D Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New) -
Since
_tcscpy_s
is expanded towcscpy_s
call in Unicode version, you must specify the size of destination buffer in 2-byte unicode characters, not in bytes. Universal solution can look like this:_tcscpy_s(array,
sizeof(array) / sizeof(TCHAR),
strText);You got halts even if the source string was short enought, because -- in Debug version -- this function fills the buffer with
0xFD
value. -- modified at 11:33 Friday 2nd June, 2006Viorel Bejan wrote:
You got halts even if the source string was short enought, because -- in Debug version -- this function fills the buffer with 0xFD value.
But in this specific case, it's not bad enough to cause a halt - since he has a 100 byte buffer and a 5 character string. Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New) -
What is happening ? I am replying to Viorel Bejan and this post gooes in reply to Zac Howland's post :confused:
Viorel Bejan wrote:
Since _tcscpy_s is expanded to wcscpy_s call in Unicode version, you must specify the size of destination buffer in 2-byte unicode characters, not in bytes. Universal solution can look like this: _tcscpy_s(array, sizeof(array) / sizeof(TCHAR), strText); You got halts even if the source string was short enought, because -- in Debug version -- this function fills the buffer with 0xFD value.
Could you be more explicit ? MSDN says, that second parameter of wcscpy_s must specify size of destination buffer in bytes, therefore in Unicode build destination size will be 100 * sizeof(TCHAR) (which is equal to
sizeof(array)
), but in you case it will be 100(which is not the size of destination buffer) I am little confused. thanks -- modified at 12:02 Friday 2nd June, 2006 -- modified at 12:04 Friday 2nd June, 2006Actually, it says the second parameter of wcscpy_s must specify size of destination buffer in words (in bytes for non-Unicode). Thus, it is the size in characters and not size in bytes! If you are using a statically-allocated array as a destination, you can use the _countof macro instead of sizeof. However, sizeof(array) / sizeof(TCHAR) also returns the correct size.
-
Actually, it says the second parameter of wcscpy_s must specify size of destination buffer in words (in bytes for non-Unicode). Thus, it is the size in characters and not size in bytes! If you are using a statically-allocated array as a destination, you can use the _countof macro instead of sizeof. However, sizeof(array) / sizeof(TCHAR) also returns the correct size.
thanks, I did not pay attention to the WORD
-
thanks, I did not pay attention to the WORD
Word, man! :)
-
Hi all :) Please help, I have next code : I am building in Unicode
TCHAR array[100];
CString strText = _T("Cool");
_tcscpy_s(array,//Location of destination string buffer
sizeof(array),// Size of the destination string buffer.
strText//Null-terminated source string buffer.
)But the program halts and hangs, when performs
_tcscpy_s
method. thank you -- modified at 11:16 Friday 2nd June, 2006According to: http://msdn2.microsoft.com/en-us/library/td1esda9.aspx[^] the sizeof(array) should correspond to the number of 'characters' - not 'bytes'. So - in case of ANSI, it's bytes. In case of unicode, it's words (2-bytes). When the code you listed compiles for unicode, sizeof(array) is 200 - which is double the actual number of characters. Note that _tcscpy_s() zeros out the buffer after copying... and that's when you get the buffer overrun. You can find that out by stepping into _tcscppy(). You can use (sizeof(array)/sizeof(array[0])) or the _countof() macro. gmileka