Click here to Skip to main content
15,888,286 members
Home / Discussions / C#
   

C#

 
QuestionCompacting MS Access database from C# code? Pin
xkx3216-Jul-06 6:54
xkx3216-Jul-06 6:54 
AnswerRe: Compacting MS Access database from C# code? Pin
Eran Aharonovich16-Jul-06 8:25
Eran Aharonovich16-Jul-06 8:25 
QuestionThread termination Pin
overfreeze16-Jul-06 5:59
overfreeze16-Jul-06 5:59 
AnswerRe: Thread termination Pin
S. Senthil Kumar16-Jul-06 6:11
S. Senthil Kumar16-Jul-06 6:11 
AnswerRe: Thread termination Pin
Dario Solera16-Jul-06 6:36
Dario Solera16-Jul-06 6:36 
QuestionHow to access object in hash table ? Pin
Yanshof16-Jul-06 5:00
Yanshof16-Jul-06 5:00 
AnswerRe: How to access object in hash table ? Pin
S. Senthil Kumar16-Jul-06 6:16
S. Senthil Kumar16-Jul-06 6:16 
QuestionNeed help to convert an IntPtr to an int[] Pin
Thomas van Veen16-Jul-06 4:12
Thomas van Veen16-Jul-06 4:12 
Hi,

I am trying to convert a little delphi source to CSharp. The code reads the names of exported methods out of a native library. In Delphi, it works very fine, but now I need to convert it to CSharp. The most work is already done, but there are two thinks to do left.

1. The variable "Name" should be of type string
2. The variable "virtualAddressOfMappedFile" should be an Int[]

You can find the bookmarks 1. and 2. in the attached source code.

For testing the source, simply create a new Console Application, and replace the code with the following.

