这是我在运行我正在使用 DLL 的项目时收到的错误:
奇怪的是,这曾一度奏效。我从这个项目中休息了一段时间,现在它不起作用了。除了更改几个参数外,没有太大变化。
我的设置包括一个我在其中构建 DLL 的项目。然后这个项目与我用来测试它的另一个项目一起用于解决方案。我按照这个例子:https://msdn.microsoft.com/en-us/library/ms235636.aspx其中我也是第一次关注,一直在运行,现在已经停止了。
在意识到它似乎只是导致问题的函数之一后,我删除了所有额外的代码,尝试重命名该函数,删除其中的所有内容,但它仍然无法正常工作。
你可以在下面看到函数定义和签名,看看我是如何尝试让它工作的
我也尝试过使用我在函数而不是类上创建的“SCOREINTERFACECPP”宏,但我得到了同样的错误。
在我正在测试的项目中,我添加了 DLL 项目作为引用和依赖项目,然后导入了头文件。我在 dll 中拥有的其他功能(为了简单起见,我已从该代码中删除了这些功能)似乎可以正常工作。
标题:
#ifdef SCOREINTERFACECPP_EXPORTS
#define SCOREINTERFACECPP __declspec(dllexport)
#else
#define SCOREINTERFACECPP __declspec(dllimport)
#endif
#include <time.h>
#include <queue>
namespace ScoreInterfaceCPP
{
class SCOREINTERFACECPP ScoreInterface
{
public:
ScoreInterface();
~ScoreInterface();
static void SubmitLogin(const std::string &displayName, const std::string &password);
static void Shutdown();
static SIEvent* GetNextEvent();
static void ClearEvents();
static int GetEventCount();
private:
static std::queue< SIEvent* > mSIEvents;
static bool mGameIsAuthorized;
static std::string mGameName;
static std::string hexedKey;
static std::wstring mAddress;
static void SubmitEventString(std::string eventString);
static int SubmitWithNewThread(void* data);
static void PostMessage(std::string data, std::string iv);
};
}
来源:
#include <sstream>
#include <SDL/SDL_thread.h>
#include <boost/tokenizer.hpp>
#include "ScoreInterfaceCPP.h"
#include "Network.h"
using namespace ScoreInterfaceCPP;
/*
ScoreInterfaceCPP.h
Handles the sending and receiving of events.
*/
ScoreInterface::ScoreInterface()
{
}
ScoreInterface::~ScoreInterface()
{
}
void ScoreInterface::SubmitLogin(const std::string &displayName, const std::string &password)
{
}
void ScoreInterface::SubmitEventString(std::string eventString)
{
}
int ScoreInterface::SubmitWithNewThread(void* data)
{
return 0;
}
SIEvent* ScoreInterface::GetNextEvent()
{
return NULL;
}
int ScoreInterface::GetEventCount()
{
return 0;
}
void ScoreInterface::ClearEvents()
{
}
void ScoreInterface::Shutdown()
{
}
测试文件:
#include "ScoreInterfaceCPP.h"
using namespace ScoreInterfaceCPP;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ScoreInterface si = ScoreInterface();
si.SubmitLogin("noplayer", "nopassword");
return 0;
}
最佳答案
根据我的经验,这种类型的问题通常伴随着您应该检查的两件事(假设 DLL 已成功构建):
- 检查运行时加载的 DLL 版本是否正确。
- 确保所讨论的功能确实已导出。
对于第一个问题,您可以使用 Process Explorer 等实用程序并查看为您正在运行的可执行文件加载的 DLL 句柄。如果您使用的是 Visual C++,您还可以查看加载的 DLL 的 Output Window
列表,并确保加载的是您正在使用的版本。
很多时候在开发过程中,您可能有多个(意外或设计)版本的 DLL 位于 Windows 可访问的目录中(请参阅 DLL Search Order ),因此是旧版本或不同版本的 DLL当您运行您的应用程序时正在加载。
对于第二个问题,有dumpbin.exe
,但我找到了Dependency Walker使用起来更友好一点。这些实用程序将向您显示从 DLL 导出的函数。
如果发现函数没有被导出,那么您需要重建您的 DLL,确保 __declspec(dllexport)
已被用于您正在导出的函数或类。
关于c++ - 在 C++ 中构建和使用 DLL 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32928889/