昨晚看了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[] = TEXT("");
TCHAR szDescription[][] = { 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 = ;
DWORD dwSize = sizeof(buffer); for (cnf = ; cnf < ComputerNameMax; cnf++)
{
if (!GetComputerNameEx((COMPUTER_NAME_FORMAT)cnf, buffer, &dwSize)) {
_tprintf(TEXT("GetComputerNameEx failed (%lu) \n"), GetLastError());
return ;
}
else {
_tprintf(TEXT("%s: %s \n"), szDescription[cnf], buffer);
} dwSize = _countof(buffer);
ZeroMemory(buffer, dwSize);
} return ;
}
之后便是各种折腾,始终找不到问题所在,后来太晚了,就睡了。。。
今早重新上网找,嘿!终于有些眉目了!
有网友说需要包含头文件stdlib.h,然后,我就包含啊,Build... 还是不行!
另有网友说,可能是因为IDE掺杂了不同的版本的库导致:http://bbs.****.net/topics/340124944
后来看到有网友把关于宏“_countof()”的定义给贴了出来:http://blog.****.net/shell2522/article/details/5790885,这里也有:http://blog.****.net/yahohi/article/details/8035743
于是,又把代码改了一下,Build... 嗯,这下终于是通过了!目前只能靠这个方法解决,改天换个IDE试试。
#define _WIN32_WINNT 0x0500 #include <windows.h>
#include <stdio.h>
#include <tchar.h> #if !defined(_countof)
#if !defined(__cplusplus)
#define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#else
extern "C++"
{
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))
}
#endif
#endif int _tmain(void)
{
TCHAR buffer[] = TEXT("");
TCHAR szDescription[][] = { 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 = ;
DWORD dwSize = sizeof(buffer); for (cnf = ; cnf < ComputerNameMax; cnf++)
{
if (!GetComputerNameEx((COMPUTER_NAME_FORMAT)cnf, buffer, &dwSize)) {
_tprintf(TEXT("GetComputerNameEx failed (%lu) \n"), GetLastError());
return ;
}
else {
_tprintf(TEXT("%s: %s \n"), szDescription[cnf], buffer);
} dwSize = _countof(buffer);
ZeroMemory(buffer, dwSize);
} return ;
}
有一些还是刚接触的,不懂,自己琢磨一下。
关于“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[] = TEXT("");
经过预处理器处理之后,其实就是:
char buffer[] = "";
语句:
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)), (), ((dwSize)));
作用就是将buffer的第一个字节至第dwSize个字节的内容改为0。
关于“_countof()”,网友是这样说的:http://blog.****.net/yahohi/article/details/8035743
MSDN给出关于“_countof()”的参考:https://msdn.microsoft.com/en-us/library/ms175773.aspx