Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How Can i Call a Function Providing its name as a string

How Can i Call a Function Providing its name as a string

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MIAN KAMRAN
    wrote on last edited by
    #1

    How Can i Call a Function Providing its name as a string in C\C++

    R C F C V 7 Replies Last reply
    0
    • M MIAN KAMRAN

      How Can i Call a Function Providing its name as a string in C\C++

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M MIAN KAMRAN

        How Can i Call a Function Providing its name as a string in C\C++

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • M MIAN KAMRAN

          How Can i Call a Function Providing its name as a string in C\C++

          F Offline
          F Offline
          Frank K
          wrote on last edited by
          #4

          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.dll typedef 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

          1 Reply Last reply
          0
          • M MIAN KAMRAN

            How Can i Call a Function Providing its name as a string in C\C++

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            you can't, not directly anyway. but, you could create a map that maps strings (names) to function pointers.

            image processing | blogging

            1 Reply Last reply
            0
            • M MIAN KAMRAN

              How Can i Call a Function Providing its name as a string in C\C++

              V Offline
              V Offline
              Viorel
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • M MIAN KAMRAN

                How Can i Call a Function Providing its name as a string in C\C++

                Y Offline
                Y Offline
                yjzh
                wrote on last edited by
                #7

                :laugh:a piece of cake, function pointer or functors can do this. boost

                1 Reply Last reply
                0
                • M MIAN KAMRAN

                  How Can i Call a Function Providing its name as a string in C\C++

                  R Offline
                  R Offline
                  Rilhas
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups