Solved

My program works only on 64bit systems .. Why.

Posted on 2009-06-27
8
267 Views
Last Modified: 2012-05-07
Hi, I have c++ console application, but it works only on 64bit systems. Please help me to find solution for this problem.
#include <windows.h>
#include <Tlhelp32.h>
#include <iostream>
#include <stdio.h>
#include <psapi.h>
#include <tchar.h>
 
using namespace std;
 
DWORD GetPIDForProcess (char* process);
void EnableDebugPriv();
DWORD GetDLLBase(char* DllName, DWORD tPid);
 
#define PATCH(i,w,l) WriteProcessMemory(hProc,reinterpret_cast<LPVOID>(gameBase+i),w,l,&dSize)
#define NPATCH(i,w,l) WriteProcessMemory(hProc,reinterpret_cast<LPVOID>(i),w,l,&dSize)
 
BOOL IsAllowedToStart()
{
   if (strstr(GetCommandLine(),"-launch")) return TRUE;
 
  return FALSE;
}
 
int main()
{ 
	system("COLOR 0");
	system("TITLE AnGrY's GameTools 2009 (Version 6.55) Released on 26.06.09");
    cout << "Welcome to E3MC Clan GameTools 2009" << endl << endl;
		  if(IsAllowedToStart())
  {
    printf("Access Granted\n");
  }
  else
  {
    printf("You are not allowed to start this program.\n");
 
    return -1;
  } 
    if(GetPIDForProcess("WAR3.EXE") == 0)
    {
        cout << "Warcraft 3 is not running!" << endl;
        system("Pause");
        exit(0);
    }
    else
    {
        EnableDebugPriv();
        HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, GetPIDForProcess("WAR3.EXE"));
        if(hProc)
        {
            cout << "Process opened... Patching" << endl;
            DWORD gameBase = GetDLLBase("Game.dll",GetPIDForProcess("WAR3.EXE"));
			DWORD dSize = 0;
 
            PATCH(0x3A1E9B,"\x90\x90",2);
            if(dSize == 0)
            {
                cout << "Failed to patch showunitsingame" << endl;
            }
 
            PATCH(0x361DFC,"\x00",1);
            if(dSize == 0)
            {
                cout << "Failed to patch showunitsmap" << endl;
            }
 
            PATCH(0x285B8C,"\x90\x90",2);  
            if(dSize == 0)
            {
                cout << "Failed to patch clickableunits" << endl;
            }
 
            PATCH(0x285BA2,"\xEB\x29",2);  
            if(dSize == 0)
            {
                cout << "Failed to patch clickableunits (2nd patch)" << endl;
            }
 
            PATCH(0x28345C,"\x40\xc3",2);
            if(dSize == 0)
            {
                cout << "Failed to patch revealillu" << endl;
            }
 
            PATCH(0x73DEC9,"\xB2\x00\x90\x90\x90\x90",6);
            if(dSize == 0)
            {
                cout << "Failed to patch removefogingame" << endl;
            }
 
            PATCH(0x431556,"\x3B\xC0\x0F\x85\xC0\x00\x00\x00\x8D\x8B\xF0\x00\x00\x00\xE8\x07\x3D\x03\x00\x3B\xC0\x0F\x85\xAD\x00\x00\x00",27);
            if(dSize == 0)
            {
                cout << "Failed to patch pingsignal" << endl;
            }
 
            PATCH(0x362211,"\x3B\xC0\x0F\x85\x30\x04\x00\x00",8);
            if(dSize == 0)
            {
                cout << "Failed to patch showinvisiblemap" << endl;
            }
 
            PATCH(0x356E7E,"\x90\x90\x90",3);
            if(dSize == 0)
            {
                cout << "Failed to patch showinvisiblemap" << endl;
            }
 
            PATCH(0x3C5C22,"\xEB",1);
            PATCH(0x3C135C,"\xB8\xFF\x00\x00\x00\xEB",6);
            if(dSize == 0)
            {
                cout << "Failed to bypass DotA -ah command" << endl;
            }
 
            PATCH(0x3F92CA,"\x90\x90",2);
            PATCH(0x3A1DDB,"\xEB",1);
            if(dSize == 0)
            {
                cout << "Failed to enable viewable items" << endl;
            }
 
            NPATCH(0x4559EC,"\x60\x0B\xC0\x75\x2F\x8A\x8E\xD0\x02\x00\x00\x80\xF9\x01\x74\x51\xC6\x86\xD0\x02\x00\x00\x01\xC6\x86\xD1\x02\x00\x00\x01\xC6\x86\xD2\x02\x00\x00\xFF\x8B\xCE\xB8\xFF\x01\x01\xFF\xFF\x15\x60\x5A\x45\x00\xEB\x2D\x8A\x8E\xD0\x02\x00\x00\x80\xF9\xFF\x74\x22\xC6\x86\xD0\x02\x00\x00\xFF\xC6\x86\xD1\x02\x00\x00\xFF\xC6\x86\xD2\x02\x00\x00\xFF\x8B\xCE\xB8\xFF\xFF\xFF\xFF\xFF\x15\x60\x5A\x45\x00\x61\xB8\x01\x00\x00\x00\x23\xD8\x89\x44\x24\xE4\xFF\x35\x65\x5A\x45\x00\xC3",116);
            DWORD addr[3];
            addr[0] = gameBase + 0x29E270; 
            addr[1] = gameBase + 0x39A3BF; 
            addr[2] = 0x4559EC;            
            NPATCH(0x455A60,&addr[0],sizeof(DWORD));
            NPATCH(0x455A65,&addr[1],sizeof(DWORD));
            NPATCH(0x455A6A,&addr[2],sizeof(DWORD));
            PATCH(0x39A3B9,"\xFF\x25\x6A\x5A\x45\x00",6); 
            if(dSize == 0)
            {
                cout << "Failed to enable colored invisibles" << endl;
            }
 
 
            PATCH(0x2030DC,"\x90\x90\x90\x90\x90\x90",6);
            PATCH(0x34FC68,"\x90\x90",2);
            if(dSize == 0)
            {
                cout << "Failed to enable view skills" << endl;
            }
 
            PATCH(0x28EBCE,"\xEB",1);
            PATCH(0x34FCA6,"\x90\x90\x90\x90",4);
            if(dSize == 0)
            {
                cout << "Failed to enable view cooldowns" << endl;
            }
 
            PATCH(0x285BA2,"\xEB",1);
            if(dSize == 0)
            {
                cout << "Failed to enable click invisible units" << endl;
            }
        }
        else
        {
            cout << "Warcraft 3 could not be opened..." << endl;
            system("Pause");
            exit(0);
        }
    }
	return 0;
}
 
