|
A good catch that does the job. Setting it to zero and then putting the value back after the code solves my problem.
Well you learn something everyday, I was completely unaware they had added that functionality into the safe routines but at least they do realize the problem and allow control of it.
In vino veritas
|
|
|
|
|
I reckon it has something to do with _tcscpy_s() internally calling _FILL_STRING() for the balance of the destination string. If you can live without the safe version, _tcscpy() will work as expected.
It looks like you may need to use memcpy() instead.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 29-Sep-16 0:11am.
|
|
|
|
|
You are correct the FILL_STRING is the culprit and no I need the function so will just do the buffer fill long handed. As Richard worked out it isn't there in release mode so it's something to do with debug mode display functions. My guess is it's a pattern for there watch for change implementation.
It did however totally catch me out
Update: Chris correctly identified why it's doing it and how you control it.
In vino veritas
modified 28-Sep-16 2:28am.
|
|
|
|
|
leon de boer wrote: ...so will just do the buffer fill long handed. Just curious but what does it matter what's after the \0 value in the buffer?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I was try to make a filter for OPENFILENAME
OPENFILENAME structure (Windows)[^]
It's not a great structure from MS and it look something like this "DXF Files\0*.dxf\0PLT Files\0*.plt\0\0 "
You can't string copy that in because of the terminating 0 characters in it.
For me it's worse "DXF files", "PLT files" is the English version I have German, French etc strings
to replace it so the string changes.
So I have to build that structure and the old code basically relied on the buffer being zeroed
and it just wrote the string bits in without having to track all the string lengths. It was assumed
strcpy doesn't play with parts of the buffer it isn't writing the result into.
So the correct way to write it would be strcpy + strcat + strcat + strcat + strcat(\0).... which I did.
The safe functions mean you have to track the amount of buffer left to pass in and so it becomes
a lot of code for a very trivial thing ... BUT IT'S GUARANTEED TO WORK
So basically while I was trying to debug the program the code was failing because the code relied
on something not guaranteed. For such a simple string structure its a pain to have to make code
track multiple string pointers just to create it but I should have done it .. lesson 1 never assume
anything not actually specified!!!!
The bigger issue for me and why I went after it was performance. If they are playing around with
stuff they don't need to they are wasting time. String functions are used often and everywhere
and on very large memory blocks and I was concerned about the impact.
In vino veritas
modified 29-Sep-16 0:12am.
|
|
|
|
|
leon de boer wrote:
You can't string copy that in because of the terminating 0 characters in it. Why would you want to? The lpstrFilter member is a pointer not an "array". You should have something like:
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = "DXF Files\0*.dxf\0PLT Files\0*.plt\0";
... Now if you are wanting a dynamic filter (one that changes at runtime), that's a horse of a different color, although still do-able.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Edit: Correction yes that works as a static.
But yes that is a static create but my strings are coming from memory the user can switch language on the fly and the number of types of file that can be loaded depends on version purchased. Bascially I can't possible do it with static strings I have to build the string into an array and pass the pointer to the array. The array is created and disposed of by the dialog and all very normal when doing multilingual dialog functions.
Many of the string bits are coming from windows via the locale API and heaven only knows how long they are and where they are coming from in memory.
I need the array because I have to assemble all the string somewhere.
I would like to see a dynamic form of your code without allocating an array on the heap or local stack
Anyhow it's all working now, the performance was more the issue to me and that is resolved.
In vino veritas
modified 29-Sep-16 1:14am.
|
|
|
|
|
leon de boer wrote:
I need the array because I have to assemble all the string somewhere. Like was previously mentioned, you may need to use memcpy() .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
_tcscpy_s and it's variants only guarantee that the string will be copied IF it fits. I suspect that the compiler "figured out" that it could safely optimize the copy using 32 or 64 bit chunks. This would result in satisfying the condition that the string be null terminated, but would also end up copying random data stored after the original string.
|
|
|
|
|
Hi,
I am new in MFC vc++ Please help me to come out from an major issue.
I want to move an object(Image,Rectangle) ..etc in Canvas buy using Arrow Key(left,right,up,down) in MFC VC++ application.only i get OnKeyDown()function I am not able to use this OnKeyDown()function to get keyboard event.
please provide a proper code
|
|
|
|
|
|
Hi Chris Losinger
Yes this is dialog-based application but arrow key functionality i need to apply in canvas/editor not in dialog.
Thanks
|
|
|
|
|
I am learning how to use ellipsis in function.
Started with putting "int" as parameters, with first entry being count of parameters passed to he function.
Now I want to pass multiple types , not just int , to the function.
The following snippet will do, but...
How do you get "type" from passing a char * - in this case "format" variable?
What did I missed ?
Thanks for your help.
Vaclav
I am not sure if I tagged the code properly.
<pre lang="c++">
void my_printf( char *format, ... ) {
va_list argptr;
va_start( argptr, format );
while( *format != '\0' ) {
// string
if( *format == 's' ) {
char* s = va_arg( argptr, char * );
printf( "Printing a string: %s\n", s );
}
// character
else if( *format == 'c' ) {
char c = (char) va_arg( argptr, int );
printf( "Printing a character: %c\n", c );
break;
}
// integer
else if( *format == 'd' ) {
int d = va_arg( argptr, int );
printf( "Printing an integer: %d\n", d );
}
*format++;
}
va_end( argptr );
} </pre>
|
|
|
|
|
That code looks like it should broadly work, but you are assuming any 's', 'c', or 'd' in the string is a format identifier. Your main loop should iterate the characters of the format string, copying them to the output until you see a format specifier. When you see a '%' character then you know that is the start of a formatting sequence so you need to get the width, precision and type details. Once you have extracted all those values, then you can process the next argument according to the type field character.
|
|
|
|
|
My feeling exactly.
I this whoever posted the code missed something.
Here is their sample use of the function.
int main( void ) {
my_printf( "sdc", "This is a string", 29, 'X' );
return( 0 );
}
This code displays the following output when run:
Printing a string: This is a string
Printing an integer: 29
Printing a character: X
Now where is the "sdc" print?
And I thought that "format" was some kind of special feature.
I guess I ditch this, maybe I can figure out how to use templates.
Thanks
|
|
|
|
|
|
As per what Richard said READ THE MANUAL.
"sdc" is the format specifier for the three parameters that follow
s = STRING
d = Signed decimal integer
c = Character
"This is a string" they want displayed as a string
29 they want displayed as a decimal
X they want displayed as a character
Try changing changing "sdc" to "s%.3fc" and watch the output
%.3f is a complex format it means write value as a float to 3 decimal places.
So we still have 3 instructions s + %.3f + c
So the first string is a FORMAT STRING it tells the print function what to do with the following data passed in.
In vino veritas
|
|
|
|
|
Thanks for reply, figured it out myself, but did not have time to reply.
As far as your first sentence goes, take a refresher course and read "how to answer the question".
You reply was great, but you do not have to be so snippy about it.
GOT MY POINT?
Have a swell day.
|
|
|
|
|
i use ncrack and in installed all this(visual studio 2005,2008,2010,2013) so ncrack working with visual c++. im wondering how can i create more threads?i foudn this but i don't know how to apply!I must open visual c++?create project?
<a href=""><a href="http://msdn.microsoft.com/en-us/library/ms682516(VS.85).aspx">Creating Threads (Windows)</a>[<a href="http://msdn.microsoft.com/en-us/library/ms682516(VS.85).aspx" target="_blank" title="New Window">^</a>]</a>
|
|
|
|
|
Member 11189357 wrote: must open visual c++?create project? Yes, because threads can only exist within an executable program. However, your question or problem is far from clear.
|
|
|
|
|
Try this:
#include "stdafx.h"
#define _MT
#include <windows.h>
#include <process.h> /* _beginthread, _endthread */
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "libcmt.lib")
#pragma comment(linker, "/NODEFAULTLIB:libcd.lib")
void ThreadFunction1(void *ch);
void ThreadFunction2(void *dummy);
BOOL repeat = TRUE;
int main(){
printf("\nMultithreading with two threads.\n\n");
printf("Thread Function 2 listens for key strokes. \n");
printf("Thread Function 1 does all the other work \n");
printf(" \n");
CHAR ch = 'A';
_beginthread(ThreadFunction2, 0, NULL);
while (repeat){
_beginthread(ThreadFunction1, 0, (void *)(ch++));
Sleep(1000L);
}
printf(" \n");
return 0;
}
void ThreadFunction2(void *dummy){
_getch();
repeat = 0;
}
void ThreadFunction1(void *ch){
while (repeat){
Sleep(100L);
}
_endthread();
}
|
|
|
|
|
For windows right?so this source will work for ncrack force bruter right?I don't know how to sue this code?must open visual c++ ?
-- modified 24-Sep-16 9:39am.
|
|
|
|
|
|
yes i use 20-30 threads if i use more i receive errors,nsock timeouts! nsock errors! etc...so i need to put that code somewhere but i don't know how?
|
|
|
|
|
Member 11189357 wrote: i need to put that code somewhere but i don't know how? And you think maybe we can guess? We have no idea what your code is supposed to be doing, or what you are trying to achieve with these threads.
|
|
|
|