|
I'm having somewhat of an issue with the new 'safe' string functions. Take the following example:
char foo[ 512 ] = {0};
strcat_s( foo, 512, "Hello World" );
char bar[ 512 ];
ZeroMemory( bar, 512 );
strcpy_s( bar, 512, foo );
Nothing wrong with the code you might think, but add bar to your watch list. Even though the memory was zero'd out, the string was written, but everything after the null character has been set to 0xFD.
In most cases this is not a problem, but I am using these with a memory mapped file. And it's causing havoc. Even strncpy_s displays the same problem. The only solution is to use CopyMemory() , after calling strlen() and adding the null.
-- modified at 19:11 Tuesday 13th February, 2007
|
|
|
|
|
WalderMort wrote: strcpy_s( bar, 512, bar);
According to MSDN "The behavior of strcpy_s is undefined if the source and destination strings overlap".
But I am sure that is a typo on your part and not what you are after.
Again from MSDN: "The debug versions of these functions first fill the buffer with 0xFD. To disable this behavior, use _CrtSetDebugFillThreshold ".
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
PJ Arends wrote: But I am sure that is a typo on your part and not what you are after.
Well spotted.
I must have missed that part of the docs. I was sure it was a debug only thing, but still, it is annoying.
|
|
|
|
|
I have created a custom control and I place text in it in the OnPaint function. If the user picks in the control, I want to get the text that is under the cursor. How can that be done? My control is derived off of CWnd.
|
|
|
|
|
If it's your own control, you can write a method on the control, which takes a co-ordinate on the screen, turns it into a local co-ordinate, and works out what word it is over. You can do this, as you have complete control over how the text is rendered.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I had the coordinate part figured out, but what I havn't figured out yet is how to tell what word it is over. When I use DrawText to put the text in the control, how can I tell where each word is located in that text? What I am basically trying to do is similar to a hyperlink, if the user picks certain text in the control, I want to perform some action. The only idea I had was to manually figure out each word location when placing them in the control and keep a map of the rectangular area used for each word. But, this would be a lot of work, and memory, and I was hoping there would be an easier way. The text can be multiple paragraphs. Thanks for any suggestions.
|
|
|
|
|
RandyS wrote: The only idea I had was to manually figure out each word location when placing them in the control and keep a map of the rectangular area used for each word.
That's pretty much how a hyperlink works. Once text is rendered to the screen it's just pixels
(unless your video card is in text mode - unlikely with Windows ).
You'd need OCR to convert the pixels back to text.
Alot of work? Maybe. Alot of memory? Nah
|
|
|
|
|
RandyS wrote: When I use DrawText to put the text in the control, how can I tell where each word is located in that text?
There is an API which tells you the size of the text you're rendering, you'd need to render one word at a time and keep track of how big each one is on screen, to work out which word holds which co-ordinates.
RandyS wrote: The only idea I had was to manually figure out each word location when placing them in the control and keep a map of the rectangular area used for each word.
Yes, that's about it.
RandyS wrote: But, this would be a lot of work, and memory
A little work, the memory usage is trivial.
RandyS wrote: and I was hoping there would be an easier way
No. There isn't.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
My program gives me a problem in XP and XP alone. The main program runs fine, but when I open a form that then runs another thread and some other tasks if the user clicks on the dialog while the tasks are being processed the form won't close after the tasks are complete (as it should). The tasks are all completed correctly except the form won't close. I also get a "Not responding message" in the dialog top bar when the user is clicking on the form. I throught maybe i needed to run the DisableProcessWindowsGhosting() function at the beginning of my program, however my program does'nt not seem to recognize that function. I have inclued "winuser.h" as a header. Any thoughts on this, either how to get the DisableProcessWindowsGhosting() to work, or another possible solution.
Note: I have also tried the PeekMessage function but this did not work.
Cheers* Any help would be greatful .
Debs*
|
|
|
|
|
Hi...
If there is a better way to do what I am doing, I'll jump on it.
I am using Sleep(milliseconds) in my code to place strategic waits for time to pass. As an example, I need to stop the application because I know that something going on in the real world will take at least 100 msecs to finish and I don't have anyway to handshake with this device/application. So I put a Sleep(100). This has worked ok until recently when I tried running my app in one of the newer PCs with the real fast processor etc.
Now my Sleep()'s are too short.
Is there a way to figure out what the Sleep() will be in msecs from one PC to another?
Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be?
Like I said earlier, if there is a better and more accurate way to do this the using Sleep(), I am open to all suggestions...
Regards and thank you in advance
Pierre
|
|
|
|
|
Use a timer, a timers interval is set in milliseconds.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
That's surprising. I would have thought that calling Sleep(100); would suspend the process for at least (but not necessarily exactly) 100mSec.
/ravi
|
|
|
|
|
So you are saying that new fast processors alter time? milliseconds are milliseconds (I thought).
pblais wrote: Also... what is the accuracy of, let's say, Sleep(20). How close to waiting 20 msecs will that be?
I believe the accuracy of Sleep() can vary from +-10ms to +-55ms, depending on the system.
A possibly interesting article: Precision is not the same as accuracy[^]
|
|
|
|
|
Mark Salsbery wrote: So you are saying that new fast processors alter time? milliseconds are milliseconds (I thought).
Everyone likes a wise cracker
|
|
|
|
|
You're the one that stated "Now my Sleep()'s are too short"
If you choose to use a timer then you'll need to use a multimedia timer. Chances are a WM_TIMER
through the SetTimer API is going to be just as inaccurate.
I agree with another poster that the problem is probably related to the time the thread's awake,
not the time it's suspended in Sleep().
Do you do I/O with the external device you are trying to sync to? If so, is there any way you
can use overlapped I/O or soething to make it a bit more event driven instead of trying to fine-
tune timeslices of threads?
Mark
|
|
|
|
|
Mark Salsbery wrote: Do you do I/O with the external device you are trying to sync to? If so, is there any way you
can use overlapped I/O or soething to make it a bit more event driven instead of trying to fine-
tune timeslices of threads?
Yes sometimes. I am writing test software. Sometimes I have to wait for another piece of test equimpmen to go through it's boot up self test and I have no way knowing when it is done. I do know that it takes it approximately 300 mSec. So I put a Sleep(350) in my code to wait for it. I have recently found, on some newer/faster PCs that the application isn't waiting long enough...
|
|
|
|
|
I suspect the problem is in the ratio of the “wake time” to the “sleep time”. On faster machines the “wake time” is lower but the “sleep time” remains about the same.
Steve
|
|
|
|
|
pblais wrote: I don't have anyway to handshake with this device/application.
pblais wrote: when I tried running my app in one of the newer PCs
pblais wrote: Now my Sleep()'s are too short.
Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded?
Otherwise at some point your application will be Sleep()ing with the fishes.
led mike
|
|
|
|
|
led mike wrote: Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded?
And use a trackbar for fine-tuning!
|
|
|
|
|
led mike wrote: Maybe the parameter for the Sleep() call or Timer event should be configurable rather than hard coded?
I like that idea... How would I make configurable?
|
|
|
|
|
pblais wrote: How would I make configurable?
Instead of Sleep(100)
something like this:
Sleep( Config.Milliseconds);
where the number in that variable is provided by a user or administrator.
led mike
|
|
|
|
|
|
I wonder if he wants special sauce on his Fish Filet?
led mike
|
|
|
|
|
The whole thing brings back memories of my first computer job - interfacing with a
home-brewed laser scanner. All the magic numbers in the legacy code, and how the new 386
CPU messed it all up LOL! To this day, it's still referred to (by myself and my business
partner, who worked with me then) as the Laser Bread Slicer (that, we figured, was the extent of
its accuracy).
So, typical tech support goes something like "See the edit box labeled 'Fudge Factor'? Try
entering 203 insead of 198 and see if that fixes it."
Thanks man...I thought I had successfully repressed those memories
|
|
|
|
|
As has been said, the number you pass to Sleep() is just a request. Your thread might not wake up for a minute if some high-priority thread grabs the CPU. Something more accurate than Sleep() seems to be in order, check out multimedia timers and waitable timers.
|
|
|
|