Question

I'm trying to interface with a particular system that checks if a file is in a particular place, and if so, uses the url stored in that file to download updates.

The C# test application dialog I'm using as an example solves this problem by using a C# helper class, which forms the path to the file by appending folders onto "Environment.SpecialFolder.CommonApplicationData". From there it can edit and delete the file to control the system's behaviour. So my C++ Application needs to do the same thing.

What I need is a way of getting that path in C++. I can reconstruct every part of it except the C# Environment variable, which is OS specific.

So, how do I get the "Environment.SpecialFolder.CommonApplicationData" path in C++?

(Solutions that solve my problem of "find this file" are acceptable, depending on the amount of working code I'd have to modify)

Was it helpful?

Solution

You should probably use the function SHGetSpecialFolderPath with CSIDL_COMMON_APPDATA, see MSDN.

As a fallback, you can always read the registry key

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common AppData

OTHER TIPS

The MSDN page for SHGetSpecialFolderPath says:

SHGetSpecialFolderPath is not supported. Instead, use ShGetFolderPath.

But ShGetFolderPath is deprecated and contains this note:

As of Windows Vista, this function is merely a wrapper for SHGetKnownFolderPath. The CSIDL value is translated to its associated KNOWNFOLDERID and then SHGetKnownFolderPath is called. New applications should use the known folder system rather than the older CSIDL system, which is supported only for backward compatibility.

The correct way to do this is now:

#include "stdafx.h"
#include <windows.h>
#include <KnownFolders.h>
#include <ShlObj.h>

int main()
{
    PWSTR path = NULL;

    HRESULT result = SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &path);
    if (SUCCEEDED(result)) {
        wprintf_s(L"Path: %ls\n", path);
    }

    CoTaskMemFree(path);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top