Process ID

I need to find out where a program is keeping its data at runtime (get the data's segment address with API's or something?) so i can get a pointer to there.  All i know about the program at runtime is it's name.  
im using visualc++4.
Thanks in advance.
not sure if it's a hard question. ill say moderate?
bod_1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PBMaxCommented:
First of all I would suggest posting in the programming section under C++.  Second of all the allocation of memory is dependent on how much memory is needed and what operating system you are using.  Basically a program says I need this much memory and the operating system gives that much memory to that process and restricts access to only that program.  Depending on what is currently running at the time that memory address can be anywhere.  The use of pointers is ususally the preferred method of assigning varaibles in memory.  Your variable can be anywhere in memory even if it is a linked list.  It doesn't have to be all together.  What I'm basically sayying is that the memory space allocated for data can be anywhere in memory each time a program is run.
0
bod_1Author Commented:
I know windows allocates memory to programs at runtime and that the memory address will be dependent on what memory segments or blocks are available when the program is run.  
That is my problem is that I need to find out where so i can take a 'snapshot' of the memory-ReadProcessMemory.  
I have a bit of sample code from enigma@exo.com who did what i'm trying to do with a different application.
I'm not sure he might be finding a process with a specified size exe?
If this doesn't make any sense then don't bother with my question, i've probably missed something somewhere (like Tlhelp32.h).
Anyways, for all it's worth the code (class) used to get the process ID is as follows but i dont completely understand what he's doing.  He uses the Tlhelp32.h file to do it though.
#include <tlhelp32.h>

long CTest4Dlg::GetPID()
{
      HANDLE hSnapshot = NULL;
      PROCESSENTRY32 *p;
      DWORD size;
      BOOL rc;
      DWORD errorCode;
      const DWORD extraSize = MAX_PATH;
      long result;

      size = sizeof(PROCESSENTRY32);
      hSnapshot = pCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      p = (PROCESSENTRY32 *)malloc(size + extraSize);
      p->dwSize = size + extraSize;
      rc = pProcess32First(hSnapshot, p);
      if (!rc)
            errorCode = GetLastError();
      else
            errorCode = 0;
      //Not found in the list!
      if (errorCode == ERROR_NO_MORE_FILES)
            return 0;
      do
      {
            if ( (strstr(p->szExeFile, "DIABLO.EXE")) || (strstr(p->szExeFile, "HYBRID.EXE")) )
            {
                  spawn = FALSE;
                  result = p->th32ProcessID;
                  free(p);
                  return result;
            }
            if (strstr(p->szExeFile, "DIABLO_S.EXE"))
            {
                  spawn = TRUE;
                  result = p->th32ProcessID;
                  free(p);
                  return result;
            }
            rc = pProcess32Next(hSnapshot, p);
            if (!rc)
                  errorCode = GetLastError();
            else
                  errorCode = 0;
            }while (errorCode != ERROR_NO_MORE_FILES);
      free(p);
      return 0;
}
Thanks again


0
bod_1Author Commented:
//Allocate memory to in OUR memory space, in which to copy that character
      buf = (char *)malloc(mode==BUFFER?BUFFER_SIZE:CHARACTER_SIZE);
      //And finally, copy that character from the other program's protected memory space into
      //our memory space!  (Wow!  I didn't think this was possible with protected memory!)
      ReadProcessMemory(h, (void *)((char *)begin+CHARACTER_SPACING*memoryCharacterSlot), buf, mode==BUFFER?BUFFER_SIZE:CHARACTER_SIZE, &size);

0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

bod_1Author Commented:
Adjusted points to 200
0
interCommented:
Dear friend,
The process walk needs a special DLL called TlHelp32.dll which is normally not distributed with windows, for 16 bit processes however there is a toolhelp.dll in system directory. So, since you have VC++ it may installs it. Search for it and respond if it is available inyour system.

The code you gave does the following, and it is partialy a answer of the question. It returns the ProcessID of the DIABLO.EXE if it  is in the memory.

