Need a project done?

C++ Programming Developer

Search This Blog

Get Desktop Path Dynamically in C++

This program automatically finds the path of desktop in C++.
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <shlobj.h> // for SHGetKnownFolderPath
using namespace std;

int _tmain() {
    PWSTR pPath = NULL; // this is always a wchar_t*

    // There is no Ansi (char*) version of this function so the TCHAR trick
    // doesn't work here
    HRESULT hr = SHGetKnownFolderPath( FOLDERID_Desktop,
                                       0,    // no flags
                                       NULL, // no tokens
                                       &pPath ); // note we're passing the address here

    if(SUCCEEDED(hr)) {
        wcout << L"Desktop path = " << pPath << endl;
        CoTaskMemFree(pPath); // free memory with correct deallocator function
    }

    return 0;
}
"Don't let anyone ever make you feel like you don't deserve what you want."