Posted on 2007-01-12 15:44
梁海泉 阅读(423)
评论(0) 编辑 收藏 引用
typedef UINT (CALLBACK
*
LPFNDLLFUNC1)(DWORD,UINT);
HINSTANCE hDLL;
//
Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;
//
Function pointer
DWORD dwParam1;
UINT uParam2, uReturnVal;
hDLL
=
LoadLibrary(
"
MyDLL
"
);
if
(hDLL
!=
NULL)
{
lpfnDllFunc1
=
(LPFNDLLFUNC1)GetProcAddress(hDLL,
"
DLLFunc1
"
);
if
(
!
lpfnDllFunc1)
{
//
handle the error
FreeLibrary(hDLL);
return
SOME_ERROR_CODE;
}
else
{
//
call the function
uReturnVal
=
lpfnDllFunc1(dwParam1, uParam2);
}
}
/**/
//////////////////////////////////////////////////////////////////////////////
#include
<
stdio.h
>
#include
<
windows.h
>
typedef
int
(
*
lpAddFun)(
int
,
int
);
//
宏定义函数指针类型
int
main(
int
argc,
char
*
argv[])
{
HINSTANCE hDll;
//
DLL句柄
lpAddFun addFun;
//
函数指针
hDll
=
LoadLibrary(
"
..\\Debug\\dllTest.dll
"
);
if
(hDll
!=
NULL)
{
addFun
=
(lpAddFun)GetProcAddress(hDll,
"
add
"
);
if
(addFun
!=
NULL)
{
int
result
=
addFun(
2
,
3
);
printf(
"
%d
"
, result);
}
FreeLibrary(hDll);
}
return
0
;
}
/**/
////////////////////////////////////////////////////////////////////////
#pragma comment(lib,
"
dllTest.lib
"
)
//
.lib文件中仅仅是关于其对应DLL文件中函数的重定位信息
extern
"
C
"
__declspec(dllimport) add(
int
x,
int
y);
int
main(
int
argc,
char
*
argv[])
{
int
result
=
add(
2
,
3
);
printf(
"
%d
"
,result);
return
0
;
}