C/++ 的简单模块化指南?
我认为模块化是正确的术语;举一个基本的例子,如果我要创建一个加密应用程序,您可以像记事本一样输入它,然后保存加密,但是在保存菜单下,有一些选项可以保存您有插件的加密方法,例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设使用 Win32api,您可以执行以下操作:
现在,当您创建 dll 时,您就拥有了所有插件通用的一组标准函数。或者,每种类型的插件都有一组标准函数,以及一个用您的应用程序标识此插件的函数。这样,您可以测试每个插件的形式是否正确,并动态调用动态库中的方法,而无需将它们编译/链接到主程序中。
该例程在任何支持共享库(DLL、so 等)的平台上都大体相似。
作为我的意思的代码示例,您可能有一个像这样的plugin.h文件:
然后您在主程序和您创建的任何插件中#include此标头。在plugin-dll.cpp(再次示例)中,您有一个这样的方法:
然后您可以在该函数的结果之间切换,并将函数指针分配给您想要运行的正确例程。
有关实施的更多信息:
只是因为,Linux (POSIX) 等效项:
Assuming Win32api, you do something like this:
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:
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:
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:
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.
除了 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.
如果你写的是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)