Can someone help me to convert the following c++ code?

Windows specific forum
mikecaliber
User
User
Posts: 22
Joined: Sun Feb 15, 2004 5:34 pm

Can someone help me to convert the following c++ code?

Post by mikecaliber »

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
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

@mikecaliber,

Should not this be like the following ? But I can't test it right now. Just try and tell what happens.

Code: Select all

Procedure FindModule(hProcess, sModule.s)
Structure HMODULE
  hMod.l[1024]
EndStructure
cbNeeded.l
  If (EnumProcessModules(hProcess, hMod, SizeOf(hMod), @cbNeeded))
      For i = 0
        char sModName[MAX_PATH];
        GetModuleBaseNameA( hProcess, HMODULE\hMod[i], sModName, SizeOf(sModName))
        If ( !stricmp(sModName, sModule) )
            Return (DWORD)hMod[i];
        EndIf
      Next
  EndIf
  ProcedureReturn 0
EndProcedure

Procedure FindProcess(sProcess.s)
  hProcessSnap.l = CreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0 )
  If  hProcessSnap = #INVALID_HANDLE_VALUE
      ProcedureReturn 0
  EndIf
  pe32.PROCESSENTRY32
  pe32.dwSize = SizeOf(PROCESSENTRY32)
  If Process32First(hProcessSnap, @pe32)
      Repeat
        If stricmp(pe32.szExeFile, sProcess) = #False
            ProcedureReturn pe32.th32ProcessID
        EndIf
      Until Process32Next(hProcessSnap, @pe32)) = #False
  EndIf
  CloseHandle(hProcessSnap)
  ProcedureReturn 0
EndProcedure
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Post Reply