#include <windows.h>

#ifdef EXPORTS #define DLL_API __declspec(dllexport) #else #define DLL_API __declspec(dllimport) #endif

DLL_API char* GetDesktopPath() { static char szPath[MAX_PATH] = { 0 }; if (SHGetSpecialFolderPathA(NULL, szPath, CSIDL_DESKTOPDIRECTORY, FALSE)) { return szPath; } else { return NULL; } }

使用方法

  1. 将以上代码保存为一个 .cpp 文件,使用 Visual Studio 等工具编译生成 .dll 文件。
  2. 在其他程序中使用时,先将 .dll 文件导入,然后调用 GetDesktopPath() 函数即可获取系统桌面路径。

例如:

#include <windows.h>

typed ef char* (*GetDesktopPathFunc)();

int main()
{
    HMODULE hModule = LoadLibrary(TEXT("DesktopPath.dll"));
    if (hModule)
    {
        GetDesktopPathFunc pGetDesktopPathFunc = (GetDesktopPathFunc)GetProcAddress(hModule, "GetDesktopPath");
        if (pGetDesktopPathFunc)
        {
            char* szPath = pGetDesktopPathFunc();
            if (szPath)
            {
                MessageBoxA(NULL, szPath, "Desktop Path", MB_OK);
            }
        }
        FreeLibrary(hModule);
    }
    return 0;
}
C++ DLL 获取系统桌面路径 - 代码示例

原文地址: https://www.cveoy.top/t/topic/nnoW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录