無論是建立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 <windows.h>
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中的函數(shù)調用完全利用的是地址
想要以函數(shù)名調用必須從字符入手,映射到函數(shù)地址,如:
#include <stdio.h>
#include <string.h>
#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<N;i++)
{
if( !strcmp(fnm,fun_nm[i]) )/*函數(shù)名匹配*/
{
fun[i]();
}
}
return 0;
}
程序輸出:
D:\>tp
fun1
Function 1 is called
*************************************************************************
#include "stdafx.h"
#include <map>
#include <windows.h>
#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); // 定義函數(shù)類型變量
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;
// 定義函數(shù)入口結構
class ClsTest{
// 定義了兩個原型不同的函數(shù)
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*[]);
// 定義函數(shù)映射表
struct cls_fun_entry
{
const char* fun_name; // 函數(shù)名稱
CLS_FUN_PTR fun_ptr; // 函數(shù)指針,實際上這里的數(shù)據(jù)類型也可以是整型
};
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"); // 獲得函數(shù)入口地址,并轉換成函數(shù)指針
if( pfoo1 ) (ct.*pfoo1)(0, 0); // 通過函數(shù)指針調用函數(shù)
//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; // 函數(shù)名稱
FUN_PTR fun_ptr; // 函數(shù)指針,實際上這里的數(shù)據(jù)類型也可以是整型
};
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"); // 獲得函數(shù)入口地址,并轉換成函數(shù)指針
if( pfoo1 ) pfoo1(); // 通過函數(shù)指針調用函數(shù)
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 <iostream>
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<<retValue;
return 1;
}
#endif