[code=c]extern "C" int __declspec(dllexport)add(int x, int y);
extern "C" int __declspec(dllexport)*add1();
int add(int x, int y)
{
return x + y;
}
int *add1()
{
static int a[3]={1,2,3};
static int *p;
p=a;
return p;
}
[/code]
---------------------------------------------------------------------------以上为DLL_test.dll文件内容,以下为Main主程序
[code=c]#include
#include
typedef int(*lpAddFun)(int, int); //宏定义函数指针类型
typedef int(*lpAddFun1)();???????????????????这里定义似乎不对,因为add1()返回值为指针,但又不知道正确的是怎么写。请帮忙指点
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
lpAddFun1 addFun1;
hDll = LoadLibrary(L"..\debug\DLL_test.dll");
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll, "add");
addFun1 = (lpAddFun1)GetProcAddress(hDll, "add1");???这里对吗
if (addFun != NULL)
{
int result = addFun(2, 3);
printf("2 + 3 = %d\n", result);
int *k;-----------------------------------这行和下面的一行,也不对,不知道怎么才行。。
k=addFun1();------------------------
printf("add1中的a[1]的结果是: %d\n", *(k+1));
system("pause");
}
FreeLibrary(hDll);
}
return 0;
}[/code]
你想复杂了,返回的是指针,又不是函数指针,k=addFun1();就可以了。
但是你的add函数本身有缺陷,它返回的是堆栈上变量的地址,这是不允许的。
返回指针,你直接用一个对应类型变量接受处理就可以了。你的static类型只有保证使用的时候还有效就可以了
还是不太明白,最好能直接给出修改后的代码。谢谢