convert a std::string to const char*

i use this code working good
1
2
3
4
5
6
7
8
9
10


				const char* SPattern{ "\x55\x00\x00\x00\x00\x00\x00\x00\x57" };
				const char* SMask{ "x???????x"};

				char* getaddress = memory.ScanInternal(SPattern, SMask, (char*)(moduleBase), 0x2000);
				LPVOID A_address = reinterpret_cast<LPVOID>(getaddress);
				std::cout << A_address << std::endl; 
				



But when I use the code like this the result is zero

1
2
3
4
5
6
7
8
9
10
				string patt = "\x55\x00\x00\x00\x00\x00\x00\x00\x57";
				string mask = "x???????x";
				const char* SPattern = patt.c_str() ;
				const char* SMask = mask.c_str() ;

				char* getaddress = memory.ScanInternal(SPattern, SMask, (char*)(moduleBase), 0x2000);
				LPVOID A_address = reinterpret_cast<LPVOID>(getaddress);
				std::cout << A_address << std::endl; 
				
Last edited on
Your mask is different in your first example vs second. Is that significant? I am assuming the second instance is just a typo.

Your "pattern" is 9 characters long.
Your "mask" is also 9 characters long.

The key difference in memory is that SPattern[8] in your first example points to valid memory, while SPattern[8] in your second example points to undefined memory.

In other words, (patt.length() == 1) because it stops constructing the std::string once it encounters a null character. Null characters can be in std::strings, but you need to use the right constructor.
1
2
const char* SPattern{ "\x55\x00\x00\x00\x00\x00\x00\x00\x57" };
std::string patt(SPattern, 9); 


What I'm guessing is happening is that ScanInternal is using the mask as the length delimiter, and ScanInternal goes out of bounds or errors out due to length of the mask being greater than length of the pattern.

_______________

I would recommend not using strings for something like this. Use arrays of bytes (unsigned chars). Not sure if that's something you have control over.
Last edited on
I am assuming the second instance is just a typo
Yes, it was a typo and I fixed it now.
i use string because , i read lines from txt like this

file name pattern.txt
1
2
 55 ?? ?? ?? ?? ?? ?? ?? 57
x???????x

i use this function

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
32
33
34
35
36
37
38
void ReplaceStringInPlace(std::string& subject, const std::string& search,
	const std::string& replace) {
	size_t pos = 0;
	while ((pos = subject.find(search, pos)) != std::string::npos) {
		subject.replace(pos, search.length(), replace);
		pos += replace.length();
	}
}

		
			string myText;
			string pattern;
			string mask;

			
			ifstream MyReadFile("pattern.txt");
			int lins = 0;
			while (getline(MyReadFile, myText)) {

				if (lins == 0) {
					ReplaceStringInPlace(myText, "??", "00");
					ReplaceStringInPlace(myText, " ", "\\x9");
					ReplaceStringInPlace(myText, "9", "");
					pattern = myText;
				}if (lins == 1) {

				
					mask = myText;

				}

				lins++;


			}
			
				std::cout << pattern << std::endl; // Output:	\x55\x00\x00\x00\x00\x00\x00\x00\x57
				std::cout << mask << std::endl;    // Output:   x???????x 


all i need convert a string to const char*
i use this code
1
2
3
4
5
6
				const char* SPattern = pattern.c_str() ;
				const char* SMask = mask.c_str() ;

				char* getaddress = memory.ScanInternal(SPattern, SMask, (char*)(moduleBase), 0x2000);
				LPVOID A_address = reinterpret_cast<LPVOID>(getaddress);
				std::cout << A_address << std::endl; 

Output 00000000
Your "real" code is significantly different than the code in your first post. I think I see at least one issue, though.

"\x55\x00\x00\x00\x00\x00\x00\x00\x57" as a string literal in the source code itself is very different than "\x55\x00\x00\x00\x00\x00\x00\x00\x57" being the output printed to the screen.
\x55 is an escape character sequence that is converted into a 1-byte character; it is not 4 separate characters (\, x, 5, 5).

See this example program:
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
32
33
34
35
36
37
38
39
#include <string>
#include <iostream>
#include <iomanip>

void print_pattern(const char* pattern)
{
    std::cout << "pattern: \"" << pattern << "\"\n";
}

void print_bytes(const char* str, int size)
{
    std::cout << "bytes: ";
    for (int i = 0; i < size; i++)
    {
        std::cout << std::hex << "0x" << (int)str[i];
        if (i != size-1)
        {
            std::cout << ", ";
        }
    }
    std::cout << '\n';
}

int main()
{
    // Example 1
    {
        const char pattern[] = "\x55\x00\x00\x00\x00\x00\x00\x00\x57";
        print_pattern(pattern);
        print_bytes(pattern, sizeof(pattern));
    }
    
    // Example 2
    {
        const char pattern[] = "\\x55\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x57";
        print_pattern(pattern);
        print_bytes(pattern, sizeof(pattern));
    }
}


output:
pattern: "U"
bytes: 0x55, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x57, 0x0
pattern: "\x55\x00\x00\x00\x00\x00\x00\x00\x57"
bytes: 0x5c, 0x78, 0x35, 0x35, 0x5c, 0x78, 0x30, 0x30, 0x5c, 0x78, 0x30, 0x30, 0x5c, 0x78, 0x30, 0x30, 0x5c, 0x78, 0x30, 0x30, 0x5c, 0x78, 0x30, 0x30, 0x5c, 0x78, 0x30, 0x30, 0x5c, 0x78, 0x30, 0x30, 0x5c, 0x78, 0x35, 0x37, 0x0

(side note: sizeof("string literal") includes the implicit null terminator, but you normally don't do this)

The memory.ScanInternal call appears to want character bytes as its input, not the literal '\', 'x', '5', '5', etc. characters.
Last edited on
Ganado Thank you very much for the clarification and explanation. I understand, but I do not know what the solution to my problem.
I'm not working off complete information since I don't know what memory.ScanInternal looks like, so I could be wrong, but I would break your issue down into the following chunk:

Given an input of " 55 ?? ?? ?? ?? ?? ?? ?? 57"
form a length-9 string that converts each hexadecimal pair into a byte (character) of that string, where ?? is converted into a zero byte.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
#include <string>

std::string parse_pattern(const std::string& pattern) {
    return ""; // TODO
}

int main()
{
    const int ExampleLength = 9;
    std::string pattern = " 55 ?? ?? ?? ?? ?? ?? ?? 57";

    std::string pattern_bytes = parse_pattern(pattern);

    if (pattern_bytes.length() != ExampleLength)
    {
        std::cout << "Failure (pattern)\n";
    }
}
Last edited on
Registered users can post here. Sign in or register to post.