How do we call methods in DLL's using a string for the method name?
-
In C or C++ we used to be able to call functions from DLL's by loading the dll library (Ex: LoadLibrary("library.dll");) then using the method GetProcAddress("functionName") to find a function by name in the library.dll. Now in C# the Dll's contain classes rather than isolated functions. Is is possible to call functions from Dll's in c#? I am mainly interested in being able to pass a function name as parameter then use something like the GetProcAddress method that uses that function name to find it in the dll so we can call it. Example in C++: //this method will call a function from the library (no error checks to simplify) void CallFunction(char* libraryName, char* functionName) { //declare the function that will be called: typedef void * (FUNC)(int); //function that returns void and takes an int FUNC pFunction; //load dll library: HINSTANCE hLibrary = LoadLibrary("Library.dll"); //find function in library: pFunction = (FUNC) GetProcAddress(hLibrary, functionName); //call function pFunction(35); }
-
In C or C++ we used to be able to call functions from DLL's by loading the dll library (Ex: LoadLibrary("library.dll");) then using the method GetProcAddress("functionName") to find a function by name in the library.dll. Now in C# the Dll's contain classes rather than isolated functions. Is is possible to call functions from Dll's in c#? I am mainly interested in being able to pass a function name as parameter then use something like the GetProcAddress method that uses that function name to find it in the dll so we can call it. Example in C++: //this method will call a function from the library (no error checks to simplify) void CallFunction(char* libraryName, char* functionName) { //declare the function that will be called: typedef void * (FUNC)(int); //function that returns void and takes an int FUNC pFunction; //load dll library: HINSTANCE hLibrary = LoadLibrary("Library.dll"); //find function in library: pFunction = (FUNC) GetProcAddress(hLibrary, functionName); //call function pFunction(35); }
-
Got it! Thanks :-D Here is a link where I found a good example in msdn web site in case someone else needs it: [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet02262002.asp](Mirror, Mirror (a vb reflection example))