用c++写一个dll 用于获取系统桌面路径
以下是用C++编写的获取系统桌面路径的DLL:
#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;
}
}
使用方法:将以上代码保存为一个.cpp文件,使用Visual Studio等工具编译生成.dll文件。在其他程序中使用时,先将.dll文件导入,然后调用GetDesktopPath()函数即可获取系统桌面路径。例如:
#include <windows.h>
typedef 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;
}
原文地址: https://www.cveoy.top/t/topic/b3jD 著作权归作者所有。请勿转载和采集!