Solved Getting standard directories/ file paths

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

user123

Newbie
Full Member
Jul 8, 2014
17
142
0
Im trying to retrieve predefined directories/paths like temp. internet files, My Documents, my music etc.
So only paths, not the files themselves.
I want to make a function for it that returns a string.
This is what I made:
C++:
string getIEFiles()
{
	wchar_t* path;
	//PWSTR* path;
	REFKNOWNFOLDERID rfid = FOLDERID_CommonTemplates;
	HRESULT works = SHGetKnownFolderPath(
		rfid,
		0,
		NULL,
		&path);

	if (works = S_OK)
	{
		cout << "temp ie files path successfully found!" << endl;
	}
	else if (works = E_FAIL)
	{
		//cout << "cannot find temp ie path!" << endl;
		return "ERROR: E_FAIL";
	}
	else if (works = E_INVALIDARG)
	{
		//cout << "cannot find temp ie path!" << endl;
		return "ERROR: E_INVALIDARG";
	}

	
	std::wstring wide(path);
	std::string str(wide.begin(), wide.end());
	CoTaskMemFree(static_cast<void*>(path));
	
	return str;
}
Problem: it always outputs error: E_FAIL (NOT when saving/compiling)
Cause: rfid variable. I changed it to every possible value, nothing works.
Just try and paste this code in a new project please, I want you to replicate the error.
I really hope anyone has the solution!
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,074
78,998
2,371
Here is a small example that worked.

Please note that SHGetKnownFolderPath works on Win Vista and higher. If you want it to work on Win XP as well. Use SHGetFolderPath instead.

C++:
int _tmain(int argc, _TCHAR* argv[])
{
    PWSTR Temp;

    HRESULT hResult = SHGetKnownFolderPath(FOLDERID_Downloads, KF_FLAG_DEFAULT_PATH, NULL, &Temp);

    if (hResult != S_OK)
        MessageBox(NULL, L"Something went wronk!", L"Oh Noez!", MB_OK | MB_ICONERROR);

    printf("Downloads Folder: %S\n\n", Temp);

    return 0;
}
or within a function perhaps?

C++:
PWSTR GetFolderPath(REFKNOWNFOLDERID rfid, DWORD dwFlags) {

    PWSTR pwPath;

    HRESULT hResult = SHGetKnownFolderPath(rfid, dwFlags, NULL, &pwPath);

    if (hResult == E_FAIL) {
        printf("[ERROR] Are you sure the rfid has a valid path?\n\n");
        return 0;
    }
    else if (hResult == E_INVALIDARG) {
        printf("[ERROR] Are you sure rfid exists?\n\n");
        return 0;
    }

    return pwPath;
}


int _tmain(int argc, _TCHAR* argv[])
{
    PWSTR pwPath = GetFolderPath(FOLDERID_Profile, KF_FLAG_DEFAULT_PATH);

    if (pwPath)
        printf("Folder: %S\n\n", pwPath);

    return 0;
}
 
Last edited by a moderator:
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods