简体   繁体   English

通过c#代码从非托管DLL(VB Dll)中提取方法名称

[英]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. 我正在编写一个应用程序,其中用户必须浏览要使用的dll。 Then if that dll does not contain the required method definition an error message is to be shown. 然后,如果该dll不包含所需的方法定义,将显示一条错误消息。 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. NET DLLs可以正常工作,但是VB DLL可以失败,有没有办法对VB DLL做到这一点。 thanks in advance. 提前致谢。

Com components comes with type library which is like an interface for that perticular component. Com组件带有类型库,它类似于该特定组件的接口。

You could use TypeLibConverter.ConvertTypeLibToAssembly to create a interop and then use reflection in normal way. 您可以使用TypeLibConverter.ConvertTypeLibToAssembly创建互操作,然后以常规方式使用反射。

See example here on msdn msdn上查看示例

You will have to check whether the dll is com component or .net assembly though. 您将必须检查dll是com组件还是.net程序集。 An example is at following link 下面的链接是一个例子

How to determine whether a DLL is a managed assembly or native (prevent loading a native dll)? 如何确定DLL是托管程序集还是本地程序集(防止加载本地dll)?

You are using reflection, which will only work on .NET dll's. 您正在使用反射,仅在.NET dll上有效。 For regular DLL's I think you are looking for the Win32 calls LoadLibrary and GetProcAddress 对于常规的DLL,我认为您正在寻找Win32调用LoadLibraryGetProcAddress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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