Thanks,
Thomas van Veen (specialwork)

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            /// This is the Delphi code
            //function ListDLLExports(const FileName: string): TStringList;
            //type
            //  TDWordArray = array [0..$FFFFF] of DWORD;
            //var
            //  imageinfo: LoadedImage;
            //  pExportDirectory: PImageExportDirectory;
            //  dirsize: Cardinal;
            //  pDummy: PImageSectionHeader;
            //  i: Cardinal;
            //  pNameRVAs: ^TDWordArray;
            //  Name: string;
            //begin
            //  {$Warnings Off}
            //  Result := TStringList.Create;
            //  if MapAndLoad(PAnsiChar(FileName), nil, @imageinfo, True, True) then begin
            //    try
            //      pExportDirectory := ImageDirectoryEntryToData(imageinfo.MappedAddress, False, IMAGE_DIRECTORY_ENTRY_EXPORT, dirsize);

            //      if (pExportDirectory <> nil) then begin
            //        pNameRVAs := ImageRvaToVa(imageinfo.FileHeader, imageinfo.MappedAddress, DWORD(pExportDirectory^.AddressOfNames), pDummy);

            //        for i := 0 to pExportDirectory^.NumberOfNames - 1 do begin
            //          Name := PChar(ImageRvaToVa(imageinfo.FileHeader, imageinfo.MappedAddress, pNameRVAs^[i], pDummy));
            //          Result.Add(Name);
            //        end;
            //      end;
            //    finally
            //      UnMapAndLoad(@imageinfo);
            //    end;
            //  end;
            //  {$Warnings ON}
            //end;

            pInvoke.LOADED_IMAGE loadedImage = new pInvoke.LOADED_IMAGE();

            try
            {
                if (pInvoke.MapAndLoad("advapi32.dll", null, ref loadedImage, true, true))
                {
                    int dirSize = 0;
                    IntPtr pImageExportDirectory = pInvoke.ImageDirectoryEntryToData(loadedImage.MappedAddress, false, pInvoke.IMAGE_DIRECTORY_ENTRY_EXPORT, ref dirSize);
                    pInvoke.IMAGE_EXPORT_DIRECTORY imageExportDirectory = (pInvoke.IMAGE_EXPORT_DIRECTORY)Marshal.PtrToStructure(pImageExportDirectory, typeof(pInvoke.IMAGE_EXPORT_DIRECTORY));
                    pInvoke.IMAGE_SECTION_HEADER imageSectionHeader = new pInvoke.IMAGE_SECTION_HEADER();
                    IntPtr virtualAddressOfMappedFile = pInvoke.ImageRvaToVa(ref loadedImage.FileHeader, loadedImage.MappedAddress, imageExportDirectory.AddressOfNames, ref imageSectionHeader);

                    for (int i = 0; i < imageExportDirectory.NumberOfNames; i++)
                    {
                        /// 1. The variable Name should be of type string
                        /// 2. The variable virtualAddressOfMappedFile should be and Int[]
                        IntPtr Name = pInvoke.ImageRvaToVa(ref loadedImage.FileHeader, loadedImage.MappedAddress, virtualAddressOfMappedFile.ToInt32(), ref imageSectionHeader);

                        Console.WriteLine(Name);
                    }

                    pInvoke.UnMapAndLoad(ref loadedImage);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                pInvoke.UnMapAndLoad(ref loadedImage);
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }

    public class pInvoke
    {
        public const int IMAGE_SIZEOF_SHORT_NAME = 8;
        public const int IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
        public const ushort IMAGE_DIRECTORY_ENTRY_EXPORT = 0;

        [DllImport("Imagehlp.dll")]
        public static extern bool MapAndLoad(string ImageName, string DllPath, ref LOADED_IMAGE LoadedImage, bool DotDll, bool ReadOnly);
        [DllImport("ImageHlp.dll")]
        public static extern bool UnMapAndLoad(ref LOADED_IMAGE LoadedImage);
        [DllImport("Dbghelp.dll")]
        public static extern IntPtr ImageDirectoryEntryToData(IntPtr Base, bool MappedAsImage, ushort DirectoryEntry, ref int Size);
        [DllImport("Dbghelp.dll")]
        public static extern IntPtr ImageRvaToVa(ref IMAGE_NT_HEADERS NtHeaders, IntPtr Base, int Rva, ref IMAGE_SECTION_HEADER LastRvaSection);

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct LOADED_IMAGE
        {
            public string ModuleName;
            public IntPtr hFile;
            public IntPtr MappedAddress;
            public IMAGE_NT_HEADERS FileHeader;
            public IMAGE_SECTION_HEADER LastRvaSection;
            public int NumberOfSections;
            public IMAGE_SECTION_HEADER Sections;
            public int Characteristics;
            public bool fSystemImage;
            public bool fDOSImage;
            public LIST_ENTRY Links;
            public int SizeOfImage;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct IMAGE_EXPORT_DIRECTORY
        {
            public int Characteristics;
            public int TimeDateStamp;
            public short MajorVersion;
            public short MinorVersion;
            public int Name;
            public int Base;
            public int NumberOfFunctions;
            public int NumberOfNames;
            public int AddressOfFunctions;
            public int AddressOfNames;
            public int AddressOfOrdinals;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct IMAGE_NT_HEADERS
        {
            /// DWORD Signature;  
            public int Signature;
            /// IMAGE_FILE_HEADER FileHeader;  
            public IMAGE_FILE_HEADER FileHeader;
            /// IMAGE_OPTIONAL_HEADER OptionalHeader;
            public IMAGE_OPTIONAL_HEADER OptionalHeader;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct LIST_ENTRY
        {
            /// struct _LIST_ENTRY *Flink;
            public IntPtr Flink;
            /// struct _LIST_ENTRY *Blink;
            public IntPtr Blink;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct IMAGE_SECTION_HEADER
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = IMAGE_SIZEOF_SHORT_NAME)]
            public byte[] Name;
            //union 
            //{    
            //    DWORD PhysicalAddress;    
            //    DWORD VirtualSize;  
            //} Misc;  
            public uint PhysicalAddress;
            //public uint VirtualSize;
            public uint VirtualAddress;
            public uint SizeOfRawData;
            public uint PointerToRawData;
            public uint PointerToRelocations;
            public uint PointerToLinenumbers;
            public short NumberOfRelocations;
            public short NumberOfLinenumbers;
            public uint Characteristics;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct IMAGE_FILE_HEADER
        {
            /// WORD Machine;  
            public ushort Machine;
            /// WORD NumberOfSections;  
            public ushort NumberOfSections;
            /// DWORD TimeDateStamp;  
            public uint TimeDateStamp;
            /// DWORD PointerToSymbolTable;  
            public uint PointerToSymbolTable;
            /// DWORD NumberOfSymbols;  
            public uint NumberOfSymbols;
            /// WORD SizeOfOptionalHeader;  
            public ushort SizeOfOptionalHeader;
            /// WORD Characteristics;
            public ushort Characteristics;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct IMAGE_OPTIONAL_HEADER
        {
            /// WORD Magic;  
            public ushort Magic;
            /// BYTE MajorLinkerVersion;  
            public byte MajorLinkerVersion;
            /// BYTE MinorLinkerVersion;  
            public byte MinorLinkerVersion;
            /// DWORD SizeOfCode;  
            public uint SizeOfCode;
            /// DWORD SizeOfInitializedData;  
            public uint SizeOfInitializedData;
            /// DWORD SizeOfUninitializedData;  
            public uint SizeOfUninitializedData;
            /// DWORD AddressOfEntryPoint;  
            public uint AddressOfEntryPoint;
            /// DWORD BaseOfCode;  
            public uint BaseOfCode;
            /// DWORD BaseOfData;  
            public uint BaseOfData;
            /// DWORD ImageBase;  
            public uint ImageBase;
            /// DWORD SectionAlignment;  
            public uint SectionAlignment;
            /// DWORD FileAlignment;  
            public uint FileAlignment;
            /// WORD MajorOperatingSystemVersion;  
            public ushort MajorOperatingSystemVersion;
            /// WORD MinorOperatingSystemVersion;  
            public ushort MinorOperatingSystemVersion;
            /// WORD MajorImageVersion;  
            public ushort MajorImageVersion;
            /// WORD MinorImageVersion;  
            public ushort MinorImageVersion;
            /// WORD MajorSubsystemVersion;  
            public ushort MajorSubsystemVersion;
            /// WORD MinorSubsystemVersion;  
            public ushort MinorSubsystemVersion;
            /// DWORD Win32VersionValue;  
            public uint Win32VersionValue;
            /// DWORD SizeOfImage;  
            public uint SizeOfImage;
            /// DWORD SizeOfHeaders;  
            public uint SizeOfHeaders;
            /// DWORD CheckSum;  
            public uint CheckSum;
            /// WORD Subsystem;  
            public ushort Subsystem;
            /// WORD DllCharacteristics;  
            public ushort DllCharacteristics;
            /// DWORD SizeOfStackReserve;  
            public uint SizeOfStackReserve;
            /// DWORD SizeOfStackCommit;  
            public uint SizeOfStackCommit;
            /// DWORD SizeOfHeapReserve;  
            public uint SizeOfHeapReserve;
            /// DWORD SizeOfHeapCommit;  
            public uint SizeOfHeapCommit;
            /// DWORD LoaderFlags;  
            public uint LoaderFlags;
            /// DWORD NumberOfRvaAndSizes;  
            public uint NumberOfRvaAndSizes;
            /// IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = IMAGE_NUMBEROF_DIRECTORY_ENTRIES)]
            public IMAGE_DATA_DIRECTORY[] DataDirectory;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct IMAGE_DATA_DIRECTORY
        {
            /// DWORD VirtualAddress;
            public uint VirtualAddress;
            /// DWORD Size;
            public uint Size;
        }
    }
}

Questioncatastrophic failure in using activex Pin
pardis16-Jul-06 1:03
pardis16-Jul-06 1:03 
AnswerRe: catastrophic failure in using activex Pin
Dave Kreskowiak16-Jul-06 2:17
mveDave Kreskowiak16-Jul-06 2:17 
GeneralRe: catastrophic failure in using activex Pin
pardis18-Jul-06 0:56
pardis18-Jul-06 0:56 
GeneralRe: catastrophic failure in using activex Pin
pardis18-Jul-06 1:04
pardis18-Jul-06 1:04 
GeneralRe: catastrophic failure in using activex Pin
Dave Kreskowiak18-Jul-06 1:38
mveDave Kreskowiak18-Jul-06 1:38 
Questionhi friends! Pin
kh.pakdaman16-Jul-06 0:06
kh.pakdaman16-Jul-06 0:06 
AnswerRe: hi friends! Pin
Ravi Bhavnani16-Jul-06 5:24
professionalRavi Bhavnani16-Jul-06 5:24 
AnswerRe: hi friends! Pin
Fahad Ali16-Jul-06 13:06
Fahad Ali16-Jul-06 13:06 
QuestionRe: hi friends! Pin
paulcortez16-Jul-06 18:58
paulcortez16-Jul-06 18:58 
AnswerRe: hi friends! Pin
PlayByTheRules16-Jul-06 23:16
PlayByTheRules16-Jul-06 23:16 
QuestionHow can I Map other web?? Pin
dabuskol16-Jul-06 0:03
dabuskol16-Jul-06 0:03 
AnswerRe: How can I Map other web?? Pin
Gavin Roberts17-Jul-06 2:11
Gavin Roberts17-Jul-06 2:11 
Questionunsure about running apps of different cd's... Pin
Anthony Mushrow15-Jul-06 15:04
professionalAnthony Mushrow15-Jul-06 15:04 
AnswerRe: unsure about running apps of different cd's... Pin
led mike15-Jul-06 16:23
led mike15-Jul-06 16:23 
GeneralRe: unsure about running apps of different cd's... Pin
Jun Du15-Jul-06 16:46
Jun Du15-Jul-06 16:46 
GeneralRe: unsure about running apps of different cd's... Pin
S. Senthil Kumar16-Jul-06 6:17
S. Senthil Kumar16-Jul-06 6:17 
GeneralRe: unsure about running apps of different cd's... Pin
led mike16-Jul-06 19:59
led mike16-Jul-06 19:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.