diff --git a/PluginDemo/PluginDemo.h b/PluginDemo/PluginDemo.h new file mode 100644 index 000000000..203188010 --- /dev/null +++ b/PluginDemo/PluginDemo.h @@ -0,0 +1,13 @@ +#pragma once +#include "PluginInterface.h" + +#ifdef __cplusplus +extern "C" { +#endif + + __declspec(dllexport) int TMPluginGetItemNum(); + __declspec(dllexport) IPluginItem* TMPluginCreateInstance(int index); + +#ifdef __cplusplus +} +#endif diff --git a/PluginDemo/PluginDemo.vcxproj b/PluginDemo/PluginDemo.vcxproj new file mode 100644 index 000000000..157a09ffd --- /dev/null +++ b/PluginDemo/PluginDemo.vcxproj @@ -0,0 +1,182 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {d1ca3ecc-dc32-445a-b734-c4db08d4ba34} + PluginDemo + 10.0 + + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + DynamicLibrary + true + v142 + Unicode + + + DynamicLibrary + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + $(ProjectDir)..\include;$(IncludePath) + $(SolutionDir)Bin\$(Configuration)\plugins\ + + + false + $(ProjectDir)..\include;$(IncludePath) + $(SolutionDir)Bin\$(Configuration)\plugins\ + + + true + $(ProjectDir)..\include;$(IncludePath) + $(SolutionDir)Bin\$(Platform)\$(Configuration)\plugins\ + + + false + $(ProjectDir)..\include;$(IncludePath) + $(SolutionDir)Bin\$(Platform)\$(Configuration)\plugins\ + + + + Level3 + true + WIN32;_DEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + WIN32;NDEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + Level3 + true + _DEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + false + + + + + Level3 + true + true + true + NDEBUG;PLUGINDEMO_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) + true + Use + pch.h + + + Windows + true + true + true + false + + + + + + + + + + + + + Create + Create + Create + Create + + + + + + + + \ No newline at end of file diff --git a/PluginDemo/PluginDemo.vcxproj.filters b/PluginDemo/PluginDemo.vcxproj.filters new file mode 100644 index 000000000..ced896dd8 --- /dev/null +++ b/PluginDemo/PluginDemo.vcxproj.filters @@ -0,0 +1,48 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + + + 源文件 + + + 源文件 + + + 源文件 + + + 源文件 + + + \ No newline at end of file diff --git a/PluginDemo/PluginSystemDate.cpp b/PluginDemo/PluginSystemDate.cpp new file mode 100644 index 000000000..c09e6cd37 --- /dev/null +++ b/PluginDemo/PluginSystemDate.cpp @@ -0,0 +1,27 @@ +#include "pch.h" +#include +#include "PluginSystemDate.h" + +const wchar_t* CPluginSystemDate::GetPluginName() const +{ + return L"Date"; +} + +const wchar_t* CPluginSystemDate::GetItemLableText() const +{ + return L"日期"; +} + +const wchar_t* CPluginSystemDate::GetItemValueText() const +{ + SYSTEMTIME system_time; + GetSystemTime(&system_time); + static wchar_t buff[128]; + swprintf_s(buff, L"%d/%.2d/%.2d", system_time.wYear, system_time.wMonth, system_time.wDay); + return buff; +} + +const wchar_t* CPluginSystemDate::GetItemValueSampleText() const +{ + return L"2022/08/08"; +} diff --git a/PluginDemo/PluginSystemDate.h b/PluginDemo/PluginSystemDate.h new file mode 100644 index 000000000..966bc206e --- /dev/null +++ b/PluginDemo/PluginSystemDate.h @@ -0,0 +1,11 @@ +#pragma once +#include "PluginInterface.h" + +class CPluginSystemDate : public IPluginItem +{ + // 通过 IPluginItem 继承 + virtual const wchar_t* GetPluginName() const override; + virtual const wchar_t* GetItemLableText() const override; + virtual const wchar_t* GetItemValueText() const override; + virtual const wchar_t* GetItemValueSampleText() const override; +}; diff --git a/PluginDemo/PluginSystemTime.cpp b/PluginDemo/PluginSystemTime.cpp new file mode 100644 index 000000000..85b86827a --- /dev/null +++ b/PluginDemo/PluginSystemTime.cpp @@ -0,0 +1,27 @@ +#include "pch.h" +#include +#include "PluginSystemTime.h" + +const wchar_t* CPluginSystemTime::GetPluginName() const +{ + return L"Time"; +} + +const wchar_t* CPluginSystemTime::GetItemLableText() const +{ + return L"时间"; +} + +const wchar_t* CPluginSystemTime::GetItemValueText() const +{ + SYSTEMTIME system_time; + GetSystemTime(&system_time); + static wchar_t buff[128]; + swprintf_s(buff, L"%.2d:%.2d:%.2d", system_time.wHour, system_time.wMinute, system_time.wSecond); + return buff; +} + +const wchar_t* CPluginSystemTime::GetItemValueSampleText() const +{ + return L"12:00:00"; +} diff --git a/PluginDemo/PluginSystemTime.h b/PluginDemo/PluginSystemTime.h new file mode 100644 index 000000000..fe5191875 --- /dev/null +++ b/PluginDemo/PluginSystemTime.h @@ -0,0 +1,12 @@ +#pragma once +#include "PluginInterface.h" + +class CPluginSystemTime : public IPluginItem +{ +public: + // 通过 IPluginItem 继承 + virtual const wchar_t* GetPluginName() const override; + virtual const wchar_t* GetItemLableText() const override; + virtual const wchar_t* GetItemValueText() const override; + virtual const wchar_t* GetItemValueSampleText() const override; +}; diff --git a/PluginDemo/dllmain.cpp b/PluginDemo/dllmain.cpp new file mode 100644 index 000000000..2fa6667af --- /dev/null +++ b/PluginDemo/dllmain.cpp @@ -0,0 +1,40 @@ +// dllmain.cpp : 定义 DLL 应用程序的入口点。 +#include "pch.h" +#include "PluginSystemDate.h" +#include "PluginSystemTime.h" +#include "PluginDemo.h" + +BOOL APIENTRY DllMain( HMODULE hModule, + DWORD ul_reason_for_call, + LPVOID lpReserved + ) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + case DLL_THREAD_ATTACH: + case DLL_THREAD_DETACH: + case DLL_PROCESS_DETACH: + break; + } + return TRUE; +} + +int TMPluginGetItemNum() +{ + return 2; +} + +IPluginItem* TMPluginCreateInstance(int index) +{ + switch (index) + { + case 0: + return new CPluginSystemDate(); + case 1: + return new CPluginSystemTime(); + default: + break; + } + return nullptr; +} diff --git a/PluginDemo/framework.h b/PluginDemo/framework.h new file mode 100644 index 000000000..80cbbc9b0 --- /dev/null +++ b/PluginDemo/framework.h @@ -0,0 +1,5 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容 +// Windows 头文件 +#include diff --git a/PluginDemo/pch.cpp b/PluginDemo/pch.cpp new file mode 100644 index 000000000..b6fb8f4ab --- /dev/null +++ b/PluginDemo/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: 与预编译标头对应的源文件 + +#include "pch.h" + +// 当使用预编译的头时,需要使用此源文件,编译才能成功。 diff --git a/PluginDemo/pch.h b/PluginDemo/pch.h new file mode 100644 index 000000000..966092717 --- /dev/null +++ b/PluginDemo/pch.h @@ -0,0 +1,13 @@ +// pch.h: 这是预编译标头文件。 +// 下方列出的文件仅编译一次,提高了将来生成的生成性能。 +// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。 +// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。 +// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。 + +#ifndef PCH_H +#define PCH_H + +// 添加要在此处预编译的标头 +#include "framework.h" + +#endif //PCH_H diff --git a/TrafficMonitor.sln b/TrafficMonitor.sln index 3270de2e0..ec48edb83 100644 --- a/TrafficMonitor.sln +++ b/TrafficMonitor.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.779 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31515.178 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TrafficMonitor", "TrafficMonitor\TrafficMonitor.vcxproj", "{09483BED-B1E9-4827-8120-A18302C84AA8}" ProjectSection(ProjectDependencies) = postProject @@ -10,6 +10,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TrafficMonitor", "TrafficMo EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "OpenHardwareMonitorApi", "OpenHardwareMonitorApi\OpenHardwareMonitorApi.vcxproj", "{C0A42F4A-ABB3-4575-B4D5-CEDD8379AC26}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PluginDemo", "PluginDemo\PluginDemo.vcxproj", "{D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug (without temperature)|x64 = Debug (without temperature)|x64 @@ -50,6 +52,22 @@ Global {C0A42F4A-ABB3-4575-B4D5-CEDD8379AC26}.Release|x64.Build.0 = Release|x64 {C0A42F4A-ABB3-4575-B4D5-CEDD8379AC26}.Release|x86.ActiveCfg = Release|Win32 {C0A42F4A-ABB3-4575-B4D5-CEDD8379AC26}.Release|x86.Build.0 = Release|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug (without temperature)|x64.ActiveCfg = Debug|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug (without temperature)|x64.Build.0 = Debug|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug (without temperature)|x86.ActiveCfg = Debug|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug (without temperature)|x86.Build.0 = Debug|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug|x64.ActiveCfg = Debug|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug|x64.Build.0 = Debug|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug|x86.ActiveCfg = Debug|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Debug|x86.Build.0 = Debug|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release (without temperature)|x64.ActiveCfg = Release|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release (without temperature)|x64.Build.0 = Release|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release (without temperature)|x86.ActiveCfg = Release|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release (without temperature)|x86.Build.0 = Release|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release|x64.ActiveCfg = Release|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release|x64.Build.0 = Release|x64 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release|x86.ActiveCfg = Release|Win32 + {D1CA3ECC-DC32-445A-B734-C4DB08D4BA34}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TrafficMonitor/Common.cpp b/TrafficMonitor/Common.cpp index cf6ba8ce5..c633300fe 100644 --- a/TrafficMonitor/Common.cpp +++ b/TrafficMonitor/Common.cpp @@ -409,7 +409,7 @@ void CCommon::GetFiles(const wchar_t* path, vector& files) //文件信息(用Unicode保存使用_wfinddata_t,多字节字符集使用_finddata_t) _wfinddata_t fileinfo; wstring file_name; - if ((hFile = _wfindfirst(wstring(path).append(L"\\*").c_str(), &fileinfo)) != -1) + if ((hFile = _wfindfirst(path, &fileinfo)) != -1) { do { diff --git a/TrafficMonitor/PluginManager.cpp b/TrafficMonitor/PluginManager.cpp new file mode 100644 index 000000000..7d60ca2bc --- /dev/null +++ b/TrafficMonitor/PluginManager.cpp @@ -0,0 +1,55 @@ +#include "stdafx.h" +#include "PluginManager.h" +#include "Common.h" + +CPluginManager::CPluginManager() +{ +} + +void CPluginManager::LoadPlugins() +{ + //从plugins目录下加载插件 + wstring plugin_dir; + plugin_dir = CCommon::GetModuleDir() + L"plugins"; + vector plugin_files; + CCommon::GetFiles((plugin_dir + L"\\*.dll").c_str(), plugin_files); //获取Plugins目录下所有的dll文件的文件名 + for (const auto& file : plugin_files) + { + //插件信息 + m_modules.push_back(PluginInfo()); + PluginInfo& plugin_info{ m_modules.back() }; + //插件dll的路径 + plugin_info.file_path = plugin_dir + file; + //载入dll + plugin_info.plugin_module = LoadLibrary(plugin_info.file_path.c_str()); + if (plugin_info.plugin_module == NULL) + { + plugin_info.state = PluginState::PS_MUDULE_LOAD_FAILED; + plugin_info.error_code = GetLastError(); + continue; + } + //获取函数的入口地址 + typedef int (*pfTMPluginGetItemNum)(); + pfTMPluginGetItemNum TMPluginGetItemNum = (pfTMPluginGetItemNum)::GetProcAddress(plugin_info.plugin_module, "TMPluginGetItemNum"); + typedef IPluginItem* (*pfTMPluginCreateInstance)(int); + pfTMPluginCreateInstance TMPluginCreateInstance = (pfTMPluginCreateInstance)::GetProcAddress(plugin_info.plugin_module, "TMPluginCreateInstance"); + if (TMPluginGetItemNum == NULL || TMPluginCreateInstance == NULL) + { + plugin_info.state = PluginState::PS_FUNCTION_GET_FAILED; + plugin_info.error_code = GetLastError(); + continue; + } + int item_num = TMPluginGetItemNum(); + for (int i = 0; i < item_num; i++) + { + std::shared_ptr item = std::shared_ptr(TMPluginCreateInstance(i)); + plugin_info.plugin_items.push_back(item); + m_plugins.push_back(item); + } + } +} + +const std::vector>& CPluginManager::GetPlugins() +{ + return m_plugins; +} diff --git a/TrafficMonitor/PluginManager.h b/TrafficMonitor/PluginManager.h new file mode 100644 index 000000000..108327acc --- /dev/null +++ b/TrafficMonitor/PluginManager.h @@ -0,0 +1,35 @@ +#pragma once +#include "PluginInterface.h" +#include + +//用于加载和管理插件 +class CPluginManager +{ +public: + //插件的状态 + enum class PluginState + { + PS_SUCCEED, //载入成功 + PS_MUDULE_LOAD_FAILED, //dll加载失败 + PS_FUNCTION_GET_FAILED, //插件函数获取失败 + }; + + //插件信息 + struct PluginInfo + { + wstring file_path; //文件路径 + HMODULE plugin_module; //dll module + std::vector> plugin_items; //插件提供的所有显示项目 + PluginState state{}; //插件的状态 + DWORD error_code{}; //错误代码(GetLastError的返回值) + }; + + CPluginManager(); + void LoadPlugins(); + + const std::vector>& GetPlugins(); + +private: + std::vector> m_plugins; + std::vector m_modules; +}; diff --git a/TrafficMonitor/TrafficMonitor.cpp b/TrafficMonitor/TrafficMonitor.cpp index d5ed45837..f14cc11a4 100644 --- a/TrafficMonitor/TrafficMonitor.cpp +++ b/TrafficMonitor/TrafficMonitor.cpp @@ -87,7 +87,7 @@ void CTrafficMonitorApp::LoadConfig() m_cfg_data.m_select_all = ini.GetBool(_T("connection"), _T("select_all"), false); //判断皮肤是否存在 std::vector skin_files; - CCommon::GetFiles(theApp.m_skin_path.c_str(), skin_files); + CCommon::GetFiles((theApp.m_skin_path + L"\\*").c_str(), skin_files); bool is_skin_exist = (!skin_files.empty()); ini.LoadMainWndColors(_T("config"), _T("text_color"), m_main_wnd_data.text_colors, (is_skin_exist ? 16384 : 16777215)); //根据皮肤是否存在来设置默认的文本颜色,皮肤文件不存在时文本颜色默认为白色 m_main_wnd_data.specify_each_item_color = ini.GetBool(_T("config"), _T("specify_each_item_color"), false); @@ -952,6 +952,8 @@ BOOL CTrafficMonitorApp::InitInstance() } #endif + m_plugins.LoadPlugins(); + //执行测试代码 #ifdef _DEBUG CTest::Test(); diff --git a/TrafficMonitor/TrafficMonitor.h b/TrafficMonitor/TrafficMonitor.h index dfe7d8c9c..d1ba44095 100644 --- a/TrafficMonitor/TrafficMonitor.h +++ b/TrafficMonitor/TrafficMonitor.h @@ -16,6 +16,7 @@ #include "TaskbarDefaultStyle.h" #include #include "OpenHardwareMonitor/OpenHardwareMonitorApi.h" +#include "PluginManager.h" // CTrafficMonitorApp: // 有关此类的实现,请参阅 TrafficMonitor.cpp @@ -82,6 +83,7 @@ class CTrafficMonitorApp : public CWinApp HWND m_option_dlg{}; //选项设置对话框的句柄 + CPluginManager m_plugins; CMenu m_main_menu; CMenu m_taskbar_menu; diff --git a/TrafficMonitor/TrafficMonitor.vcxproj b/TrafficMonitor/TrafficMonitor.vcxproj index 9c815def6..304aac840 100644 --- a/TrafficMonitor/TrafficMonitor.vcxproj +++ b/TrafficMonitor/TrafficMonitor.vcxproj @@ -426,6 +426,7 @@ + @@ -465,6 +466,7 @@ + @@ -527,6 +529,7 @@ + diff --git a/TrafficMonitor/TrafficMonitor.vcxproj.filters b/TrafficMonitor/TrafficMonitor.vcxproj.filters index 246be13b0..8240a4e1d 100644 --- a/TrafficMonitor/TrafficMonitor.vcxproj.filters +++ b/TrafficMonitor/TrafficMonitor.vcxproj.filters @@ -175,6 +175,9 @@ {bc164584-be21-4a91-9af5-772d61ed9529} + + {f7dd3288-085a-4de2-9f83-d8ba2f3eff47} + @@ -371,6 +374,12 @@ 源文件和头文件\对话框类\选项设置对话框\SetItemOrderDlg + + 源文件和头文件\头文件 + + + 源文件和头文件\头文件 + @@ -547,6 +556,9 @@ 源文件和头文件\对话框类\选项设置对话框\SetItemOrderDlg + + 源文件和头文件 + diff --git a/TrafficMonitor/TrafficMonitorDlg.cpp b/TrafficMonitor/TrafficMonitorDlg.cpp index 7d417e6d6..1d3f78bab 100644 --- a/TrafficMonitor/TrafficMonitorDlg.cpp +++ b/TrafficMonitor/TrafficMonitorDlg.cpp @@ -913,7 +913,7 @@ BOOL CTrafficMonitorDlg::OnInitDialog() //初始化皮肤 - CCommon::GetFiles(theApp.m_skin_path.c_str(), m_skins); + CCommon::GetFiles((theApp.m_skin_path + L"\\*").c_str(), m_skins); if (m_skins.empty()) m_skins.push_back(L""); m_skin_selected = 0; diff --git a/include/PluginInterface.h b/include/PluginInterface.h new file mode 100644 index 000000000..3aded81f4 --- /dev/null +++ b/include/PluginInterface.h @@ -0,0 +1,29 @@ +#pragma once +//插件接口的定义 + +class IPluginItem +{ +public: + //获取插件的名称 + virtual const wchar_t* GetPluginName() const = 0; + + //获取项目标签的文本 + virtual const wchar_t* GetItemLableText() const = 0; + + //获取项目数值的文本 + virtual const wchar_t* GetItemValueText() const = 0; + + //获取项目数值的示例文本 + virtual const wchar_t* GetItemValueSampleText() const = 0; +}; + +/* +* 注意:插件dll需要导出如下两个函数 +* int TMPluginGetItemNum(); +* IPluginItem* TMPluginCreateInstance(int index); +* +* 一个插件dll可以提供多个实现IPluginItem接口的对象,对应多个显示项目。 +* TMPluginGetItemNum函数返回对象的个数。 +* TMPluginCreateInstance函数用于创建对象,返回IPluginItem接口的指针, +* 参数index为对象的索引,它的值必须大于或等于0且小于TMPluginGetItemNum的返回值。 +*/