简体   繁体   中英

Extracting Method Names from an unmanaged DLL(VB Dll) through c# Code

I am writing an application in which user has to browse for a dll to be used. Then if that dll does not contain the required method definition an error message is to be shown. I used following code :

    private void CheckDll()
    {
        string dllName;
        string methodName;
        bool isMethodFound = false;
        OpenFileDialog browseFile = new OpenFileDialog();
        browseFile.Filter = "DLL : |*.dll;*.DLL |OCX Files| *.ocx|All File|*.*";
        try
        {
            if (browseFile.ShowDialog() != DialogResult.Cancel)
            {
                methodName = CommonMod.GetMethodName(1);
                dllName = browseFile.FileName;
                Assembly loadedDll = Assembly.LoadFile(dllName);
                foreach (Type memberType in loadedDll.GetTypes())
                {
                    if (memberType.IsClass)
                    {
                        MethodInfo method = memberType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
                        if (method != null)
                        {
                            isMethodFound = true;
                        }

                    }

                }
                if (!isMethodFound)
                {
                    MessageBox.Show(methodName + " method not found in DLL.", "Script Generator", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            Debug.Print(e.Message);

        }
    }
}

}

This works fine with .net DLLs but with VB dll it fails, is there a way to do it for vb DLLs. thanks in advance.

Com components comes with type library which is like an interface for that perticular component.

You could use TypeLibConverter.ConvertTypeLibToAssembly to create a interop and then use reflection in normal way.

See example here on msdn

You will have to check whether the dll is com component or .net assembly though. An example is at following link

How to determine whether a DLL is a managed assembly or native (prevent loading a native dll)?

You are using reflection, which will only work on .NET dll's. For regular DLL's I think you are looking for the Win32 calls LoadLibrary and GetProcAddress

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM