How Can i Call a Function Providing its name as a string
-
How Can i Call a Function Providing its name as a string in C\C++
-
How Can i Call a Function Providing its name as a string in C\C++
Name as a string? Can you be more specific? I do not get this question. Brahmma
According to the laws of aerodynamics, the bumble bee must not be able to fly. The bumble bee does not know aerodynamics. It goes on flying anyway.
-
How Can i Call a Function Providing its name as a string in C\C++
Simple answer: you can't. But, why would like to do such a thing ? Provide as much relevant information you can so we can help you to find a better solution (it probably results from a bad design).
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
How Can i Call a Function Providing its name as a string in C\C++
Hi, Function as a string?? Why? ... but ... You can get the address of a function with
FARPROC GetProcAddress(HMODULE hModule, LPCSTR lpProcName);
You need the module handle and the name of the function. Example for the function GetMenu from user32.dlltypedef HMENU (WINAPI *GETMENU) (HWND); GETMENU FuncGetMenu; HMODULE hUser32Dll; _hUser32Dll = ::LoadLibrary("user32.dll"); if(NULL != hUser32Dll) { FuncGetMenu = (GETMENU)::GetProcAddress(hUser32Dll, "GetMenu"); } if(NULL != FuncGetMenu) { FuncGetMenu(WindowHandle); //HMENU GetMenu(HWND hWnd); }
This is an example for dynamic load a dll. --> You need more than only the function name! You also need the function declaration. HTH Frank -
How Can i Call a Function Providing its name as a string in C\C++
you can't, not directly anyway. but, you could create a map that maps strings (names) to function pointers.
-
How Can i Call a Function Providing its name as a string in C\C++
The next sample for Visual Studio 2005 uses map and function pointers:
#include "stdafx.h" #include "math.h" #include <map> #include <string> void main() { using namespace std; typedef double (* FUNCTION_POINTER)(double); // make the map map< string, FUNCTION_POINTER > functions; functions["sin"] = sin; functions["cos"] = cos; functions["tg"] = tan; // test: call a function by name std::string name; name = "cos"; FUNCTION_POINTER function = functions[name]; double result = function(1.2); }
I hope it works and gives you some ideas.
-
How Can i Call a Function Providing its name as a string in C\C++
-
How Can i Call a Function Providing its name as a string in C\C++
Complementing to the other answers already given, the quick answer would be no, at least not in the way BASIC (for example) lets you do it (something like execute("a=myfunc(10,20)"). The process of calling functions based on the contents of a string has to be manually coded in some way. The most "automatic" way is the already mentioned "GetProcAddress" (you can hide most of the steps required inside macros or in a general purpose function), but prototypes will be always a pain (unless if they are, trivially, the same). Depending on the extent of work you are willing to do, you can go as far as building your own C/C++ interpreter. I did this once, for a home automation system, where I made a program to go through all of my header files, scan function names and prototypes, and write code to execute the correct function with the correct parameters based on the contents of a string (the command entered by the user). Is not very hard work if you control the classes and functions. If not then be prepared for trouble. I hope this helps, Rilhas