通过字符串来动态调用函数

来源:百度文库 编辑:神马文学网 时间:2024/04/24 05:07:47

无论是建立WIN32   Application   还是WIN32   Console   Applicate   都能正常运行。
下面是建立WIN32   Console   project   的例子,如果还不行,把email留给我,我把我的project   file   给你。
1.VC   ->   new   WIN32   Console   Application   ->   选   "Hello,World"   application
2.把主文件的内容置换成以下内容:
#include   "stdafx.h"
#include  
extern   "C"
__declspec(dllexport)     int   test1(void)
{
return   100;
}
extern   "C"
__declspec(dllexport)     int   test2(void)
{
return   200;
}
typedef   int   (*   FUNC_TYPE)(void);
int   call_func(const   char*   name)
{
HMODULE   hDLL   =   GetModuleHandle(NULL);
FUNC_TYPE   my_func;         //   Function   pointer
if   (hDLL   !=   NULL)
{
my_func   =   (FUNC_TYPE)GetProcAddress(hDLL,name);
if   (!my_func)
{
//   handle   the   error
return   NULL;
}
else
{
//   call   the   function
return   my_func();
}
}
return   NULL;
}
int   main(int   argc,   char*   argv[])
{
int   i   =   call_func("test1");
printf("call   test1:   %d\n",i);
i   =   call_func("test2");
printf("call   test2:   %d\n",i);
return   0;
}
/**************************************************************/
c中的函数调用完全利用的是地址
想要以函数名调用必须从字符入手,映射到函数地址,如:
#include  
#include  
#define   N   3
typedef   void   (*fun_t)(void);
void   fun1(void)
{
puts("Function   1   is   called");
}
void   fun2(void)
{
puts("Function   2   is   called");
}
void   fun3(void)
{
puts("Function   3   is   called");
}
int   main(void)
{
int   i;
fun_t   fun[N];
char   fnm[256];
char   *fun_nm[N]={"fun1","fun2","fun3"};
fun[0]   =   fun1;
fun[1]   =   fun2;
fun[2]   =   fun3;
scanf("%255s",fnm);
for(i=0;i{
if(   !strcmp(fnm,fun_nm[i])   )/*函数名匹配*/
{
fun[i]();
}
}
return   0;
}
程序输出:
D:\>tp
fun1
Function   1   is   called
*************************************************************************
#include "stdafx.h"
#include
#include
#define CLS_PIONT 1
#ifdef C_EXTERN
extern "C"
__declspec(dllexport) int test1(void)
{
return 100;
}
extern "C"
__declspec(dllexport) int test2(void)
{
return 200;
}
typedef int (* FUNC_TYPE)(void);
int call_func(const char* name)
{
HMODULE hDLL = GetModuleHandle(NULL);
FUNC_TYPE my_func;   // Function pointer
if (hDLL != NULL)
{
my_func = (FUNC_TYPE)GetProcAddress(hDLL,name);
if (!my_func)
{
// handle the error
return NULL;
}
else
{
// call the function
return my_func();
}
}
return NULL;
}
int main(int argc, char* argv[])
{
int i = call_func("test1");
printf("call test1: %d\n",i);
i = call_func("test2");
printf("call test2: %d\n",i);
return 0;
}
#elif FUN_CLASS
typedef double (*MyType)(double) ;
class CNewtonIterative
{
private:
double x, x0, x01;
double (* f)(double); // 定义函数类型变量
double (* fp)(double);
public:
CNewtonIterative(double (* _f)(double), double _x0, double _x01)
: f(_f), x0(_x0), x01(_x01) {};
~CNewtonIterative() {};
CNewtonIterative& setF(double (* _f)(double)) { f = _f; };
MyType getF() const { return f; };
};
double ft(double x)
{
return x * x - 2;
};
int main(int argc, char* argv[])
{
CNewtonIterative cni(ft, 470, 475);
MyType p=cni.getF();
double x=p(10.0);
printf("Hello World! x=%f\n",x);
return 0;
}
#elif CLS_PIONT
using namespace std;
// 定义函数入口结构
class ClsTest{
// 定义了两个原型不同的函数
public:
ClsTest(){};
void foo1(int n, char* a[]) { cout << "foo1" << endl; }
int foo2(int n, char* a[]) { cout << "foo2:" << n << endl; return 0; }
};
typedef void (ClsTest::*CLS_FUN_PTR)(int, char*[]);
// 定义函数映射表
struct cls_fun_entry
{
const char* fun_name; // 函数名称
CLS_FUN_PTR fun_ptr;  // 函数指针,实际上这里的数据类型也可以是整型
};
cls_fun_entry cls_fun_entry_table[] =
{
{ "foo1", (CLS_FUN_PTR)(&ClsTest::foo1) },
{ "foo2", (CLS_FUN_PTR)(&ClsTest::foo2) }
};
CLS_FUN_PTR get_cls_proc_address(const char* fun_name)
{
int n = sizeof(cls_fun_entry_table)/sizeof(cls_fun_entry_table[0]);
for(int i=0; i < sizeof(cls_fun_entry_table)/sizeof(cls_fun_entry_table[0]); i++)
{
if(strcmp(fun_name, cls_fun_entry_table[i].fun_name) == 0)
return cls_fun_entry_table[i].fun_ptr;
}
return NULL;
}
int main()
{
ClsTest ct;
CLS_FUN_PTR pfoo1 = (CLS_FUN_PTR)get_cls_proc_address("foo1"); // 获得函数入口地址,并转换成函数指针
if( pfoo1 ) (ct.*pfoo1)(0, 0); // 通过函数指针调用函数
//typedef int (ClsTest::*funPtr2)(int);
CLS_FUN_PTR pfoo2 = (CLS_FUN_PTR)get_cls_proc_address("foo2");
char * sa[5];
sa[0] = new char[128];
sa[2] = new char[128];
sa[0] = "asss";
if( pfoo2 ) (ct.*pfoo2)( 5, sa );
return 0;
}
/*************************************************/
typedef void (*FUN_PTR)(void);
struct fun_entry
{
const char* fun_name; // 函数名称
FUN_PTR fun_ptr; // 函数指针,实际上这里的数据类型也可以是整型
};
void foo1() { cout << "foo1" << endl; }
int foo2(int i) { cout << "foo2:" << i << endl; return 0; }
fun_entry fun_entry_table[] =
{
{ "foo1", (FUN_PTR)foo1 },
{ "foo2", (FUN_PTR)foo2 }
};
FUN_PTR get_proc_address(const char* fun_name)
{
int n = sizeof(fun_entry_table)/sizeof(fun_entry_table[0]);
for(int i=0; i < sizeof(fun_entry_table)/sizeof(fun_entry_table[0]); i++)
{
if(strcmp(fun_name, fun_entry_table[i].fun_name) == 0)
return fun_entry_table[i].fun_ptr;
}
return NULL;
}
int main2()
{
typedef void (*foo1_ptr)(void);
typedef int (*foo2_ptr)(int);
foo1_ptr pfoo1 = (foo1_ptr)get_proc_address("foo1"); // 获得函数入口地址,并转换成函数指针
if( pfoo1 ) pfoo1(); // 通过函数指针调用函数
foo2_ptr pfoo2 = (foo2_ptr)get_proc_address("foo2");
if( pfoo2 ) pfoo2( 100 );
//tmain();
system("pause");
return 0;
}
//
//class CA
//{
//public:
// char lcFun(int a){ return a; }
//};
//
//typedef char (CA::*PTRFUN)(int);
//
//
//void tmain()
//{
// CA ca;
// PTRFUN pFun = (PTRFUN)(&CA::lcFun);
// char c = (ca.*pFun)(2);
//}
#else
#include
class A
{
public:
A(){};
virtual ~A(){};
};
class B : public A
{
public:
B(){};
~B(){};
int Fb(int a){ return a*a;}
};
typedef int (A::*Myfunction)(int);
int main()
{
B* pB = new B;
Myfunction pf = (Myfunction)(&B::Fb);
int retValue = (pB->*pf)(3);
std::cout<return 1;
}
#endif