Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Snippets

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming -> General programming+
View previous topic :: View next topic  
Author Message
Irwin
Banned!
Reputation: 1

Joined: 04 Feb 2007
Posts: 0

PostPosted: Sun Aug 31, 2008 5:13 am    Post subject: Snippets Reply with quote

I make a lot of snippets I almost never use again, so I thought I might post them here.

Code:
#include <tlhelp32.h>

DWORD FindProcessForFags(__in_z LPCTSTR lpcszFileName)
{
  PROCESSENTRY32  ProcessEntry;
  HANDLE          hSnapshot;
  DWORD           dwProcessId = 0;

  hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if (hSnapshot != INVALID_HANDLE_VALUE)
  {
    ProcessEntry.dwSize = sizeof(PROCESSENTRY32);
    if (Process32First(hSnapshot, &ProcessEntry))
    {
      do
      {
        if (!lstrcmpi(ProcessEntry.szExeFile, lpcszFileName))
        {
          dwProcessId = ProcessEntry.th32ProcessID;
          break;
        }
      } while (Process32Next(hSnapshot, &ProcessEntry));
    }
    CloseHandle(hSnapshot);
  }
  return dwProcessId;
}


Code:
#include <psapi.h>
#define MAX_PROCESSES 1024

DWORD FindProcess(__in_z LPCTSTR lpcszFileName)
{
  LPDWORD lpdwProcessIds;
  LPTSTR  lpszBaseName;
  HANDLE  hProcess;
  DWORD   i, cdwProcesses, dwProcessId = 0;

  lpdwProcessIds = (LPDWORD)HeapAlloc(GetProcessHeap(), 0, MAX_PROCESSES*sizeof(DWORD));
  if (lpdwProcessIds != NULL)
  {
    if (EnumProcesses(lpdwProcessIds, MAX_PROCESSES*sizeof(DWORD), &cdwProcesses))
    {
      lpszBaseName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, MAX_PATH*sizeof(TCHAR));
      if (lpszBaseName != NULL)
      {
        cdwProcesses /= sizeof(DWORD);
        for (i = 0; i < cdwProcesses; i++)
        {
          hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, lpdwProcessIds[i]);
          if (hProcess != NULL)
          {
            if (GetModuleBaseName(hProcess, NULL, lpszBaseName, MAX_PATH) > 0)
            {
              if (!lstrcmpi(lpszBaseName, lpcszFileName))
              {
                dwProcessId = lpdwProcessIds[i];
                CloseHandle(hProcess);
                break;
              }
            }
            CloseHandle(hProcess);
          }
        }
        HeapFree(GetProcessHeap(), 0, (LPVOID)lpszBaseName);
      }
    }
    HeapFree(GetProcessHeap(), 0, (LPVOID)lpdwProcessIds);
  }
  return dwProcessId;
}


Code:
typedef struct _TRANSLATION {
  WORD  wLanguageID;
  WORD  wCharacterSet;
} TRANSLATION, *LPTRANSLATION;

BOOL GetFileDescription(__in_z LPCTSTR lpFileName, __out_ecount_z(nSize) LPTSTR lpBuffer, __in UINT nSize)
{
  LPTRANSLATION lpTranslation;
  LPVOID        lpData;
  LPTSTR        lpFormat, lpFileDescription;
  DWORD         dwLength;
  UINT          uLength;
  BOOL          bRET = FALSE;

  dwLength = GetFileVersionInfoSize(lpFileName, NULL);
  if (dwLength != 0)
  {
    lpData = HeapAlloc(GetProcessHeap(), 0, dwLength);
    if (lpData != NULL)
    {
      if (GetFileVersionInfo(lpFileName, 0, dwLength, lpData))
      {
        if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslation, &uLength))
        {
          if (uLength == sizeof(TRANSLATION))
          {
            lpFormat = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, 50*sizeof(TCHAR));
            if (lpFormat != NULL)
            {
              wsprintf(lpFormat, _T("\\StringFileInfo\\%04x%04x\\FileDescription"), lpTranslation->wLanguageID, lpTranslation->wCharacterSet);
              if (VerQueryValue(lpData, lpFormat, (LPVOID*)&lpFileDescription, &uLength))
                bRET = lstrcpyn(lpBuffer, lpFileDescription, min(nSize, uLength)) != NULL;
              HeapFree(GetProcessHeap(), 0, (LPVOID)lpFormat);
            }
          }
        }
      }
      HeapFree(GetProcessHeap(), 0, (LPVOID)lpData);
    }
  }
  return bRET;
}


Code:
#include <psapi.h>

BOOL DumpModuleEx(__in HANDLE hProcess, __in HMODULE hModule)
{
  MODULEINFO  ModuleInfo;
  LPVOID      lpBuffer;
  LPTSTR      lpFileName;
  HANDLE      hFile;
  DWORD       NumberOfBytesWritten;
  BOOL        bRET = FALSE;

  if (GetModuleInformation(hProcess, hModule, &ModuleInfo, sizeof(MODULEINFO)))
  {
    lpBuffer = VirtualAlloc(NULL, ModuleInfo.SizeOfImage, MEM_COMMIT, PAGE_READWRITE);
    if (lpBuffer != NULL)
    {
      if (ReadProcessMemory(hProcess, ModuleInfo.lpBaseOfDll, lpBuffer, ModuleInfo.SizeOfImage, NULL))
      {
        lpFileName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, MAX_PATH*sizeof(TCHAR));
        if (lpFileName != NULL)
        {
          if (GetModuleFileNameEx(hProcess, hModule, lpFileName, MAX_PATH))
          {
            lstrcat(lpFileName, TEXT(".dmp"));
            hFile = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
            if (hFile != INVALID_HANDLE_VALUE)
            {
              if (WriteFile(hFile, lpBuffer, ModuleInfo.SizeOfImage, &NumberOfBytesWritten, NULL))
                bRET = CloseHandle(hFile);
            }
          }
          HeapFree(GetProcessHeap(), 0, (LPVOID)lpFileName);
        }
      }
      VirtualFree(lpBuffer, ModuleInfo.SizeOfImage, MEM_DECOMMIT);
    }
  }
  return bRET;
}

#define DumpModule(x) DumpModuleEx(GetCurrentProcess(), x)


More will be added later.


Last edited by Irwin on Sun Aug 31, 2008 2:31 pm; edited 1 time in total
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming -> General programming+ All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites