#include <windows.h>
#include <tchar.h>
#include <shlobj.h>
#define NUM_ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))
LPCTSTR lpszShell32 = TEXT("shell32.dll");
LPCTSTR lpszSHFolder = TEXT("shfolder.dll");
#ifdef
UNICODE
LPCSTR lpszGFP = "SHGetFolderPathW";
#else
LPCSTR lpszGFP = "SHGetFolderPathA";
#endif
typedef HRESULT (
WINAPI * SHGETFOLDERPATH)(HWND, int,
HANDLE, DWORD, LPTSTR);
UINT_PTR GetProgramFilesFolder(LPTSTR lpFolder, UINT cchMax)
{
SHGETFOLDERPATH pfnGFP;
HINSTANCE hDll;
UINT_PTR cch = 0;
TCHAR szFolder[MAX_PATH * 2];
if(NULL != (hDll = LoadLibrary(lpszShell32)))
{
if(NULL != (pfnGFP = (SHGETFOLDERPATH)GetProcAddress(hDll, lpszGFP)))
{
if(SUCCEEDED(pfnGFP(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szFolder)))
{
_tcsncpy(lpFolder, szFolder, min(cchMax, NUM_ELEMENTS(szFolder)));
lpFolder[cchMax - 1] = TEXT('\0');
cch = _tcslen(lpFolder);
}
}
FreeLibrary(hDll);
}
if(!cch)
{
if(NULL != (hDll = LoadLibrary(lpszSHFolder)))
{
if(NULL != (pfnGFP = (SHGETFOLDERPATH)GetProcAddress(hDll, lpszGFP)))
{
if(SUCCEEDED(pfnGFP(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, szFolder)))
{
_tcsncpy(lpFolder, szFolder, min(cchMax, NUM_ELEMENTS(szFolder)));
lpFolder[cchMax - 1] = TEXT('\0');
cch = _tcslen(lpFolder);
}
}
FreeLibrary(hDll);
}
}
return(cch);
}