//Queries the ProcessId of a certain process
DWORD GetPIDForProcess (char* process)
{
    BOOL            working=0;
    PROCESSENTRY32 lppe= {0};
    DWORD            targetPid=0;
 
    HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS ,0);
    if (hSnapshot) 
    {
        lppe.dwSize=sizeof(lppe);
        working=Process32First(hSnapshot,&lppe);
        while (working)
        {
            if(_stricmp(lppe.szExeFile,process)==0)
            {
                targetPid=lppe.th32ProcessID;
                break;
            }
            working=Process32Next(hSnapshot,&lppe);
        }
    }
 
    CloseHandle( hSnapshot );
    return targetPid;
}
 
//Enables to open other processes
void EnableDebugPriv()
{
    HANDLE hToken;
    LUID sedebugnameValue;
    TOKEN_PRIVILEGES tkp;
 
    if ( ! OpenProcessToken( GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) )
        return;
    if ( ! LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) ){
        CloseHandle( hToken );
        return;
    }
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Luid = sedebugnameValue;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    if ( ! AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof tkp, NULL, NULL ) )
        CloseHandle( hToken );
} 
 
//Gets the base of our dll
DWORD GetDLLBase(char* DllName, DWORD tPid)
{
    HANDLE snapMod;  
    MODULEENTRY32 me32;
 
    if (tPid == 0) return 0;
    snapMod = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, tPid);  
    me32.dwSize = sizeof(MODULEENTRY32);  
    if (Module32First(snapMod, &me32)){ 
        do{
            if (strcmp(DllName,me32.szModule) == 0){ 
                CloseHandle(snapMod); 
                return (DWORD) me32.modBaseAddr; 
            }
        }while(Module32Next(snapMod,&me32));
    }
 
    CloseHandle(snapMod); 
    return 0;  
 
}

Open in new window

0
Comment
Question by:GameOverlayUI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 8

Expert Comment

by:JIEXA
ID: 24727397
Make sure you compile it for 32-bit.
What is the output of the program on 32-bit system if compiled 32-bit?
0
 

Author Comment

by:GameOverlayUI
ID: 24727412
It works on both ( I mean I can launch it ) but it patches offsets only in 64bit version of windows.
0
 
LVL 8

Expert Comment

by:JIEXA
ID: 24727453
What is exact failure? Do you check the results of WriteProcessMemory?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:GameOverlayUI
ID: 24727464
no
0
 

Author Comment

by:GameOverlayUI
ID: 24727466
well, I can see result... it is not patching memory as it should be on 32bit systems.
0
 
LVL 8

Accepted Solution

by:
JIEXA earned 500 total points
ID: 24727494
You need to find out whether the offsets are wrong, or just WriteProcessMemory() calls fail.
#define PATCH(i,w,l) do { if (!WriteProcessMemory(hProc,reinterpret_cast<LPVOID>(gameBase+i),w,l,&dSize)) printf("PATCH: %d\n",GetLastError());} while(0)
#define NPATCH(i,w,l) do { if (!WriteProcessMemory(hProc,reinterpret_cast<LPVOID>(i),w,l,&dSize)) printf("NPATCH: %d\n",GetLastError());} while(0)

Open in new window

0
 
LVL 8

Expert Comment

by:JIEXA
ID: 24727508
Explain me:
1. do you have both 32-bit and 64-bit OS?
2. what is the bitness of WAR3.EXE on all systems?
3. what is the bitness of your patching application on all systems?
4. are the offsets known to be good for both 32-bit and 64-bit WAR3.exe?
0
 
LVL 8

Expert Comment

by:JIEXA
ID: 24727522
Also, what is the output on working and not working systems.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question