1

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.

sll
  • 61,540
  • 22
  • 104
  • 156
Ishan
  • 67
  • 4
  • @Ramhound . VB 6.0 is unmanaged. Please brush up your knowledge – Ishan Apr 05 '12 at 11:20
  • in order to be managed, it must be *interpreted* by a virtual machine/runtime. .NET code runs in the CLR which "interprets" bytecode and executes machine code. VB6 applications are essentially COM apps. Since VB5, they could be compiled to native executables. In other words, VB6 isn't managed. –  Apr 05 '12 at 15:25

2 Answers2

1

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)?

Community
  • 1
  • 1
Anand
  • 14,545
  • 8
  • 32
  • 44
0

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

huysentruitw
  • 27,376
  • 9
  • 90
  • 133