Max_Power Posted March 28, 2004 Posted March 28, 2004 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 Quote
*Gurus* Derek Stone Posted March 29, 2004 *Gurus* Posted March 29, 2004 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) Quote Posting Guidelines
*Gurus* Derek Stone Posted March 29, 2004 *Gurus* Posted March 29, 2004 The first time through the loop the value for iidImportDesc.Name is zero. This should be a good clue towards your problem (incorrect offsets). Quote Posting Guidelines
Max_Power Posted March 30, 2004 Author Posted March 30, 2004 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? Quote
Recommended Posts