[英]Getting path to the desktop for current user in C using WinAPI
I'm very new to WinAPI programming. 我是WinAPI编程的新手。 I was wondering how I could get the path of the user's Desktop and then print out the full path to the console. 我想知道如何获取用户桌面的路径,然后打印出控制台的完整路径。 This is my current code: 这是我当前的代码:
TCHAR* path = 0;
HRESULT result = SHGetKnownFolderPath(&FOLDERID_Desktop, 0, NULL, &path);
if (result == S_OK)
{
printf("%s\n", path);
}
CoTaskMemFree(path)
It does find the path, but it prints out "C" for the path rather than the entire path with slashes. 它确实找到了路径,但是它为该路径而不是带有斜杠的整个路径输出“ C”。 What am I missing? 我想念什么?
Thanks! 谢谢!
SHGetKnownFolderPath()
outputs a wchar_t*
pointer, not a TCHAR*
pointer. SHGetKnownFolderPath()
输出wchar_t*
指针,而不是TCHAR*
指针。 There is no ANSI version of SHGetKnownFolderPath()
, so you should not be using TCHAR
at all in this situation. 没有SHGetKnownFolderPath()
ANSI版本,因此在这种情况下您根本不应该使用TCHAR
。 In fact, your code will not compile unless UNICODE
is defined so TCHAR
maps to wchar_t
. 实际上,除非定义了UNICODE
否则您的代码将无法编译,因此TCHAR
映射到wchar_t
。
The reason you only see the 1st character is because you are passing a wchar_t*
where a char*
is expected. 您仅看到第一个字符的原因是因为您传递了wchar_t*
,其中期望使用char*
。 On Windows, wchar_t
is 16-bit, and so wchar_t*
strings are encoded in UCS-2 or UTF-16LE. 在Windows上, wchar_t
是16位的,因此wchar_t*
字符串以UCS-2或UTF-16LE编码。 All ASCII characters in UCS-2/UTF-16LE have their high 8 bits set to 0. Your use of printf()
is expecting a null terminated char*
string, so the high 0x00 byte of the 1st wchar_t
character gets misinterpreted as a null terminator. UCS-2 / UTF-16LE中的所有ASCII字符的高8位都设置为0。您使用printf()
期望以null结尾的char*
字符串,因此第一个wchar_t
字符的高0x00字节被误解为null终结者。
To do what you want, you need to print out the returned path as-is as a wide string, not as a (misinterpreted) narrow string. 要执行所需的操作,您需要按原样将返回的路径打印为宽字符串,而不是(误解的)窄字符串。
You could use %S
with printf()
, eg: 您可以将%S
与printf()
,例如:
PWSTR path;
if (SHGetKnownFolderPath(&FOLDERID_Desktop, 0, NULL, &path) == S_OK)
{
printf("%S\n", path);
CoTaskMemFree(path);
}
But this is not portable across compilers. 但这不能跨编译器移植。 You should use %s
with wprintf()
instead: 您应该将%s
与wprintf()
使用:
PWSTR path;
if (SHGetKnownFolderPath(&FOLDERID_Desktop, 0, NULL, &path) == S_OK)
{
wprintf(L"%s\n", path);
CoTaskMemFree(path);
}
It prints a single character because you tried to print a wide string as a narrow string. 它只打印一个字符,因为您尝试将宽字符串打印为窄字符串。 You probably have UNICODE/_UNICODE defined so TCHAR is WCHAR. 您可能已经定义了UNICODE / _UNICODE,所以TCHAR是WCHAR。
If you insist on using TCHAR you should use the correct printing function from tchar.h: 如果您坚持使用TCHAR,则应使用tchar.h中的正确打印功能:
_tprintf(_T("%s\n"), path);
Otherwise you can use the wide version: 否则,您可以使用宽版本:
wprintf(L"%s\n", path);
Or the worst solution, convert the string to a narrow codepage string: 或最差的解决方案,将字符串转换为狭窄的代码页字符串:
printf("%ls\n", path); // This might not display all Unicode characters correctly
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.