So,
1 - Do you have tlhelp32.h?
2 - Do you have tlhelp32.dll?
3 - If so did you try the code above?

Once you answer these, we'll solve your problem(and the problem does make sense, all the debugging applications use tlhelp32 to perform such tasks)

Igor
0
interCommented:
I am in jam, sorry!
The toolhelp.dll is seperate for 16 bit apps however they are not for 32 bit apps. The toolhelp functions for 32bit apps reside in kernel32.dll. So, your kernel32 should include the tool help functions.

Sorry for the jam!
Igor
0
bod_1Author Commented:
Thanks for response Inter,
Now i know i've found the major header i've got to work with.
Yea, I guess my question should read how would you make the transition between having a processes ID to having the physical memory address of that process?
i've since found a small program called Tinkerbell which does exatly this.  It lists all of the running processes. Once you select one it reads the memory of it.  it can be found at;
http://www.burgoyne.com/pages/dchriste
anyway's thanks

0
interCommented:

// I want to write it for you but I have no time for that
// So this is the code that may give you the idea

...
  HEAPENTRY32 lphe;
  DWORD th32ProcessID, th32HeapID;
  LPCVOID lpBaseAddress;
  LPVOID  lpBuffer;
  DWORD   cbRead;
  DWORD lpNumberOfBytesRead;
  ...
// assume we find the th32ProcessID as in the previous code

  hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST, 0);

// and obtain th32HeapID by snapshot
  ...
  Heap32First(&lphe, th32ProcessID, th32HeapID);
  do {
    // Here we have the pointer to data in the lphe structure as follows
..
    DWORD  dwSize;
    HANDLE hHandle;      // Handle of this heap block
    DWORD  dwAddress;      // Linear address of start of block
                        // Examine it to find what you seek
    DWORD  dwBlockSize; // Size of block in bytes
    DWORD  dwFlags;
    DWORD  dwLockCount;
    DWORD  dwResvd;
    DWORD  th32ProcessID;   // owning process
    DWORD  th32HeapID;          // heap block is in...
    Heap32Next(&lphe);
  } while (lphe.dwAdress == NULL);
  CloseHandle(hSnapShot);

Sorry, I could really complete but, I have no time!

Igor

0
bod_1Author Commented:
Thanks Inter,
At first glance i think your codes' gotten me thinking in the right terms so it's already helped. I'll get it. Thanks man.
Now, i cant seem to find anything to close this question as answered, there's only a submit button...
jeez...
0
interCommented:
I am back again,

Happy to hear it helps

Sincerey,
Igor
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gourouCommented:
Ok it's true but for windows 9X only, on WIN NT try this

DWORD pdwProcess[100];
HANDLE hProcess;
HMODULE phModule[100];
DWORD dwNbProcessInByte,dwNbModuleInByte;
char pcNameModule[255+1];
unsigned int uiI;

 EnumProcesses(pdwProcess,100*sizeof(DWORD),&dwNbProcessInByte);
 for(iI=0;iI<(dwNbProcessInByte/4);iI++)
 {
  hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pdwProcess[iI]);
  EnumProcessModules(hProcess,phModule,100*sizeof(HMODULE),&dwNbModuleInByte);
  GetModuleBaseName(hProcess,*phModule,pcNameModule,255+1);
  CloseHandle(hProcess);
 }

you must include the psapi.h and link with psapi.lib ( to use psapi.dll)

bye...
0
bod_1Author Commented:
Thanks for the xtra gourou.
I noticed Windows NT has a lot of extras that 95 doesn't have.
I've got a 95/Linux box.  With 95, I decided to use the Toolhelp 'debugging' library.
Jeez, In January I'd just started teaching myself C (less than one month). I did a little bit with windows and have just recently took on programming Linux because at school - semesters two and on - we're going to be programming for System V.
I'm trying to learn X-Windows now so Windows programming is pretty much on the back-burner for me.
Anyways thanks for the info.
Cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Hardware

From novice to tech pro — start learning today.