获取Windows桌面路径

Fac*_*res 1 c++ windows winapi

这是我的代码:

#include <Windows.h>
#include <ShlObj.h>
#include <iostream>

using namespace std;

int main()
{
    LPTSTR myPath = NULL;

    SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);

    if(myPath != NULL)
        cout << "It returns something" << endl;
    else
        cout << "It returns nothing" << endl;
    system("PAUSE");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但myPath什么也没有返回.我只想获取桌面路径.我使用的是Windows 7 64位.

Joe*_*csy 5

您需要为数据提供空间:

T_CHAR myPath[ MAX_PATH ];
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);
Run Code Online (Sandbox Code Playgroud)