Calling function without signature knowledge

Asked

Viewed 113 times

-1

If I have a function called calculator() in C. And I want to call this function, obviously I would call it by name, ie by her signature. But if I don’t know the signature of this function, and its signature is found in a char variable, how could I call this function? ie, take the name of the function stored in the variable of char type, and call the function.

Any suggestions?

Thanks

  • 1

    What is the problem what do you want to solve? More importantly, what is the architecture (processor, operating system, compiler) on which you are working?

  • The problem is this described above, is a program in C language, I need to call a function x, but do not know her name, ie the signature. I will receive the name of this function in a variable y which is a string containing the name of the function, as I could call the function x with this variable y informing me the name of the function?

  • 1

    That’s the solution that you thought for your problem. Rubbing two sticks to make fire is the solution pro problem to make foods more nutritious and tasty; another solution is a microwave oven, which in many circumstances is a more practical and efficient solution. After you can call the function (and catch the result of it), you will use this to do what? If you explain the problem that you want to solve, maybe I can give you a solution that works within the resources and limitations of C.

  • I don’t expect a function result, that doesn’t matter, the only problem is calling the function. It can be a function of type INT that returns 0 only.

2 answers

2


Rafael,

C does not support this type of operation. What you can do is compile a dll with the functions and then search the function by name within the dll.

Example

#include <Windows.h>
HANDLE funcoes;
int (*teste)(int);

funcoes = LoadLibrary("funcoes.dll");

if(funcoes>(void*)HINSTANCE_ERROR){
      teste = GetProcAddress(funcoes, nomedafuncao);
      teste(5);
} 
  • I never programmed DLL’s, but when I pass the function name in Getprocaddress it calls the function directory, or inside the DLL I do a number of IF’s to find the function?

  • The Getprocaddress function returns the address of the function within the DLL by searching directly by name, which can be a variable.

  • In which library is this function declared? Another thing, in linux it is possible to also read these DLL’s?

  • The linux library is dlfcn.h. Yes, but the syntax is different, it will use dlopen to open (uses flags, see documentation) and dlsym to find the function name within the dll. For more information about this linux library please go to this link: http://man7.org/linux/man-pages/man3/dlopen.3.html

1

Tip: Show your code.
I’ll show you mine :-)

#include <stdio.h>
#include <string.h>

typedef int (*fx_t)(int); /* fx_t e ponteiro para funcao que recebe e devolve int */

int fx1(int a) { return a + 1; }
int fx2(int a) { return a + 2; }
int fx3(int a) { return a + 3; }

int main(void) {
    char y[10];
    strcpy(y, "fx2"); /* obtem y do utilizador */

    fx_t x = NULL;
    if (strcmp(y, "fx1") == 0) x = fx1;
    if (strcmp(y, "fx2") == 0) x = fx2;
    if (strcmp(y, "fx3") == 0) x = fx3;
    if (x) printf("%d\n", x(42)); // chama fx1(42) ou fx2(42) ou fx3(42) */
    return 0;
}

You can see the code running on ideone.

  • I understood what you did. But I don’t want to have to make comparisons, checking whether y is such a function. I’m assuming that I don’t know the function names, only the string in y.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.