C# PE Imports Object Error

Max_Power

New member
Joined
Mar 28, 2004
Messages
2
I am writing a small C# program that reads an exectuable files imports into a list. I am having a program with this method:

Code:
public void ListImports(ListBox lstList, int hMod)
{
	IMAGE_DOS_HEADER idhDosHeader = new IMAGE_DOS_HEADER();
	IMAGE_NT_HEADERS inhNtHeaders = new IMAGE_NT_HEADERS();
	IMAGE_IMPORT_DESCRIPTOR iidImportDesc = new IMAGE_IMPORT_DESCRIPTOR();
	int intImportDesc;

    Marshal.PtrToStructure(new IntPtr(hMod), idhDosHeader);

    Marshal.PtrToStructure(new IntPtr(hMod + idhDosHeader.e_lfanew), inhNtHeaders); 


    if (inhNtHeaders.OptionalHeader.DataDirectory0.VirtualAddress == 0) 
		return;


    intImportDesc = hMod + inhNtHeaders.OptionalHeader.DataDirectory0.VirtualAddress;
    Marshal.PtrToStructure(new IntPtr(intImportDesc), iidImportDesc);

    do 
	{ 
		///////////////////////////////////////////
		//ERRORS HERE
		///////////////////////////////////////////
        MessageBox.Show(Marshal.PtrToStringAnsi(new IntPtr(hMod + iidImportDesc.Name)));
        intImportDesc += Marshal.SizeOf(iidImportDesc);

        Marshal.PtrToStructure(new IntPtr(intImportDesc), iidImportDesc);
	} while(iidImportDesc.Name != 0);
}//End ListImports

More specifically the error is "Object reference not set to an instance of an object."

I am attaching all the code to bottom in case it helps.

Thanks for any help.

Regards,

Max_Power

EDIT: Added attachment.
 

Attachments

Stack trace:
Code:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at [b]Microsoft.Win32.Win32Native.CopyMemoryAnsi(StringBuilder pdst, IntPtr psrc, IntPtr sizetcb)[/b]
at System.Runtime.InteropServices.Marshal.PtrToStringAnsi(IntPtr ptr)
at CS_PE_Get_Imports.clsPERoutines.ListImports(ListBox lstList, Int32 hMod)
 
The first time through the loop the value for iidImportDesc.Name is zero. This should be a good clue towards your problem (incorrect offsets).
 
Derek Stone said:
The first time through the loop the value for iidImportDesc.Name is zero. This should be a good clue towards your problem (incorrect offsets).

That had already occured to me. I played around and frankly I am stumped, there is something I am missing. Suppose you could provide a bit more assistance?
 
Back
Top