通过字符串来动态调用函数
来源:百度文库 编辑:神马文学网 时间: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
通过字符串来动态调用函数
通过DB link进行远程过程或函数调用
利用VC调用动态链接库中的函数
字符串函数大全
Delphi字符串函数大全
VB.NET字符串函数
Delphi字符串函数大全
Delphi字符串函数
Sybase字符串函数
SQL字符串函数
通过内核源码看函数调用之前世今生 - 极光 - CSDN博客
通过一个程序分析C++中析构与构造函数的调用顺序
论函数调用约定
函数调用约定
函数调用方式介绍
论函数调用约定
利用VC调用动态链接库中的函数1-开发频道-VC-天极网
截取中文字符串的函数
C语言字符串函数大全
VB.NET字符串函数1
C语言字符串函数大全
SQL字符串处理函数大全
SOA 探索,第 1 部分: 通过动态解耦来简化 Web 服务调用
SOA 探索,第 1 部分: 通过动态解耦来简化 Web 服务调用