I am pretty new to .dll stuffs, however, I have been assigned a task in which I have to call a method from .dll file using C#. I am completely new to these stuffs. So can someone point me in the right direction regarding this. Thanks
-
1Add reference to the .dll file from within Visual Studio by right clicking References in the proper project then use its namespace. You will even have auto complete. This of course assuming that DLL was compiled by .NET framework. (Doesn't have to be C#)– Shadow The Kid WizardNov 5, 2012 at 12:50
-
Does the DLL need to be loaded dynamically or can it be loaded with the project?– JamesNov 5, 2012 at 12:50
-
3why type of dll? activex, com? if none of these, than try with pinvoke msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx– cichyNov 5, 2012 at 12:51
-
@cichy that one is better msdn.microsoft.com/en-us/magazine/cc164123.aspx– t3hn00bNov 5, 2012 at 12:51
-
1Is it a .NET dll or a dll created using some native language like C or C++?– S. Ravi KiranNov 5, 2012 at 12:51
2 Answers
If you are using a .NET dll, Load the DLL via Assembly.Load (or Assembly.LoadFrom) and then call Assembly.GetTypes and then for each type call Type.GetMethods. When you have a MethodInfo, you can call MethodInfo.Invoke on it.
Assuming it's a native non-net dll like kernel32.dll:
using System.Runtime.InteropServices;
...
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
Then just call e.g. AttachConsole like a normal Function.
-
-
If its a dll which can be reached by path: no. If its a dll located elsewhere, then either path needs to be adjusted or dll copied. If using system dll's, read something about "dll-hell" to learn about some problems with those...– pbhdNov 5, 2012 at 15:06