Alter a basic string in memory


Hey,

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.

So I have the following struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct _StdString
{
	char data[0x10];
	int size;
	int	cap;

	char* c_str()
	{
		if (size > 0xF)
		{
			return (char*) * (HMODULE*)data;
		}

		return data;
	}

	void copy(const char* str)
	{
		//size = strlen(str); //Results in crash
		if (size < 0x0F)
		{
			strcpy(data, str);
			cap = 0x0F;
		}
		else
		{
			*(HMODULE * *)data = (HMODULE*)str;
			//data[strlen(str)] = '\0'; //Custom end does not work
		}
	}
};



And call it this way to place my own string:
1
2
3
char mystr[] = "myreplacementstring";
_StdString* oneee = (_StdString*)argument;
oneee->copy(mystr);


Last edited on
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.
Last edited on
Thanks Jonnin

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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!
}

Last edited on
Line 20 in your original post should be
if (strlen(str) < 0xf)

In other words, what matters is the size of what you're copying from, not what you're copying to.

Also copy() needs to update size.
Topic archived. No new replies allowed.