Thread: Unexpected output

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Unexpected output

    The following code is supposed to print out the names of all the sections in the specified exe (c:\linked list.exe in this case), but it produces some bizzare output. Can someone please help me figure out whats wrong??

    Code:
    #include<Windows.h>
    #include<stdio.h>
    #include<WinNT.h>
    
    int main()
    {
        FILE *fp; 
        int i;
    
        if((fp = fopen("c:\\Linked List.exe","rb"))==NULL)
            printf("unable to open");
    
    
        IMAGE_DOS_HEADER imdh;
        fread(&imdh,sizeof(imdh),1,fp);
    
    
        IMAGE_NT_HEADERS imnth;
        fread(&imnth,sizeof(imnth),1,fp);
    
        IMAGE_SECTION_HEADER *pimsh;
        pimsh = (IMAGE_SECTION_HEADER *)malloc(sizeof(IMAGE_SECTION_HEADER) * imnth.FileHeader.NumberOfSections);
    
        fread(pimsh,sizeof(IMAGE_SECTION_HEADER),imnth.FileHeader.NumberOfSections,fp);
    
        for(i=0;i<imnth.FileHeader.NumberOfSections;i++)
        {
            printf("%s\n",pimsh->Name);
            pimsh++;
        }
    
    }
    Last edited by juice; 03-09-2012 at 10:00 AM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Don't typecast malloc - FAQ > Casting malloc - Cprogramming.com

    Always free() memory when you're done using it.

    And, post this question in the Windows board if you want specific answers to your question.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I would've answered this question except that the guy never says "thanks".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Moved
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by oogabooga View Post
    I would've answered this question except that the guy never says "thanks".
    Hey.. come on.... I say thanks... and I wonder where I would have been without you people, and I am pretty much greatful to you all, especially you, laserlight, salem and commontater, so with sugar on top "THANK YOU ALL.... VERY MUCH!!"

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Well, you've probably already figured this out, but you need to seek to the offset of the IMAGE_NT_HEADERS after reading the dos header. Something like this:
    Code:
        fseek(fp, imdh.e_lfanew, SEEK_SET);
    There's actually a very short program between the two that prints "This program cannot be run in DOS mode" if you try to run it in dos (which is not of course the same as running it from the console).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by oogabooga View Post
    Well, you've probably already figured this out
    Yes I have, but thanks anyways...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected output
    By juice in forum C Programming
    Replies: 24
    Last Post: 11-18-2011, 11:18 PM
  2. Unexpected output
    By GolDRoger in forum C Programming
    Replies: 9
    Last Post: 11-18-2011, 02:49 PM
  3. Unexpected Output in structure
    By bhagwat_maimt in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2006, 10:50 PM
  4. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 09:03 PM