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

本文关键字:VB-DLL 提取 方法 DLL 代码 通过 | 更新日期: 2023-09-27 18:29:40

我正在编写一个应用程序,用户必须在其中浏览要使用的dll。然后,如果该dll不包含所需的方法定义,则会显示一条错误消息。我使用了以下代码:

    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);
        }
    }
}

}

这在.net dll中工作得很好,但在VB dll中失败了,有没有一种方法可以为VB dll做到这一点。提前谢谢。

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

Com组件附带了类型库,它就像是该特定组件的接口。

您可以使用TypeLibConverter.ConvertTypeLibToAssembly来创建互操作,然后以正常方式使用反射。

请参阅msdn 上的示例

不过,您必须检查dll是com组件还是.net程序集。下面的链接就是一个例子

如何确定DLL是托管程序集还是本机程序集(防止加载本机DLL)?

您正在使用反射,它只能在.NET dll上工作。对于常规DLL,我认为您正在寻找Win32调用LoadLibrary和GetProcAddress