C/++ 的简单模块化指南?

发布于 2024-08-21 17:47:31 字数 290 浏览 10 评论 0原文

我认为模块化是正确的术语;举一个基本的例子,如果我要创建一个加密应用程序,您可以像记事本一样输入它,然后保存加密,但是在保存菜单下,有一些选项可以保存您有插件的加密方法,例如 AES、Blowfish 等,并且还允许将新方法编码到插件中并分发,而无需重新编译主应用程序。

我在网上找到了一些解释,但我主要是在努力弄清楚如何让新选项出现在最初不存在的保存菜单下(这可能更多是一个 Windows 应用程序问题),如果您明白我的意思。

鉴于模块化开发似乎非常特定于平台,我现在将继续使用 Windows 示例,并希望在此之后尝试确定范围。

I think modular is the correct term; to give a basic example if I was to create an encryption application which you could type in like notepad, and then save encrypted, however under the save menu there are options to save for the encryption methods that you have plugins for like AES, Blowfish etc, and also allow new methods to be coded into a plugin and distributed without having to recompile the main application.

I've found a couple of explanations online but I'm mostly struggling to get my head around how you would get new options to appear under the save menu that originally didn't exist (this maybe more a Windows application question), if you get what I mean.

Seeing as modular development seems to be very platform specific I'll stick with Windows examples for now and hopefully try and scope out after that.

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

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

发布评论

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

评论(4

情魔剑神 2024-08-28 17:47:31

假设使用 Win32api,您可以执行以下操作:

  • 为您的应用程序创建一个插件目录。
  • 加载应用程序时,列出该目录中的所有文件
  • 任何具有扩展名 DLL 的文件,都可以使用 LoadLibrary 调用。
  • 您从 dll 中获取一些信息,告诉您插件的名称是什么。
  • 您可以适当地创建菜单/ui 更改。

现在,当您创建 dll 时,您就拥有了所有插件通用的一组标准函数。或者,每种类型的插件都有一组标准函数,以及一个用您的应用程序标识此插件的函数。这样,您可以测试每个插件的形式是否正确,并动态调用动态库中的方法,而无需将它们编译/链接到主程序中。

该例程在任何支持共享库(DLL、so 等)的平台上都大体相似。

作为我的意思的代码示例,您可能有一个像这样的plugin.h文件:

#ifndef PLUGIN_H_
#define PLUGIN_H_

#define PLUGIN_CRYPTO   1
#define PLUGIN_FORMAT   2
#define PLUGIN_EXAMPLE  3

#endif 

然后您在主程序和您创建的任何插件中#include此标头。在plugin-dll.cpp(再次示例)中,您有一个这样的方法:

int GetPluginType()
{
    return PLUGIN_CRYPTO;
}

然后您可以在该函数的结果之间切换,并将函数指针分配给您想要运行的正确例程。

有关实施的更多信息:

只是因为,Linux (POSIX) 等效项:

  • dlopen - 动态库打开。
  • dlsym - 相当于 GetProcAddress - 获取函数 ptr 到符号名称。
  • dlclose - 相当于 FreeLibrary

Assuming Win32api, you do something like this:

  • Have a plugins directory for your application.
  • On load of your application, list all files in that directory
  • Any with the extension DLL, you load with the LoadLibrary call.
  • You get some information from the dll that tells you what the plugin's name is
  • You create menus/ui changes appropriately.

Now, when you create your dll, you have a standard set of functions common to all plugins. Or, a standard set of functions per type of plugin and a function that identifies this with your application. This way, you can test each plugin is of the correct form and call the methods in the dynamic library on the fly without having to compile / link them in to your main program.

The routine is broadly similar on any platform that supports shared libraries (DLLs, so's etc).

As a code example for what I mean, you might have a plugin.h file like this:

#ifndef PLUGIN_H_
#define PLUGIN_H_

#define PLUGIN_CRYPTO   1
#define PLUGIN_FORMAT   2
#define PLUGIN_EXAMPLE  3

#endif 

Then you #include this header in both your main program and any plugins you create. In plugin-dll.cpp (example again) you have a method like this:

int GetPluginType()
{
    return PLUGIN_CRYPTO;
}

Then you can switch between the results of this function and assign function pointers to the correct routines you want to run.

More info for implenentation:

Just because, Linux (POSIX) equivalents:

  • dlopen - Dynamic library open.
  • dlsym - Equivalent to GetProcAddress - get function ptr to symbol name.
  • dlclose - Equivalent to FreeLibrary
本王不退位尔等都是臣 2024-08-28 17:47:31

Windows 包含一个名为ModifyMenu 的函数,可让您在运行时插入、删除和重新排列菜单项。更困难(虽然不是很多)的部分是将事物连接起来,以便菜单条目实际上调用附加功能。

当您选择菜单项时,一条包含特定号码的消息将发送到程序。传统的 C 程序将有一个很大的 switch 语句来根据该数字决定要做什么。对于在运行时添加的插件,您不能使用 switch 语句,因此您通常会使用某种映射结构。

Windows includes a function named ModifyMenu that will let you insert, delete and rearrange menu entries at run-time. The more difficult (though not a lot more difficult) part is connecting things up so the menu entry actually invokes the added-on functionality.

When you select a menu item, a message containing a particular number is sent to the program. A conventional C program will have a big switch statement to decide what to do based on that number. For a plug-in that's added on at run-time you can't use a switch statement, so instead you typically use some sort of map structure instead.

相对绾红妆 2024-08-28 17:47:31

除了 Jerry 在运行时填充菜单的解释之外,您可能还需要扫描一组文件夹(例如应用程序文件夹\插件)以查找新的 dll 文件,这些文件将提供某些功能,例如加密/解密和插件名称等。Windows 具有以下功能:在运行时查找 DLL 文件中的内容。

Along with Jerry's explanation of populating the menu at runtime, you will probably have to scan a set folder (say Application Folder\plugins) for new dll files which will provide certain functions, like encrypt/decrypt and plugin_name, etc. Windows has facilities for looking stuff up in DLL files at runtime.

雨后彩虹 2024-08-28 17:47:31

如果你写的是c#,你可以使用MEF http://www.codeplex.com/MEF

你真的应该用 C# 来做,只有真正的受虐狂才会用 C++ 写这种 GUI 客户端的东西(等待火焰)

if you write this is c# the you can use MEF http://www.codeplex.com/MEF

and you really should do it in C#, only real masochists would write this kind of GUI client thing in C++ (standing by for flaming)

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