VC++:使用 DLL 作为“子程序”

发布于 2024-10-23 12:49:35 字数 441 浏览 2 评论 0原文

因此,在推迟了多年并且不知道从哪里开始之后,我才开始尝试仿真,并且我成功地编写了我的第一个仿真器!现在我正在组织我的代码,以便我可以重用该代码来模拟其他系统。我一直在考虑拥有一个共享前端“平台处理程序”,我将其编译为我的可执行文件,而我将我的模拟系统代码编译为 dll,平台处理程序将使用它来识别可用的内容并从中实例化。这将使我能够将代码分成不同的项目,并保留使用具有更多功能的笨重前端或简化的“仅限游戏”的选项,并在它们之间共享相同的 dll,而不是制作两个不同的解决方案。

我知道如何编译 dll 与可执行文件,但我不知道如何将可执行文件链接到自定义 dll,以便我可以从中实例化一个类。我什至不确定我想做的事情在技术上是否可行。 dll 类必须是静态的吗?我以前从未编写过类似的代码,甚至从未使用自定义 dll 做过很多工作,因此任何帮助或想法将不胜感激。顺便说一句,我正在使用 Visual C++ 2010。预先感谢任何人可能提出的任何建议。

So I just began to try my hand at emulation after years of putting it off and not knowing where to start and I have managed to successfully write my first emulator! Now I am organizing my code in so that I can reuse the code to emulate other systems. I've been toying with the idea of having a shared frontend "platform handler" of sorts that I will compile as my executable whereas I will compile my emulated system code into dlls that the platform handler will use to identify what is available and instantiate from. This would allow me to separate my code into different projects and to leave the option open of using a bulkier front-end with more features or a streamlined "game only" and to share the same dlls between them rather than make two different solutions.

I know how to compile dlls vs executables but I don't know how to link the executable to the custom dll in such a way that I can instantiate a class from it. I'm not even sure what I'm trying to do is technically possible. Do the dll classes need to be static? I've never coded anything like this before or even done much with custom dlls so any help or ideas would be appreciated. I'm using Visual C++ 2010 by the way. Thanks in advance for any advice anyone may have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2024-10-30 12:49:35

您实际上不必做太多不同的事情。只需像导出函数一样从 dll 中导出类即可。在您的应用程序中,像平常一样包含标头并链接到生成的库。请参阅此页面:http://msdn.microsoft .com/en-us/library/81h27t8c%28v=vs.80%29.aspx

#ifdef DLL_EXPORT
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API __declspec(dllimport)
#endif

class EXPORT_API Example
{
public:
    Example();
    ~Example();

    int SomeMethod();
};

int EXPORT_API ExampleFuncion();

Example.hExample.cpp

#include "Example.h"

Example::Example()
{
    // construct stuff
}

Example::~Example()
{
    // destruct stuff
}

int Example::SomeMethod()
{
    // do stuff
    return 0;
}

int EXPORT_API ExampleFunction()
{
    return 0;
}

您的 dll 项目中,定义 DLL_EXPORT 并构建。您将获得 .lib.dll 输出。在您将使用 dll 的主项目中,除了包含标头和针对 .lib 的链接之外,您无需执行任何操作。不要在主项目中定义 DLL_EXPORT 符号,并确保 .dll 位于您的应用程序可以找到的位置。

如果真想耍点小聪明,这个问题对工厂设计模式来说简直就是尖叫。如果您设计得足够好,您可以让您的 dll 在加载时向您的应用程序注册其实现。您可以永远扩展,甚至无需重建主可执行文件。

You don't really have to do much different. Just export your classes from the dll like you do for functions. In your app, include the header and link to the generated lib like you usually do. See this page: http://msdn.microsoft.com/en-us/library/81h27t8c%28v=vs.80%29.aspx

Example.h

#ifdef DLL_EXPORT
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API __declspec(dllimport)
#endif

class EXPORT_API Example
{
public:
    Example();
    ~Example();

    int SomeMethod();
};

int EXPORT_API ExampleFuncion();

Example.cpp

#include "Example.h"

Example::Example()
{
    // construct stuff
}

Example::~Example()
{
    // destruct stuff
}

int Example::SomeMethod()
{
    // do stuff
    return 0;
}

int EXPORT_API ExampleFunction()
{
    return 0;
}

In your dll project, define DLL_EXPORT and build. You will get a .lib and .dll output. In your main project where you will be using the dll you do not have to do anything except include the header and link against the .lib. Do not define the DLL_EXPORT symbol in your main project and be sure the .dll is somewhere your application can find it.

If you really want to get clever, this problem is screaming for the factory design pattern. If you design your interface well enough, you can have your dlls register their implementation with your application when they are loaded. You can extend forever without even rebuilding your main executable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文