locked
Get List of functions defined in DLL. RRS feed

  • Question

  • Hi,

    I want to display list of functions one can access in DLL. My dll is Managed DLL
    With the code given below i was able to get the list but there are some functions they appear even though they are not really useful to user e.g.

    Boolean Equals(System.Object)
    Int32 GetHashCode()
    System.String ToString();

    Is there any way to only show the functions available to user. I dont want to get the functions defined in Base type.

    Assembly assembly = Assembly.LoadFile(tbXMLFile.Text);
    foreach (Type type in assembly.GetTypes())
    {
    MethodInfo[] info = type.GetMethods();
    //MessageBox.Show(info.Length.ToString());
    foreach(MethodInfo infor in info)
    {
    listBox1.Items.Add(infor.ToString());
    }


    }
    Friday, May 25, 2007 10:08 PM

Answers

  • Try using the GetMethods (BindingFlags) overload http://msdn2.microsoft.com/en-us/library/4d848zkb.aspx 

     

    For example BindingFlags.DeclaredOnly might be useful.

    Friday, May 25, 2007 10:25 PM
  • Try Assembly.GetExportedTypes().
    Sunday, May 27, 2007 7:53 PM
  • Hi, Nobugz,

    I got the solution of above problem. basically i have to use

     MethodInfo info = type.GetMethod(methodename,types);                               
                  where types is array of Type

    Still wondering about the Unload problem of DLL.
    Wednesday, May 30, 2007 8:49 PM
  • Hi nobugs,

    Here is the way i create a type array. I ask user the parameter the function needs..
    and using this function i create a Type array.

    static Type CreateType(String type)
            {
                Type abc = null;
                try
                {
                    if (type.Equals("Boolean"))
                        abc = typeof(bool);
                    if (type.Equals("Byte"))
                        abc = typeof(byte);
                    if (type.Equals("Char"))
                        abc = typeof(char);
                    if (type.Equals("Decimal"))
                        abc = typeof(decimal);
                    if (type.Equals("Double"))
                        abc = typeof(double);              
                    if (type.Equals("Int16"))
                        abc = typeof(Int16);
                    if (type.Equals("Int32"))
                        abc = typeof(Int32);
                    if (type.Equals("Int64"))
                        abc = typeof(Int64);
                    if (type.Equals("Object"))
                        abc = typeof(Object);
                    if (type.Equals("SByte"))
                        abc = typeof(SByte);
                    if (type.Equals("Single"))
                        abc = typeof(Single); ;
                    if (type.Equals("String"))
                        abc = typeof(string); ;
                    if (type.Equals("UInt16"))
                        abc = typeof(UInt16);
                    if (type.Equals("UInt32"))
                        abc = typeof(UInt32);
                    if (type.Equals("UInt64"))
                        abc = typeof(UInt64);


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return abc;
            }
    Friday, June 1, 2007 7:14 PM

All replies

  • Try using the GetMethods (BindingFlags) overload http://msdn2.microsoft.com/en-us/library/4d848zkb.aspx 

     

    For example BindingFlags.DeclaredOnly might be useful.

    Friday, May 25, 2007 10:25 PM
  • Try Assembly.GetExportedTypes().
    Sunday, May 27, 2007 7:53 PM
  • Hi nobugz,

    Thanks for your advice, after changing my previous code with this one i am getting what i want..

    Assembly assembly = Assembly.LoadFile(tbXMLFile.Text);
    foreach (Type type in assembly.GetExportedTypes())
                {
                    MethodInfo[] info = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                  
                    foreach (MethodInfo infor in info)
                    {
                        listBox1.Items.Add(infor.ToString());
                    }

                }

    What abt if dll is Unmanaged..how do i get this list...

    Monday, May 28, 2007 6:10 PM
  • An unmanaged DLL doesn't export any types, just function entrypoints.  There is no API function I know of that can enumerate them.  Start the VS2005 command prompt and run Dumpbin.exe /exports name.dll to see the list.
    Monday, May 28, 2007 6:46 PM
  • Or run Depends for a more graphically oriented tool
    Tuesday, May 29, 2007 5:16 PM
  • Good news is Guys...
    I was able to get the List of Exported functions from Unmanaged DLL.

    here is the c++ code , just need to convert it to c#...

    void ListDLLFunctions(AnsiString sADllName, TStringList *slListOfDllFunctions){
    DWORD *dNameRVAs = (DWORD *)calloc(1048575,sizeof(DWORD));
    _IMAGE_EXPORT_DIRECTORY *ImageExportDirectory;
    _IMAGE_SECTION_HEADER *ImageSectionHeader;
    unsigned long cDirSize;
    _LOADED_IMAGE LoadedImage;
    AnsiString sName;
    slListOfDllFunctions->Clear();
    if (MapAndLoad(sADllName.c_str(), NULL, &LoadedImage, True, True)){
    try{
    ImageExportDirectory = (_IMAGE_EXPORT_DIRECTORY *)ImageDirectoryEntryToData(LoadedImage.MappedAddress,
    false, IMAGE_DIRECTORY_ENTRY_EXPORT, &cDirSize);
    if (ImageExportDirectory != NULL){
    dNameRVAs = (DWORD *)ImageRvaToVa(LoadedImage.FileHeader, LoadedImage.MappedAddress,
    DWORD(ImageExportDirectory->AddressOfNames), &ImageSectionHeader);
    for( int i = 0; i < ImageExportDirectory->NumberOfNames; i++){
    sName = (char *)ImageRvaToVa(LoadedImage.FileHeader, LoadedImage.MappedAddress,
    dNameRVAsIdea, &ImageSectionHeader);
    slListOfDllFunctions->Add(sName);
    }
    }
    }__finally{
    UnMapAndLoad(&LoadedImage);
    }
    }
    }

    One More questions regarding the Previous issues

    1) How do i unload the DLL both Manage and Unmanaged..

    Tuesday, May 29, 2007 5:34 PM
  • Guys ,

    Here is one new problem i am facing now.

    Lets say i have a managed dll in which these two function are defined.

    int Add(int a, int b)

    long Add(long a, long b)

    Now i want to call method add whose arguments are long how can i do it

    I am getting "Ambiguous match found" error  at this line in my code
    MethodInfo mInfo = type.GetMethod(methodename);

    foreach (Type type in assembly.GetTypes())
                    {
                        MethodInfo mInfo = type.GetMethod(methodename);
                        Object obj = Activator.CreateInstance(type);
                        object ret = mInfo.Invoke(obj, arg);                   
                        return ret;
                    }
    Wednesday, May 30, 2007 7:18 PM
  • While "long" and "int" are distinct types to a C++ compiler, they are both 32-bit integer types for a 32-bit compiler.  The .NET marshaler can't decide which one of those functions is a better match, they both can take a System.Int32.  You'll have to drop one.
    Wednesday, May 30, 2007 7:26 PM
  • Hi, Nobugz,

    I got the solution of above problem. basically i have to use

     MethodInfo info = type.GetMethod(methodename,types);                               
                  where types is array of Type

    Still wondering about the Unload problem of DLL.
    Wednesday, May 30, 2007 8:49 PM
  • Just so I'll know: what type do you pass to let it distinguish between "int" and "long"?  You didn't mention an unload problem before.
    Wednesday, May 30, 2007 8:56 PM
  • Hi nobugs,

    Here is the way i create a type array. I ask user the parameter the function needs..
    and using this function i create a Type array.

    static Type CreateType(String type)
            {
                Type abc = null;
                try
                {
                    if (type.Equals("Boolean"))
                        abc = typeof(bool);
                    if (type.Equals("Byte"))
                        abc = typeof(byte);
                    if (type.Equals("Char"))
                        abc = typeof(char);
                    if (type.Equals("Decimal"))
                        abc = typeof(decimal);
                    if (type.Equals("Double"))
                        abc = typeof(double);              
                    if (type.Equals("Int16"))
                        abc = typeof(Int16);
                    if (type.Equals("Int32"))
                        abc = typeof(Int32);
                    if (type.Equals("Int64"))
                        abc = typeof(Int64);
                    if (type.Equals("Object"))
                        abc = typeof(Object);
                    if (type.Equals("SByte"))
                        abc = typeof(SByte);
                    if (type.Equals("Single"))
                        abc = typeof(Single); ;
                    if (type.Equals("String"))
                        abc = typeof(string); ;
                    if (type.Equals("UInt16"))
                        abc = typeof(UInt16);
                    if (type.Equals("UInt32"))
                        abc = typeof(UInt32);
                    if (type.Equals("UInt64"))
                        abc = typeof(UInt64);


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return abc;
            }
    Friday, June 1, 2007 7:14 PM
  • Get List of functions defined in DLL.  What abt Unmanaged Dll..any idea how to get that one crack...
    Monday, June 11, 2007 10:23 PM