Valhalla Legends Archive

Programming => General Programming => Topic started by: iago on February 05, 2003, 06:02 PM

Title: Question [c++]
Post by: iago on February 05, 2003, 06:02 PM
Does anybody know how I could get a pointer to the beginning and the end of (my own) program's datasegment?  I tried adding a void FirstFunction() and void LastFunction() but that didn't work :-/
Title: Re: Question [c++]
Post by: Yoni on February 05, 2003, 09:08 PM
You would have to open your PE header (in memory) and parse it to get that.
Title: Re: Question [c++]
Post by: iago on February 06, 2003, 05:50 AM
So there's no function GetPointerToEnd() ? :-(
Title: Re: Question [c++]
Post by: Yoni on February 06, 2003, 06:08 AM
AFAIK there is no AllPurposeMagicFunctions.dll
Title: Re: Question [c++]
Post by: iago on February 06, 2003, 07:12 AM
I guess somebody should write it, then...
Title: Re: Question [c++]
Post by: Skywing on February 06, 2003, 08:16 AM
You turn on .map and .cod generation, and parse those.
Title: Re: Question [c++]
Post by: Etheran on February 06, 2003, 08:50 PM
QuoteBe careful, though, if it was made by a Canadian programmer you might end up with $10000000cdn, which happens to be less than a dollar American!
in that case I would just go with
 deposit_money_in_eths_bank_account('£',100000000);
:)
Title: Re: Question [c++]
Post by: Arta on February 07, 2003, 03:55 AM
What Eth said :P
Title: Re: Question [c++]
Post by: Banana fanna fo fanna on February 07, 2003, 02:24 PM
This isn't an exact answer to your question, but it may help.

You can do stuff like this in C:

void myfunc() {
      DWORD addr;

      __asm {
            mov dword ptr addr, offset [label]
      }
label:
}
Title: Re: Question [c++]
Post by: iago on February 07, 2003, 10:51 PM
hmm.. I like the header idea, but if Storm's actually works it would be a lot easier.  

Thanks!  Can somebody close this topic now?  It's dead :-)
Title: Re: Question [c++]
Post by: Adron on February 08, 2003, 06:41 AM
I deleted my off-topic post and add this instead:
void printinfo()
{
      unsigned base = (unsigned)GetModuleHandle(0);
      IMAGE_DOS_HEADER *idh = (IMAGE_DOS_HEADER*)base;
      IMAGE_NT_HEADERS *inh = (IMAGE_NT_HEADERS*)((unsigned)idh + idh->e_lfanew);
      IMAGE_SECTION_HEADER *ish = IMAGE_FIRST_SECTION(inh);
      for(int i = 0; i < inh->FileHeader.NumberOfSections; i++) {
            printf("Section %s at %08x ends at %08x, flags %08x\n",
                  ish[i].Name, ish[i].VirtualAddress + base, ish[i].VirtualAddress + base + ish[i].Misc.VirtualSize, ish[i].Characteristics);
      }
}
Title: Re: Question [c++]
Post by: iago on February 08, 2003, 09:12 AM
oooh, very nice, thanks :)
Title: Re: Question [c++]
Post by: Banana fanna fo fanna on February 09, 2003, 07:47 AM
Can you give some code to convert a virtual addr to a file offset?
Title: Re: Question [c++]
Post by: Adron on February 09, 2003, 08:07 AM
Most of those things are actually in the imagehlp api. If you're doing things that require mapping virtual address to file offset, you're probably modifying executables. Then you should be using the imagehlp api.
Title: Re: Question [c++]
Post by: Banana fanna fo fanna on February 09, 2003, 05:30 PM
I am...it's just that no one taught me how to load memmapped files and I'm lost lol.
Title: Re: Question [c++]
Post by: Adron on February 10, 2003, 01:31 PM
Since i've been opening nbbot again and making some changes, i'll post this snippet from it, regarding memory mapping files:

     LPCVOID map;
      int size;
      HANDLE h, sh;
      h = CreateFile(exe, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);
      if(h != INVALID_HANDLE_VALUE) {
            sh = CreateFileMapping(h, 0, PAGE_READONLY|SEC_COMMIT, 0, 0, 0);
            map = MapViewOfFile(sh, FILE_MAP_READ, 0, 0, 0);
            if((int)map) {
                  size = GetFileSize(h, 0);
                  // Do something with it
                  UnmapViewOfFile(map);
            }
            CloseHandle(h);
            CloseHandle(sh);
      }
Title: Re: Question [c++]
Post by: Banana fanna fo fanna on February 10, 2003, 06:01 PM
Thank you my friend.