dll.cpp
extern "C" _declspec(dllexport) int MyFunction(int x);
// CDLL7App 初始化
BOOL CDLL7App::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
extern "C" _declspec(dllexport) int MyFunction(int x)
{
return x * 10;
}
dll.def
EXPORTS
MyFunction @1
call
typedef int (* MyFunction)(int);
typedef int (CALLBACK* fndll3)(void);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败/n"));
nRetCode = 1;
}
else
{
HINSTANCE hDLL; // Handle to DLL
MyFunction lpfnDllFunc1; // Function pointer
double uParam2, uReturnVal;
hDLL = LoadLibrary(_T("dll7.dll"));
if (hDLL != NULL)
{
lpfnDllFunc1 = (MyFunction)GetProcAddress(hDLL,
("MyFunction"));
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
return -1;
}
else
{
// call the function
uReturnVal = lpfnDllFunc1(113);
printf("%f/r/n", uReturnVal);
}
}
}
return nRetCode;
}