GetComputerNameEx()

昨晚看了MSDN提供的GetComputerNameEx function(参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724301),想试试这个函数,于是Ctrl + C、Ctrl + V,稍作修改,Build...

提示错误:error: '_countof' was not declared in this scope 

 

代码如下(基于Code::Blocks):

IDE: Code::Blocks

操作系统:Windows 7 x64

#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(void)
{
    TCHAR buffer[256] = TEXT("");
    TCHAR szDescription[8][32] = { TEXT("NetBIOS"),
        TEXT("DNS hostname"),
        TEXT("DNS domain"),
        TEXT("DNS fully-qualified"),
        TEXT("Physical NetBIOS"),
        TEXT("Physical DNS hostname"),
        TEXT("Physical DNS domain"),
        TEXT("Physical DNS fully-qualified") };
    int cnf = 0;
    DWORD dwSize = sizeof(buffer);

    for (cnf = 0; cnf < ComputerNameMax; cnf++)
    {
        if (!GetComputerNameEx((COMPUTER_NAME_FORMAT)cnf, buffer, &dwSize)) {
            _tprintf(TEXT("GetComputerNameEx failed (%lu) \n"), GetLastError());
            return 1;
        }
        else {
            _tprintf(TEXT("%s: %s \n"), szDescription[cnf], buffer);
        }

        dwSize = _countof(buffer);
        ZeroMemory(buffer, dwSize);
    }

    return 0;
}

 

之后便是各种折腾,始终找不到问题所在,后来太晚了,就睡了。。。

 


 

今早重新上网找,嘿!终于有些眉目了!

有网友说需要包含头文件stdlib.h,然后,我就包含啊,Build... 还是不行!

另有网友说,可能是因为IDE掺杂了不同的版本的库导致:http://bbs.csdn.net/topics/340124944

后来看到有网友把关于宏“_countof()”的定义给贴了出来:http://blog.csdn.net/shell2522/article/details/5790885,这里也有:http://blog.csdn.net/yahohi/article/details/8035743

于是,又把代码改了一下,Build... 嗯,这下终于是通过了!目前只能靠这个方法解决,改天换个IDE试试。

 1 #define _WIN32_WINNT 0x0500
 2 
 3 #include <windows.h>
 4 #include <stdio.h>
 5 #include <tchar.h>
 6 
 7 #if !defined(_countof)
 8 #if !defined(__cplusplus)
 9 #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
10 #else
11 extern "C++"
12 {
13     template <typename _CountofType, size_t _SizeOfArray>
14     char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
15     #define _countof(_Array) sizeof(*__countof_helper(_Array))
16 }
17 #endif
18 #endif
19 
20 int _tmain(void)
21 {
22     TCHAR buffer[256] = TEXT("");
23     TCHAR szDescription[8][32] = { TEXT("NetBIOS"),
24         TEXT("DNS hostname"),
25         TEXT("DNS domain"),
26         TEXT("DNS fully-qualified"),
27         TEXT("Physical NetBIOS"),
28         TEXT("Physical DNS hostname"),
29         TEXT("Physical DNS domain"),
30         TEXT("Physical DNS fully-qualified") };
31     int cnf = 0;
32     DWORD dwSize = sizeof(buffer);
33 
34     for (cnf = 0; cnf < ComputerNameMax; cnf++)
35     {
36         if (!GetComputerNameEx((COMPUTER_NAME_FORMAT)cnf, buffer, &dwSize)) {
37             _tprintf(TEXT("GetComputerNameEx failed (%lu) \n"), GetLastError());
38             return 1;
39         }
40         else {
41             _tprintf(TEXT("%s: %s \n"), szDescription[cnf], buffer);
42         }
43 
44         dwSize = _countof(buffer);
45         ZeroMemory(buffer, dwSize);
46     }
47 
48     return 0;
49 }

 


 

有一些还是刚接触的,不懂,自己琢磨一下。


关于“TEXT()”的宏定义:

#ifdef UNICODE
/*
 * __TEXT is a private macro whose specific use is to force the expansion of a
 * macro passed as an argument to the macro TEXT.  DO NOT use this
 * macro within your programs.  It's name and function could change without
 * notice.
 */
#define __TEXT(q) L##q
#else
#define __TEXT(q) q
#endif
/*
 * UNICODE a constant string when UNICODE is defined, else returns the string
 * unmodified.
 * The corresponding macros  _TEXT() and _T() for mapping _UNICODE strings
 * passed to C runtime functions are defined in mingw/tchar.h
 */
#define TEXT(q) __TEXT(q)

 

所以,语句:

TCHAR buffer[256] = TEXT("");

 

经过预处理器处理之后,其实就是:

char buffer[10] = "";

 


 

语句:

ZeroMemory(buffer, dwSize);

 

关于“ZeroMemory()”的宏定义:

#define ZeroMemory RtlZeroMemory
#define RtlZeroMemory(d,l) RtlFillMemory((d),(l),0)
#define RtlFillMemory(d,l,f) memset((d), (f), (l))

 

线索渐渐清晰了,“ZeroMemory()”最终是关联到函数“memset()”,实际上就是等效:

memset(((buffer)), (0), ((dwSize)));

 

作用就是将buffer的第一个字节至第dwSize个字节的内容改为0。

 


 

关于“_countof()”,网友是这样说的:http://blog.csdn.net/yahohi/article/details/8035743

MSDN给出关于“_countof()”的参考:https://msdn.microsoft.com/en-us/library/ms175773.aspx

posted @ 2018-01-04 10:40  heismk  阅读(912)  评论(0编辑  收藏  举报