C++ GetSystemDirectory()

关于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???


posted @ 2018-01-05 19:16  heismk  阅读(1418)  评论(0编辑  收藏  举报