Public paste
Undefined
By: . _ . | Date: Jan 10 2009 16:21 | Format: C++ | Expires: never | Size: 4.24 KB | Hits: 1046

  1. // gdi_draw.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "class.h"
  6. #include <iostream>
  7. #include <string>
  8. #include <windows.h>
  9. #include "process.h"
  10. #include <cstdlib>
  11. #include <winbase.h>
  12.  
  13. // hat ich auch schonmal versucht..
  14. void draw(){
  15.                 HWND hWnd=FindWindow(NULL,"Counter-Strike");
  16.         HDC hdc=GetDC(hWnd);
  17.         RECT r;
  18.        
  19.         // Obtain the window's client rectangle
  20. GetClientRect(hWnd, &r);
  21.  
  22. // THE FIX: by setting the background mode
  23. // to transparent, the region is the text itself
  24. // SetBkMode(hdc, TRANSPARENT);
  25.  
  26. // Bracket begin a path
  27. BeginPath(hdc);
  28.  
  29. // Send some text out into the world
  30. TCHAR text[ ] = "Defenestration can be hazardous";
  31. TextOut(hdc,r.left,r.top,text, ARRAYSIZE(text));
  32.  
  33. // Bracket end a path
  34. EndPath(hdc);
  35.  
  36. // Derive a region from that path
  37. SelectClipPath(hdc, RGN_AND);
  38.  
  39. // This generates the same result as SelectClipPath()
  40. // SelectClipRgn(hdc, PathToRegion(hdc));
  41.  
  42. // Fill the region with grayness
  43.  
  44.  
  45. }
  46. using namespace std;
  47. //using namespace System::Threading;
  48.  
  49. BOOL TextOut(int x, int y,
  50.                      LPCTSTR lpszString,
  51.                      int nCount);
  52.  
  53. void InitMenu(void);
  54. int ReadMem(char *window, int dAddr);
  55. bool SetDebugPrivilege();
  56. bool SetPrivilege( HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege );
  57. //void DisplayError( LPTSTR szAPI );
  58. void runInThread();
  59.  
  60. int main(int argc, char *argv[]) {
  61. unsigned long threadId;
  62. HWND hWnd=FindWindow(NULL,"Counter-Strike");
  63. HDC hdc=GetDC(hWnd);
  64. cout<<"hWnd: "<<hWnd<<endl;
  65. cout<<"hcd: "<<hdc<<endl;
  66. /*
  67. CreateThread(
  68.     __in_opt  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  69.     __in      SIZE_T dwStackSize,
  70.     __in      LPTHREAD_START_ROUTINE lpStartAddress,
  71.     __in_opt  LPVOID lpParameter,
  72.     __in      DWORD dwCreationFlags,
  73.     __out_opt LPDWORD lpThreadId
  74.     );
  75.         */
  76. HANDLE hThread = CreateThread(
  77. NULL, 2000, (LPTHREAD_START_ROUTINE)runInThread,"", 0, &threadId
  78. );
  79. for(int i=0;i<30;++i) {
  80. Sleep(10);
  81. /*
  82. DrawTextA(
  83.     __in HDC hdc,
  84.     __inout_ecount_opt(cchText) LPCSTR lpchText,
  85.     __in int cchText,
  86.     __inout LPRECT lprc,
  87.     __in UINT format);
  88.         */
  89.  
  90. RECT r;
  91. DrawText(hdc,"text",4,&r,DT_CENTER);
  92. //InitMenu();
  93. //printf("main");
  94. }
  95.  
  96. return EXIT_SUCCESS;
  97. }
  98.  
  99. void runInThread() {
  100. for(int i=0;i<30;++i) {
  101.  
  102.  
  103.        
  104.        
  105.        
  106. }
  107. }
  108. bool SetDebugPrivilege()
  109. {
  110. HANDLE hToken;
  111. bool bOK = false;
  112.  
  113. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  114. {
  115. if (!SetPrivilege(hToken, SE_DEBUG_NAME, TRUE))
  116. {
  117. cout<<"setprivilege"<<endl;//DisplayError("SetPrivilege");
  118. }
  119. else
  120. bOK = true;
  121.  
  122. CloseHandle(hToken);
  123. }
  124. else
  125. //DisplayError("OpenProcessToken");
  126. cout<<"openprocesstoken"<<endl;
  127.  
  128. return bOK;
  129. }
  130.  
  131. bool SetPrivilege( HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege )
  132. {
  133. TOKEN_PRIVILEGES tp;
  134. LUID luid;
  135. TOKEN_PRIVILEGES tpPrevious;
  136. DWORD cbPrevious = sizeof(TOKEN_PRIVILEGES);
  137.  
  138. if(!LookupPrivilegeValue( NULL, lpszPrivilege, &luid ))
  139. return false;
  140.  
  141. // 1) Get current privilege setting
  142. tp.PrivilegeCount = 1;
  143. tp.Privileges[0].Luid = luid;
  144. tp.Privileges[0].Attributes = 0;
  145.  
  146. AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &tpPrevious, &cbPrevious);
  147. if (GetLastError() != ERROR_SUCCESS)
  148. return false;
  149.  
  150. // 2) Set privilege based on previous setting
  151. tpPrevious.PrivilegeCount = 1;
  152. tpPrevious.Privileges[0].Luid = luid;
  153. if (bEnablePrivilege)
  154. tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED);
  155. else
  156. tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes);
  157.  
  158. AdjustTokenPrivileges( hToken, FALSE, &tpPrevious, cbPrevious, NULL, NULL );
  159. if (GetLastError() != ERROR_SUCCESS)
  160. return false;
  161.  
  162. return true;
  163. }
  164.  
  165.  
  166.  
  167. int ReadMem(char *window, int dAddr)
  168. {
  169.         int buffer;
  170.         //DWORD prevProtection;
  171.         HWND hWnd = FindWindow(0, window);
  172.         if (!hWnd) MessageBox(0, "Error finding window", 0, MB_OK);
  173.         DWORD proc_id;
  174.         GetWindowThreadProcessId(hWnd, &proc_id);
  175.         HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proc_id);
  176.        
  177.         &prevProtection);
  178.    
  179.         if (!ReadProcessMemory(hProcess, (LPCVOID)dAddr, &buffer, 2, NULL))
  180.                 MessageBox(0, "Error doing memory reading", 0, MB_OK);
  181.        
  182.        
  183.         return buffer;
  184. }