【发布时间】:2014-01-31 16:41:48
【问题描述】:
我创建了一个项目,它使用 2 个 DLL 来相互对抗(一个 DLL 就是一个播放器)。游戏是第一个玩家选择一个数字,第二个玩家选择另一个数字,然后 PlayRound 函数将两个数字进行比较。我不确定如何加载 DLL(运行/加载时间)的问题。我创建了我的第一个 DLL (simple.dll),它有一个 Pick 函数,为简单起见总是返回“int 2”:
#include "stdafx.h"
#define ASEXPORT
#include <iostream>
#include "player.h"
using namespace std;
int Pick(int Round, int MyMoves[], int OpponentMoves[])
{
return 2;
}
这个项目有一个头文件(player.h),代码如下:
#ifndef ASEXPORT
#define DLLIMPORTOREXPORT dllimport
#else
#define DLLIMPORTOREXPORT dllexport
#endif
_declspec(DLLIMPORTOREXPORT) int Pick(int Round, int MyMoves[], int OpponentMoves[]);
不确定在哪里包含此代码,我应该将它包含在 main 中还是函数中:
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
//hinstLib = LoadLibrary(TEXT(player2Name));
hinstLib = LoadLibrary();
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "simple.DLL");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (L"Message sent to the DLL function\n");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// Report any failures
if (! fRunTimeLinkSuccess)
printf("Unable to load DLL or link to functions\n");
if (! fFreeResult)
printf("Unable to unload DLL\n");
//
我希望我说得通俗易懂
【问题讨论】:
-
为什么选择运行时链接。加载时间链接和导入库 (.lib) 更容易
-
@DavidHeffernan
load time linking?我猜你的意思是静态链接或至少编译时链接 -
@Paranaix 不,我的意思是加载时间链接:msdn.microsoft.com/en-us/library/windows/desktop/…
-
@DavidHeffernan 我不介意加载时间或运行时间
-
如果您在加载时执行此操作,那么您将只能拥有一个播放器 DLL。