Advertisement
Guest User

Functions.h

a guest
Jul 13th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include <iostream>
  4. #include <Windows.h>
  5. #include <windef.h>
  6. #include <fstream>
  7. #include <time.h>
  8. #include <ole2.h>
  9. #include <olectl.h>
  10. using namespace std;
  11.  
  12. void CreateTextFile(char Text[])
  13. {
  14.     char myPath[_MAX_PATH + 1];
  15.     GetModuleFileName(NULL, myPath, _MAX_PATH);
  16.  
  17.     char filename[] = "EXIT_FAILURE.txt";
  18.     ofstream myfile;
  19.     myfile.open("EXIT_FAILURE.txt", ofstream::out | ofstream::app);
  20.  
  21.     time_t now = time(0);
  22.     tm* localtm = localtime(&now);
  23.  
  24.     myfile << "-" << asctime(localtm) << Text;
  25.  
  26.     myfile.close();
  27.     myfile.clear();
  28.  
  29.     cout << "Creato file di debug all'indirizzo dell'exe...\n" << myPath << endl;
  30.  
  31.     return;
  32. }
  33.  
  34. HWND FindLolClient(CONST LPCSTR handlename, CONST LPCSTR childname, HWND &RCLIENT)
  35. {
  36.     HWND Leaguehwnd, Clienthwnd;
  37.     int MessageBox_Value;
  38.     CHAR TargetNameClass[50];
  39.  
  40.     do
  41.     {
  42.  
  43.         Leaguehwnd = FindWindow(handlename, NULL);
  44.         Clienthwnd = FindWindowEx(Leaguehwnd, NULL, childname, NULL);
  45.  
  46.         if (!Clienthwnd)
  47.         {
  48.             cout << "Finestra non trovata...\n";
  49.             MessageBox_Value = MessageBox(NULL, "Impossibile trovare l'handle di League Of Legends...", "Errore", MB_RETRYCANCEL);
  50.  
  51.             if (MessageBox_Value == IDCANCEL)
  52.             {
  53.                 cout << "EXIT_FAILURE\n";
  54.                 CreateTextFile("Impossibile trovare la finestra, errore funzione: FindLolClient...");
  55.                 return NULL;
  56.             }
  57.         }
  58.         else
  59.         {
  60.             Clienthwnd = GetWindow(Clienthwnd, GW_CHILD);
  61.             Clienthwnd = GetWindow(Clienthwnd, GW_CHILD);
  62.             GetClassNameA(Clienthwnd, TargetNameClass, 49);
  63.             cout << "Nome della classe trovata: " << TargetNameClass << endl;
  64.             RCLIENT = Leaguehwnd;
  65.             return Clienthwnd;
  66.         }
  67.  
  68.     } while (true);
  69.  
  70. }
  71.  
  72. //BOOL CenterWindow(HWND hwnd, int &xPos, int &yPos, int &width, int &height, RECT &rc)
  73. //{
  74. //  GetWindowRect(hwnd, &rc);
  75. //
  76. //  width  = GetSystemMetrics(SM_CXSCREEN);
  77. //  height = GetSystemMetrics(SM_CYSCREEN);
  78. //
  79. //  xPos = (width / 2) -  ((rc.right-rc.left) / 2);
  80. //  yPos = (height / 2) - ((rc.bottom - rc.top) / 2);
  81. //
  82. //  SetWindowPos(hwnd, 0, xPos, yPos, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
  83. //
  84. //  cout <<"Finestra spostata alle coordinate:\nx = " << xPos << "\ny = " << yPos << endl;
  85. //
  86. //  return TRUE;
  87. //
  88. //}
  89. //
  90. //BOOL IsWindowCentred(HWND hwnd, int x, int y, int width, int height, RECT rc)
  91. //{
  92. //
  93. //  int xPos = (width / 2) - ((rc.right - rc.left) / 2);
  94. //  int yPos = (height / 2) - ((rc.bottom - rc.top) / 2);
  95. //
  96. //  if (xPos == x && yPos == y) return TRUE;
  97. //  else
  98. //  {
  99. //      cout << "\nEXIT_FAILURE\n";
  100. //      CreateTextFile("E' stato impossibile riposizionare la finestra di League Of Legends...");
  101. //      return FALSE;
  102. //  }
  103. //
  104. //}
  105.  
  106. BOOL saveBitmap(LPCSTR filename, HBITMAP bmp, HPALETTE pal)
  107. {
  108.     bool result = FALSE;
  109.     PICTDESC pd;
  110.  
  111.     pd.cbSizeofstruct = sizeof(PICTDESC);
  112.     pd.picType = PICTYPE_BITMAP;
  113.     pd.bmp.hbitmap = bmp;
  114.     pd.bmp.hpal = pal;
  115.  
  116.     LPPICTURE picture;
  117.     HRESULT res = OleCreatePictureIndirect(&pd, IID_IPicture, false,
  118.         reinterpret_cast<void**>(&picture));
  119.  
  120.     if (!SUCCEEDED(res))
  121.         return FALSE;
  122.  
  123.     LPSTREAM stream;
  124.     res = CreateStreamOnHGlobal(0, true, &stream);
  125.  
  126.     if (!SUCCEEDED(res))
  127.     {
  128.         picture->Release();
  129.         return FALSE;
  130.     }
  131.  
  132.     LONG bytes_streamed;
  133.     res = picture->SaveAsFile(stream, true, &bytes_streamed);
  134.  
  135.     HANDLE file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0,
  136.         CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  137.  
  138.     if (!SUCCEEDED(res) || !file)
  139.     {
  140.         stream->Release();
  141.         picture->Release();
  142.         return FALSE;
  143.     }
  144.  
  145.     HGLOBAL mem = 0;
  146.     GetHGlobalFromStream(stream, &mem);
  147.     LPVOID data = GlobalLock(mem);
  148.  
  149.     DWORD bytes_written;
  150.  
  151.     result = !!WriteFile(file, data, bytes_streamed, &bytes_written, 0);
  152.     result &= (bytes_written == static_cast<DWORD>(bytes_streamed));
  153.  
  154.     GlobalUnlock(mem);
  155.     CloseHandle(file);
  156.  
  157.     stream->Release();
  158.     picture->Release();
  159.  
  160.     return result;
  161. }
  162.  
  163. BOOL ScreenShot(HWND hwnd,LPCSTR fname)
  164. {
  165.     RECT rc;
  166.  
  167.     GetClientRect(hwnd, &rc);
  168.  
  169.     HDC hdcScreen = GetDC(NULL);
  170.     HDC hdc = CreateCompatibleDC(hdcScreen);
  171.     HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen,
  172.         rc.right - rc.left, rc.bottom - rc.top);
  173.     SelectObject(hdc, hbmp);
  174.  
  175.     PrintWindow(hwnd, hdc, PW_CLIENTONLY);
  176.  
  177.     HPALETTE hpal = NULL;
  178.     saveBitmap(fname, hbmp, hpal);
  179.  
  180.     if (saveBitmap(fname, hbmp, hpal)) return TRUE;
  181.     else
  182.     {
  183.         cout << "EXIT_FAILURE\n";
  184.         CreateTextFile("Impossibile eseguilo lo ScreenShot del client, errore funzione ScreenShot/SaveBitmap...");
  185.         return FALSE;
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement