2012-04-02 52 views
1

我写在用户浏览一个DLL的应用程序的非托管的DLL(VB DLL)提取方法名称中使用。然后,如果该DLL不包含所要求的方法定义一个错误消息将被显示。我用下面的代码:从通过C#代码

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。 在此先感谢。

+0

@Ramhound。 VB 6.0是不受管理的。请刷你的知识 – Ishan 2012-04-05 11:20:35

+0

为了进行管理,必须由*虚拟机/运行时进行解释*。 .NET代码,其中“解释”字节码,并且执行机器代码的CLR运行。 VB6应用程序本质上是COM应用程序。自VB5以来,它们可以编译为本机可执行文件。换句话说,VB6不受管理。 – Will 2012-04-05 15:25:14

回答

0

您使用反射,这将只能在.NET的DLL的工作。对于常规DLL的我认为你正在寻找Win32调用LoadLibraryGetProcAddress