tags:

views:

194

answers:

2

How do I determine the (local-) path for the "Program Files" directory on a remote computer? There does not appear to any version of SHGetFolderPath (or related function) that takes the name of a remote computer as a parameter.

I guess I could try to query HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir using remote-registry, but I was hoping there would be "documented" way of doing it.

+1  A: 

Many of the standard paths require a user to be logged in, especially the SH* functions as those are provided by the "shell", that is, Explorer. I suspect the only way you're going to get the right path is through the registry like you already mentioned.

Joel Lucsy
+1  A: 

This is what I ended up doing: (pszComputer must be on the form "\\name". nPath is size of pszPath (in TCHARs))

DWORD GetProgramFilesDir(PCTSTR pszComputer, PTSTR pszPath, DWORD& nPath) 
{
    DWORD n;
    HKEY hHKLM;
    if ((n = RegConnectRegistry(pszComputer, HKEY_LOCAL_MACHINE, &hHKLM)) == ERROR_SUCCESS)
    {
        HKEY hWin;
        if ((n = RegOpenKeyEx(hHKLM, _T("Software\\Microsoft\\Windows\\CurrentVersion"), 0, KEY_READ, &hWin)) == ERROR_SUCCESS)
        {
            DWORD nType, cbPath = nPath * sizeof(TCHAR);
            n = RegQueryValueEx(hWin, _T("ProgramFilesDir"), NULL, &nType, reinterpret_cast<PBYTE>(pszPath), &cbPath);
            nPath = cbPath / sizeof(TCHAR);
            RegCloseKey(hWin);
        }
        RegCloseKey(hHKLM);
    }
    return n;
}
MobyDX