Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am writing a small C# program that reads an exectuable files imports into a list. I am having a program with this method:

 

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.

CS_PE_Get_Imports.zip

  • *Gurus*
Posted

Stack trace:

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)

Posted
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?

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...