Memory writing issues

Ok, here's what I've got:

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
40
41
42
43
#include <windows.h>
#include <iostream>
using namespace std;

int main(){

  HANDLE hProcess = 0;
  HWND hWindow;
  DWORD pid = 0;

  //get window handle
  hWindow = FindWindow(NULL, TEXT("[Conquer2.0]"));

  //test for window handle
  if (hWindow != 0)
	  cout << "Obtained handle: " << hWindow << endl;
  else
	  cout << "Failed to obtain handle!" << endl;

  //get process id
  if (hWindow){
     GetWindowThreadProcessId(hWindow, &pid);
  }

  //test for process id
  if (pid != 0)
	  cout << "Obtained process id: " << pid << endl;
  else
	  cout << "Failed to obtain pid!" << endl;

  hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);

  if(hProcess != NULL)
     cout << "Process Found!";
  else {
     cout << "Process Not Found!";
     return 0;
  }
 
  WriteProcessMemory(hProcess, (void*)0x30FF3AC, (void*)600, sizeof(valueToWrite), NULL);

  return 0;
}


And it gives this output:
Obtained handle: 001b0182
Obtained process id: 2532
Process not found!
Press any key to continue...


I can't figure out what's wrong! Can anyone help me? =[
Wrong forum, but anyway.
It's not giving you PROCESS_ALL_ACCESS, that's all. Use PROCESS_VM_WRITE instead.
By the way, (void*)0x30FF3AC will most likely not work (if it did work, it'd be simply by chance).
And (void*)600 will definitely not work.
And valueToWrite is not declared.
Last edited on
Yeah I realized that last thing afterwords.
Keep in mind I just took a skeleton code, worked out some bugs with the FindWindow function, added the tests, and tried it. I'm not very good with the Windows API. Here's the original code, followed by mine with the declaration included.

ORIGINAL
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
#include <windows.h>
#include <stdio.h>

int main(){

  HANDLE hProcess = 0;

  HWND hWindow;
  DWORD pid = 0;

  hWindow = FindWindow(NULL, "Window Name");
  if (hWindow){
     GetWindowThreadProcessId(hWindow, &pid);
  }

  hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);

  if(hProcess != NULL)
     printf("Process Found!");
  else {
     printf("Process Not Found!");
     return 0;
  }
 
  //Writes byte values to 0x00567...
  BYTE valueToWrite[] ={0x90, 0x90};
  WriteProcessMemory(hProcess, (void*)0x00567A8F, (void*)&valueToWrite, sizeof(valueToWrite), NULL);

  return 0;
}


MINE (changes since original post)
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
40
41
42
43
44
#include <windows.h>
#include <iostream>
using namespace std;

int main(){

  HANDLE hProcess = 0;
  HWND hWindow;
  DWORD pid = 0;

  //get window handle
  hWindow = FindWindow(NULL, TEXT("[Conquer2.0]"));

  //test for window handle
  if (hWindow != 0)
	  cout << "Obtained handle: " << hWindow << endl;
  else
	  cout << "Failed to obtain handle!" << endl;

  //get process id
  if (hWindow){
     GetWindowThreadProcessId(hWindow, &pid);
  }

  //test for process id
  if (pid != 0)
	  cout << "Obtained process id: " << pid << endl;
  else
	  cout << "Failed to obtain pid!" << endl;

  hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);

  if(hProcess != NULL)
     cout << "Process Found!";
  else {
     cout << "Process Not Found!";
     return 0;
  }
 
  BYTE valueToWrite[] ={0x90, 0x90};
  WriteProcessMemory(hProcess, (void*)0x30FF3AC, (void*)valueToWrite, sizeof(valueToWrite), NULL);

  return 0;
}


What I'm trying to do is write the value 600 to memory location 0x30FF3AC.

So anyone got some ideas? (After about an hour or so of no replies I'll move the topic to the Windows Programming category.)
You maybe mean (void*)&valueToWrite and if you want to write he value 600 to 0x30FF3AC you can't use {0x90, 0x90} ({144, 144}). 600 would be 0x58, 0x02
If valueToWrite is declared as BYTE valueToWrite[] he can't use (void*)&valueToWrite (that would be a pointer to the pointer to the array).
However, it'd be easier to change the declaration to short valueToWrite=600.
Topic archived. No new replies allowed.