472,103 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,103 software developers and data experts.

Is it possible to call a function which name is given by a string?

Is it possible to call a function which name is given by a string?

Let assume that I created a program which call some functions for example

void f1(void),
void f2(void),
void f3(void).

After some time, I added new function void f4(void).

However my previous program doesn't know that the function f4 exist.
It is possible to call this function without recompiling the whole program?

It is possible to create a procedure, lets say

void CallFunctionByName(char *FunctionName);

which will be called function which is added to the program in the future?

For example the instruction

CallFunctionByName("f4");

Will call the function "f4", if the function is not linked by the program
then error message will be given.

Is it possible to do that in C++?

Regards,

Andrzej
Jul 23 '05 #1
7 2176
well, i think a function pointer could do that

Jul 23 '05 #2
On 2005-05-23, Andrzej <cp***************@test.com> wrote:
Is it possible to call a function which name is given by a string?

Let assume that I created a program which call some functions for example

void f1(void),
void f2(void),
void f3(void).

After some time, I added new function void f4(void). However my previous program doesn't know that the function f4 exist.
It is possible to call this function without recompiling the whole program?


You can call functions indirectly by using function pointers. Not sure if
that solves your problem or not. Most platforms support dynamic loading --
which makes it possible to load libraries at run time. Further discussion
of this is off-topic here (see your OS newsgroup), but this might be an
option worth pursuing.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3
Andrzej wrote:

Is it possible to call a function which name is given by a string?


Not directly.
But function pointers open up a way:
Create a table which maps a functions name to a function pointer.

Short scetch:

#include <iostream>
#include <string>
#include <map>

typedef void (*FnctPtr) (void);
typedef std::map< std::string, FnctPtr > LookUpTable;

class Dispatcher
{
public:
void Register( const std::string& Name, FnctPtr Function );
void Dispatch( const std::string& Name );

private:
LookUpTable m_Functions;
};

void Dispatcher::Register( const std::string& Name, FnctPtr Function )
{
m_Functions[Name] = Function;
}

void Dispatcher::Dispatch( const std::string& Name )
{
LookUpTable::iterator it = m_Functions.find( Name );
if( it != m_Functions.end() )
it->second();
else
std::cout << "Error: Function \'" << Name << "\' not in dispatch table\n";
}

void f1()
{
std::cout << "This is f1\n";
}

void f2()
{
std::cout << "This is f2\n";
}

int main()
{
Dispatcher Disp;

Disp.Register( "f1", f1 );
Disp.Register( "f2", f2 );

Disp.Dispatch( "f1" );
Disp.Dispatch( "f2" );
Disp.Dispatch( "f3" );

return 0;
}
--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 23 '05 #4
I agree with Teddy that a function pointer is a nice way to solve this
problem of yours.

First of all you would need some kind of "function registry" and a
function which your new functions can call to register themselves.
It might look something like this:

std::map<std::string, void*) registry;

void registerFunction(std::string functionName, void* fP)
{
registry[functionName] = fP;
}

And your callFunctionByName would then look like:

void callFunctionByName(std::string functionName)
{
void (*fx)() = registry[functionName];
fx();
// Throw some exceptions if function is not registred...
}

And here goes an example which shows how to use it:

void exampleFunction() { // Do something good }

int main()
{
registerFunction("example", &exampleFunction);
callFunctionByName("example);
}

Hope this helps
// eKIK

P.S.
This code is straight out of my head, so it probably won't work
cut-n-paste style...but hopefully the basics are right at least.
D.S.

Jul 23 '05 #5
> Is it possible to call a function which name is given by a string?

Maybe, but you really don't want to do this unless there is absolutely
no alternative solution to your problem. Just not wanting to recompile
the whole program is not a good enough reason to resort to this, IMHO.

On Linux you can use h=dlopen(NULL) followed by dlsym(h,"funcname").
The key thing is that passing NULL means to look up the symbol in the
main program. Cast the result of dlsym to a function pointer and then
call it. Similar things are possible on other Unix variants though not
all support passing NULL to dlopen. On Windows there is something
called LoadLibrary which does some of what dlopen does, but I don't
know if it has a way to access the main program's symbols.

For a bit more info, have a look at
http://www.isotton.com/howtos/C++-dl...ini-HOWTO.html

--Phil.

Jul 23 '05 #6
>> Is it possible to call a function which name is given by a string?
on Windows there is something called LoadLibrary


You need GetModuleHandle(NULL) followed by GetProcAddress("function
name")

Jul 23 '05 #7
Thank you very much.

Andrzej
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

36 posts views Thread by rbt | last post: by
13 posts views Thread by Alison Givens | last post: by
11 posts views Thread by vbgunz | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.