Hope I can get some help with this basic string struct.
I'm hooking an unknown function which has some sort of basic string as a parameter. This parameter needs to be changed before caling the real function.
The issue is that when I change the string, the length of the string remains as the previous string which was originally passed as an argument.
strlen just counts until it finds the zero.
strcpy will manage the zero for you but the target needs to be big enough to hold the input.
if you want to copy the first N chars of a c-string into a target that is smaller, some sort of truncation, use memcpy and set the last byte of the target to zero.
something like
if strlen (input) < size of target, use strcpy
else
memcpy...
but maybe I am misreading your intent here?
//size = strlen(str); //Results in crash
that should work. what is the problem? did you try doing nothing but cout strlen there?
that else seems risky, it assumes a lot about str... it may be fine in your code, but its not going to be generally safe.
Basically this line is the only one I use, which just makes data point to my pointer:
*(HMODULE * *)data = (HMODULE*)str;
The issue is that the length remains the same. So if the application would write the string into a text file, it will not write the length of "myreplacementstring" but instead it writes "myreplacementstring ". Notice the whitespaces in the end because the string that was replaced was longer in length.
-I tried manually placing a '/0' which you can see. That does not work, it still prints multiple extra spaces because the old string's length was more than the one I placed.
ok, I don't know what a hmodule is, so I am not sure I can help here without more info:
what is it, and why are you using it for this, and what *exactly* do you want to have happen once copy is done?
*(HMODULE * *)data = (HMODULE*)str;
that just looks screwy. It may be right, but it looks like you are either trying to modify data's pointer (bad, bad news) or casting it every which way to enable some kind of copy that could be done with strcpy or memcpy, which seems weird (why not use a std::string and just pull the c-string out at the very end?!).
here is pretty much everything you need in c-string lingo, though:
int main()
{
//you can put conditions around what you do to check if you are trying to put something
// that is too big into a smaller memory block, in which case, you want the last example.
char s[16] = "abcd";
strcpy(s, "abcd_"); //add a underscore
strcat(s,"+"); //add one letter, a + sign
cout << s << endl;
strcpy(s, "ab"); //shorter than it was
cout << s << endl;
char s2[] ="0123456789abcdef10000x"; //too big.
memcpy(s,s2,15);
s[15] = 0;
cout << s << endl;
// or for the memory switcheroo, but be careful, you can't add a space to it directly:
char * sp = &s[0]; //I think this is ok as just s, actually.
cout << sp << endl;
sp = &s2[0];
cout << sp << " " << endl; //you can add a space to output, but not to the string, there isnt any place for it to go!
}