Can someone help me to convert the following c++ code?
Posted: Mon Sep 12, 2005 5:30 pm
I need help converting the following code so that it works in pb..
I am not a complete newb with winapi, but i will say that i haven't got much experience trying to make api calls and converting things to pb. this would be a learning experience for me and also help with my api calls in the future-
code here:
DWORD FindModule (HANDLE hProcess, const char* sModule)
{
HMODULE hMod[1024];
DWORD cbNeeded;
If( EnumProcessModules( hProcess, hMod, SizeOf(hMod), &cbNeeded) )
{
For ( unsigned int i=0 ; i < (cbNeeded / sizeof(HMODULE)) ; i++ )
{
char sModName[MAX_PATH];
GetModuleBaseNameA( hProcess, hMod, sModName, SizeOf(sModName) );
If ( !stricmp(sModName, sModule) )
{
Return (DWORD)hMod;
}
}
}
Return 0;
}
DWORD FindProcess (const char* sProcess)
{
HANDLE hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
If( hProcessSnap == INVALID_HANDLE_VALUE ) Return 0;
PROCESSENTRY32 pe32;
pe32.dwSize = SizeOf(PROCESSENTRY32);
If( Process32First( hProcessSnap, &pe32 ) )
{
do
{
If ( !stricmp(pe32.szExeFile, sProcess) )
{
Return pe32.th32ProcessID;
}
}
While( Process32Next( hProcessSnap, &pe32 ) );
}
CloseHandle( hProcessSnap );
Return 0;
}
How to Use:
DWORD ProcessID = FindProcess("calc.exe");
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
DWORD ModuleBase = FindModule(hProcess, "USER32.dll");
// other code here //
CloseHandle( hProcess );
include files needed for c++ linking and compiling...
#include <windows.h>
#include "psapi.h"
#include <tlhelp32.h>
I am trying to use these methods to find the process handle for programs (like calculator) and also to find the base addressess of modules used by that program (for example the USER32 library being used by calculator).
Thanks in advance,
Cal
I am not a complete newb with winapi, but i will say that i haven't got much experience trying to make api calls and converting things to pb. this would be a learning experience for me and also help with my api calls in the future-
code here:
DWORD FindModule (HANDLE hProcess, const char* sModule)
{
HMODULE hMod[1024];
DWORD cbNeeded;
If( EnumProcessModules( hProcess, hMod, SizeOf(hMod), &cbNeeded) )
{
For ( unsigned int i=0 ; i < (cbNeeded / sizeof(HMODULE)) ; i++ )
{
char sModName[MAX_PATH];
GetModuleBaseNameA( hProcess, hMod, sModName, SizeOf(sModName) );
If ( !stricmp(sModName, sModule) )
{
Return (DWORD)hMod;
}
}
}
Return 0;
}
DWORD FindProcess (const char* sProcess)
{
HANDLE hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
If( hProcessSnap == INVALID_HANDLE_VALUE ) Return 0;
PROCESSENTRY32 pe32;
pe32.dwSize = SizeOf(PROCESSENTRY32);
If( Process32First( hProcessSnap, &pe32 ) )
{
do
{
If ( !stricmp(pe32.szExeFile, sProcess) )
{
Return pe32.th32ProcessID;
}
}
While( Process32Next( hProcessSnap, &pe32 ) );
}
CloseHandle( hProcessSnap );
Return 0;
}
How to Use:
DWORD ProcessID = FindProcess("calc.exe");
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessID);
DWORD ModuleBase = FindModule(hProcess, "USER32.dll");
// other code here //
CloseHandle( hProcess );
include files needed for c++ linking and compiling...
#include <windows.h>
#include "psapi.h"
#include <tlhelp32.h>
I am trying to use these methods to find the process handle for programs (like calculator) and also to find the base addressess of modules used by that program (for example the USER32 library being used by calculator).
Thanks in advance,
Cal