|
Hi all
Please help, I have next code :
I am building in Unicode
TCHAR array[100];
CString strText = _T("Cool");
_tcscpy_s(array,
sizeof(array),
strText
)
But the program halts and hangs, when performs _tcscpy_s method.
thank you
-- modified at 11:16 Friday 2nd June, 2006
|
|
|
|
|
There's no problem in the snippet of code you have shown. The error, presumably, lies elsewhere.
Regards,
Nish
|
|
|
|
|
big_denny_200 wrote: But the program halts and hangs, when performs _tcscpy_s method.
How are you verifying this?
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
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.
-- modified at 11:33 Friday 2nd June, 2006
|
|
|
|
|
Try explicitly casting the CString object:
<br />
TCHAR array[100];<br />
<br />
CString strText = _T("Cool");<br />
<br />
_tcscpy_s(array,
sizeof(array),
(LPCTSTR)strText
);<br />
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 to LPCTSTR is not required even in printf -like calls -- at least in VS 6.0).
|
|
|
|
|
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
|
|
|
|
|
It shouldn't be, but I've run into problems with it converting (implicitly) to char* instead of wchar_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 from sizeof(array) to sizeof(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
|
|
|
|
|
What is happening ? I am replying to Viorel Bejan and this post gooes in reply to Zac Howland's post
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
|
|
|
|
|
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
|
|
|
|
|
Word, man! 
|
|
|
|
|
Viorel 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
|
|
|
|
|
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.
Regards,
Nish
|
|
|
|
|
According 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
|
|
|
|
|
I am using Visual Studio C++.NET
I am familiar with creating dialog applications. But i want to include menus
However, i would like to create a windows application with menu options, file etc.. and at the same time, include controls like buttons drop down boxes, picture control area. Can anyone direct me to a good tutorial or if you can explain some basic steps i'd be grateful... I know that it should be easy, i just don't know where to start...
Thanks,
|
|
|
|
|
LCI wrote: ...at the same time, include controls like buttons drop down boxes, picture control area.
In the menu, or the client area?
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
Well, in the client area. I suspect that if i create an MFC Single or multiple document application, i will get the menu. But if i do that. Now i have a menu, but i want to be able to add controls and buttons in the client area just like i would in a dialog based application.
Do you recommend me generating the project as a win32 project?
I am currently creating projects of different types just to see if i can get a feel for doing the above but cannot seem to figure it out.
|
|
|
|
|
LCI wrote: Well, in the client area.
Then just derive the view from CFormView .
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
I really need help on this one.
I have a VS6 app that I loaded in VS.NET 2003. Convertion ok, compile ok, runs ok.
Now, I don't know what happend but the control IDs dont get listed in the events pane of dialog properties. Also, if I choose 'Add variable' from the context menu after right clicking a control, the add variable dialog does not allow me to add a 'control' variable. The "Control variable" check box is disabled.
Looks like some link between the resoirce IDs and the dialog properties in VS is broken.
Hope I explain this clearly enough.
Anyone has an idea why this is happening, I been on this probleme for quite a while and really need a way to fix this. ?
Louis
* google is your friend *
|
|
|
|
|
Since your problem is difficult to reproduce, I would suggest you to perform a test: make a simple new application in VS 6 (of the same type -- MDI, Dialog-based, etc.), and then convert it to VS 2003 (hope you have VS 6). Then see if the same problem appears.
I think the problem can be caused by some missing special comments (like "//{{AFX_MSG ", used widely by older VS wizards) accidentally deleted from source files.
|
|
|
|
|
Hi,
If I remember, I think that it was working at some point. So, for sure, I must have broken something. I have other apps that I converted and I dont have this problem.
I have been comparing with other projects t osee what can be different but with no avail.
If I'm not mistaking, the "//{{AFX_MSG " and others similar, are not used anymore by VS.NET. So even if they are not in the source file it wont affect. In fact, new classes created for new dialogs dont have any of those special comments (tags).
Heres some more info.
If I click a control on a dialog form to select it, then the event pane in the properties fills with the pssible notification messages for that control. This works normally. If there is a function handler to a notification message it is also listed. I can also add a new handler to the seelcted control this way.
Control Ids dont get listed only when the dialog is selected, when usually all the control IDs would appear.
The biggest problem I have with this, and which is causing me a lot of delays, is that I cannot add control variables.
Louis
* google is your friend *
|
|
|
|
|
Finally found the problem, so I post the solution here for anyone else that would stumble on this problem.
My original project was a VC6 project. It was converted by VC7. At this point I think resources were ok. I was using two resource files in this project, just to split them up so it is easier to work in a team environnement. I also was including a resource file from another project which is a global library for many of our projects.
I am not to sure how the problem occured, but the project lost track of which of the resource file was the main resource file. The main resource file is saved in the .vcproj file. At the end of this file there is a <Globals> then a <Global> section in which the main resource file path appears. Mine was pointing to the wrong one, after putting back the proper path, everything was back to normal.
Took me a while to find this after trying several things.
Case closed.
Louis
* google is your friend *
-- modified at 10:51 Tuesday 11th July, 2006
|
|
|
|
|
We are creatng an application that hooks up event sinks to IE html elements.We want to start with sinks for html text boxes only;and later proceed on to include sinks for other elements.
For this we plan to have a static registry which contains key-mapped Classfactory instances.
A typical entry in our case can be HTMLTextElement vs. ClassFactory instance for the HTMLTextElement sink.
This will be inserted into the registry as part of the static initialization of the classfactory.
When a html page containing text elements is invoked,the classfactory instance from the registry will be returned corresponding to the key.
This will be used to create and hook up an instance of the listener.
Can someone give a pointer on how to implement the above concept in ATL?
|
|
|
|
|