码迷,mamicode.com
首页 > 编程语言 > 详细

C++ GetSystemDirectory()

时间:2018-01-05 19:56:08      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:getchar   buffer   ror   orm   argv   desktop   library   err   failed   

关于GetSystemDirectory function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724373(v=vs.85).aspx

 

以下代码摘自Getting System Information(https://msdn.microsoft.com/en-us/library/windows/desktop/ms724426(v=vs.85).aspx)。

IDE: Microsoft Visual Studio Community 2017 15.5.2

操作系统:Windows 7 x64

 1 #include "stdafx.h"    /* IDE自行创建的 */
 2 
 3 #include <windows.h>
 4 
 5 #define INFO_BUFFER_SIZE 21
 6 
 7 int main(int argc, char **argv)
 8 {
 9     TCHAR infoBuf[INFO_BUFFER_SIZE];
10     UINT RetSize;
11 
12     RetSize = GetSystemDirectory(infoBuf, INFO_BUFFER_SIZE);
13 //    RetSize = 0;
14     if ((RetSize != 0) && (RetSize <= INFO_BUFFER_SIZE)) {
15         // If the function succeeds, the return value is the length, in TCHARs, of the string copied to the buffer,
16         // not including the terminating null character.
17 
18         // Display the system directory.
19         printf("System Directory: %ls", infoBuf);
20     }
21     else if (RetSize > INFO_BUFFER_SIZE) {
22         // If the length is greater than the size of the buffer, 
23         // the returnv alue is the size of the buffer required to hold the path, 
24         // including the terminating null character.
25         printf("The size of the buffer is not enough, need %d TCHARs at least. \n", RetSize);
26     }
27     else {
28         // If the function fails, the return value is zero. To get extended error information, call GetLastError.
29         printf("Get system directory failed with error: %d", GetLastError());
30     }
31 
32     getchar();
33 
34     return 0;
35 }

 

可以修改INFO_BUFFER_SIZE的值,改变程序的运行结果。

也可以去掉RetSize = 0;前面的注释,模拟错误(实际上不是错误,我只是用来测试if语句的条件)。

不知道是否存在BUG???

C++ GetSystemDirectory()

标签:getchar   buffer   ror   orm   argv   desktop   library   err   failed   

原文地址:https://www.cnblogs.com/Satu/p/8206456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!