Damn strings and pointers! I just can't get it right.
Suppose this:
MessageBox(NULL, "You have made this error x times", "Error", MB_OK);
now I want to replace x with some counter:
LPTSTR ModString(LPTSTR line)
{
counter ++;
// chage line so x is replaced by counter
return line;
}
and then I go:
MessageBox(NULL, ModString("You have made this error x times"), "Error", MB_OK);
..and it doesn't work. It does work when I do this:
LPTSTR ModString(LPTSTR line)
{
counter ++;
char *temp = new char(strlen(line));
// strcpy line into temp
// chage temp so x is replaced by counter
return temp;
}
But when I loop this function I run out of memory in no time, which makes sense because 'new' is never deleted.
What should my ModString function look like for this to work?
-Jay
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hmmm... I don't have a link on me... but there is an excelent post by tkorrovi that does something very similar... i would highly suggest you search the forum for his name plus winmain and/example...
Zero Valintine
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
as I see, you just need the function in the messagebox.
LPTSTR is, as I remember dependent of the definiton _UNICODE it's characterstring either char or wchar..
if you work with char you never work with wide character anyway, so you could use:
static string outfield /* so you reuse your string */
return outfield.c_str();
I'm even not shure, whether the string must be static, as you just use it, after return.
Patrick
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
MessageBox(NULL, ModString("You have made this error %d times"), "Error", MB_OK);
LPTSTR ModString(char line[])
{
char temp[] = "You have made this error %d times";
sprintf(line, line, counter++);
return line;
}
Maybe somethiing like this would work better for you? I am unfamilier with the LPTSTR type, so the return maybe buggy. This can be fixed though I'm sure.
Curtis
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks Curtis. At least I now know how that sprintf thing works. But, as you suspected, the return fails. It compiles ok, but when I run I get a memory allocation error.
Zero Valentine, I have only found one tkorrovi post with the search criteria you mentioned and I can't see anything relevant there... thanks anyway.
-Jay
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Damn strings and pointers! I just can't get it right.
Suppose this:
MessageBox(NULL, "You have made this error x times", "Error", MB_OK);
now I want to replace x with some counter:
LPTSTR ModString(LPTSTR line)
{
counter ++;
// chage line so x is replaced by counter
return line;
}
and then I go:
MessageBox(NULL, ModString("You have made this error x times"), "Error", MB_OK);
..and it doesn't work. It does work when I do this:
LPTSTR ModString(LPTSTR line)
{
counter ++;
char *temp = new char(strlen(line));
// strcpy line into temp
// chage temp so x is replaced by counter
return temp;
}
But when I loop this function I run out of memory in no time, which makes sense because 'new' is never deleted.
What should my ModString function look like for this to work?
-Jay
hmmm... I don't have a link on me... but there is an excelent post by tkorrovi that does something very similar... i would highly suggest you search the forum for his name plus winmain and/example...
Zero Valintine
This may be a stupid question since I don't do alot pure win api programming, but wouldn't wsprintf() fullfill your needs? Just a guess...
as I see, you just need the function in the messagebox.
LPTSTR is, as I remember dependent of the definiton _UNICODE it's characterstring either char or wchar..
if you work with char you never work with wide character anyway, so you could use:
static string outfield /* so you reuse your string */
return outfield.c_str();
I'm even not shure, whether the string must be static, as you just use it, after return.
Patrick
MessageBox(NULL, ModString("You have made this error %d times"), "Error", MB_OK);
LPTSTR ModString(char line[])
{
char temp[] = "You have made this error %d times";
sprintf(line, line, counter++);
return line;
}
Maybe somethiing like this would work better for you? I am unfamilier with the LPTSTR type, so the return maybe buggy. This can be fixed though I'm sure.
Curtis
Patrick you are right. This is what I did:
LPSTR ModString(string line)
{
counter ++;
// chage line so x is replaced by counter
LPSTR abc = (LPSTR)line.c_str();
return abc;
}
this does not even depend on char or whar, because it is possible to do something like this:
typedef std::basic_string<TCHAR> tString;
and then use tString where it says string.
My problem is now: WHY does this work (and the previous not)?
-Jay
Thanks Curtis. At least I now know how that sprintf thing works. But, as you suspected, the return fails. It compiles ok, but when I run I get a memory allocation error.
Zero Valentine, I have only found one tkorrovi post with the search criteria you mentioned and I can't see anything relevant there... thanks anyway.
-Jay
Curtis,
on second thought.. what's that char temp[] for?
1. It isn't used and 2. it is identical to line
-Jay
I was in a hurry, disregard that line. Sorry, I should have dbl checked my post.
Curtis