Source:Warcraft3GameState/gamestate.cpp

From Codemotion
Jump to: navigation, search

Source:Warcraft3GameState/gamestate.cpp

Description

Source.

Code

Parent Directory: Source:Warcraft3GameState
Plain Code: edit

  1. #include <windows.h>
  2. #include <iostream>
  3. #include <Tlhelp32.h>
  4.  
  5. using namespace std;
  6.  
  7. void EnableDebugPriv();
  8. DWORD GetPID (char* proc);
  9. DWORD GetDLL (char* DllName, DWORD tPid);
  10. void ClearConsole();
  11.  
  12. int main()
  13. {
  14. SetConsoleTitle("Gamestate");
  15. EnableDebugPriv();
  16. TCHAR War3Name[32] = TEXT("Warcraft III");
  17. HWND hWar3 = FindWindow(War3Name, NULL);
  18.  
  19. if(!hWar3)
  20. {
  21. cout << "Please open Warcraft III first." << endl;
  22. system( "pause" );
  23. return 1;
  24. }
  25.  
  26. DWORD pid;
  27. GetWindowThreadProcessId( hWar3, &pid );
  28. HANDLE hOpen = OpenProcess( PROCESS_ALL_ACCESS, false, pid );
  29. if( !hOpen )
  30. {
  31. cout << "Can't open Warcraft III." << endl;
  32. system( "pause" );
  33. return 1;
  34. }
  35. // Address in decimal.
  36. DWORD Address = 11147656;
  37. DWORD Buffer;
  38. DWORD WINAPI GetLastError(void);
  39. DWORD GameDLL = GetDLL("Game.dll",GetPID("war3.exe"));
  40. SIZE_T BytesRead = 0;
  41.  
  42. for (;;)
  43.  
  44. {
  45.  
  46. // Notice the GameDLL+Address: we are adding our address to the base, AA1988h, because while the base is 6F 95 % of the time, it may change for specific reasons.
  47. ReadProcessMemory(hOpen, (LPCVOID)(GameDLL+Address), &Buffer, 4, &BytesRead);
  48.  
  49. // If our address has value(buffer) 0, then we are not in-game:
  50. if(Buffer == 0)
  51. {
  52. cout << "Not in game" <<endl;;
  53. Sleep(2000);
  54. ClearConsole();
  55. }
  56.  
  57. else
  58. {
  59. cout << "In game" <<endl;
  60. Sleep(1000);
  61. ClearConsole();
  62. }
  63. }
  64. }
  65.  
  66. // Priviledges
  67.  
  68. void EnableDebugPriv( )
  69. {HANDLE hToken;
  70. LUID sedebugnameValue;
  71. TOKEN_PRIVILEGES tkp;
  72. OpenProcessToken( GetCurrentProcess( ), TOKEN_ADJUST_PRIVILEGES |TOKEN_QUERY, &hToken );
  73. LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue );
  74. tkp.PrivilegeCount = 1;tkp.Privileges[0].Luid = sedebugnameValue;
  75. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  76. AdjustTokenPrivileges( hToken, false, &tkp, sizeof( tkp ), NULL, NULL );
  77. CloseHandle( hToken );
  78. }
  79.  
  80. // Get PID for process.
  81. DWORD GetPID (char* proc)
  82. {
  83. BOOL working=0;
  84. PROCESSENTRY32 lppe= {0};
  85. DWORD targetPid=0;
  86. HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROC ESS ,0);
  87.  
  88. if (hSnapshot)
  89. {
  90. lppe.dwSize=sizeof(lppe);
  91. working=Process32First(hSnapshot,&lppe);
  92. while (working)
  93. {
  94. if (_stricmp(lppe.szExeFile,proc)==0)
  95. {
  96. targetPid=lppe.th32ProcessID;
  97. break;
  98. }
  99. working=Process32Next(hSnapshot,&lppe);
  100. }
  101. }
  102.  
  103. CloseHandle( hSnapshot );
  104. return targetPid;
  105. }
  106.  
  107. //Base (6F).
  108. DWORD GetDLL(char* DllName, DWORD tPid)
  109. {
  110. HANDLE snapMod;
  111. MODULEENTRY32 me32;
  112.  
  113. if (tPid == 0) return 0;
  114. snapMod = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, tPid);
  115. me32.dwSize = sizeof(MODULEENTRY32);
  116.  
  117. if (Module32First(snapMod, &me32)){
  118. do{
  119. if (strcmp(DllName,me32.szModule) == 0){
  120. CloseHandle(snapMod);
  121. return (DWORD) me32.modBaseAddr;
  122. }
  123. }while(Module32Next(snapMod,&me32));
  124. }
  125.  
  126. CloseHandle(snapMod);
  127. return 0;
  128.  
  129. }
  130.  
  131. // Function to clear console.
  132. void ClearConsole()
  133. {
  134. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  135. COORD coord = {0, 0};
  136. DWORD count;
  137. CONSOLE_SCREEN_BUFFER_INFO csbi;
  138. if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  139. {
  140. FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  141. FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
  142. SetConsoleCursorPosition(hStdOut, coord);
  143. }
  144. return;
  145. }
Personal tools