[英]Extracting Method Names from an unmanaged DLL(VB Dll) through c# Code
我正在编写一个应用程序,其中用户必须浏览要使用的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 DLLs可以正常工作,但是VB DLL可以失败,有没有办法对VB DLL做到这一点。 提前致谢。
Com组件带有类型库,它类似于该特定组件的接口。
您可以使用TypeLibConverter.ConvertTypeLibToAssembly创建互操作,然后以常规方式使用反射。
在msdn上查看示例
您将必须检查dll是com组件还是.net程序集。 下面的链接是一个例子
您正在使用反射,仅在.NET dll上有效。 对于常规的DLL,我认为您正在寻找Win32调用LoadLibrary和GetProcAddress
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.