7 Star 6 Fork 3

梁欢 / DynamicLib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

DynamicLib

DynamicLib,极简的方式从dll或者so或者dylib文件获取导出(C)函数。下面展示的是一个简单的例子。

动态存储的方式导出User32.dll中的函数

struct ShareUser32 {
    ShareUser32();
    DllFunc<HWND()> GetDesktopWindow;
    DllFunc<BOOL(HWND, LPRECT)> GetClientRect;
};

ShareUser32::ShareUser32()
    : GetClientRect("User32.dll", "GetClientRect", DllFuncType::DTF_STDCALL)
    , GetDesktopWindow("User32.dll", "GetDesktopWindow", DllFuncType::DTF_STDCALL)
{}

静态存储方式导出User32.dll中的函数

struct StaticUser32 {
    static DllFunc<HWND()> GetDesktopWindow;
    static DllFunc<BOOL(HWND, LPRECT)> GetClientRect;
};

DllFunc<HWND()> StaticUser32::GetDesktopWindow("User32.dll", "GetDesktopWindow", DllFuncType::DTF_STDCALL);
DllFunc<BOOL(HWND, LPRECT)> StaticUser32::GetClientRect("User32.dll", "GetClientRect", DllFuncType::DTF_STDCALL);

使用动态存储方式可以很方便的在相同接口不同实现的dll文件间自由切换

struct DllTest {
    DllFunc<int()> fnDllTest;
    DllTest() : fnDllTest("fnDllTest") {}
};

调用示例

int main(int argc, char* argv[]) {

#ifdef WIN32

    RECT rcWnd;

    // 动态存储懒加载
    ShareUser32 mUser32;
    HWND hWnd = mUser32.GetDesktopWindow();
    mUser32.GetClientRect(hWnd, &rcWnd);

    // 静态存储懒加载
    hWnd = StaticUser32::GetDesktopWindow();
    StaticUser32::GetClientRect(hWnd, &rcWnd);

#endif

    char buffer[256];
    _getcwd(buffer, sizeof(buffer));

    //////////////////////////////////////////////////////////////////////////

    DllTest DllTestInst;

    // 动态存储切换加载资源
#ifdef WIN32
    DllTestInst.fnDllTest.Load("DllTestOne.dll");
#else
    std::string OnePath(buffer);
#ifdef __APPLE__
    OnePath.append("/DllTestOne.dylib");
#else
    OnePath.append("/DllTestOne.so");
#endif
    DllTestInst.fnDllTest.Load(OnePath.c_str());
#endif

DllTestInst.fnDllTest();
DllTestInst.fnDllTest.Free();

    // 动态存储切换加载资源
#ifdef WIN32
    DllTestInst.fnDllTest.Load("DllTestTwo.dll");
#else
    std::string TwoPath(buffer);
#ifdef __APPLE__
    TwoPath.append("/DllTestTwo.dylib");
#else
    TwoPath.append("/DllTestTwo.so");
#endif
    DllTestInst.fnDllTest.Load(TwoPath.c_str());
#endif
    DllTestInst.fnDllTest();
    DllTestInst.fnDllTest.Free();

    return 0;
}										
The MIT License (MIT) Copyright (c) 2015 lvan100 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

DynamicLib,极简的方式从dll或者so或者dylib文件获取导出(C)函数。 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/lvan100/DynamicLib.git
git@gitee.com:lvan100/DynamicLib.git
lvan100
DynamicLib
DynamicLib
master

搜索帮助