昨晚看了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.csdn.net/topics/340124944

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

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

GetComputerNameEx()的更多相关文章

  1. 英文不好也能快速"记忆" API

    英文不好不要紧,把API函数导入打字练习类软件,即是练习打字速度,提高编程效率:也能短时间记忆API. 坚持每天打一遍,约2小时,连续打两周,会对API有很好的记忆,此方法是结合英文学习方法!以下是W ...

  2. windows server 2008 - 创建域和本机用户

    /* * ===================================================================================== * Filenam ...

  3. VC++获取计算机Hardware Information (CPU ID, MainBoard Info, Hard Disk Serial, System Information)

    转载:http://blog.csdn.net/yapingxin/article/details/50107799 转载:http://zhidao.baidu.com/link?url=A5K6N ...

  4. pywin32记录备忘

    项目地址:http://sourceforge.net/projects/pywin32/ 文档地址:http://docs.activestate.com/activepython/2.7/pywi ...

  5. 系统信息的管理函数API

    1.Windows系统信息 1.1获取系统版本:   BOOL WINAPI GetVersionEx( __in_out LPOSVERSIONINFO lpVersionInfo ); lpVer ...

随机推荐

  1. Django学习手册 - 连接mysql数据库

    版本问题: 首先确认django.msql.python版本是统一支持 当前所用的版本信息: Django setting.py 配置 替换之前的DATABASES 配置: DATABASES = { ...

  2. python - beautifulsoup4模块

    # beautifulsoup4学习 # 是一个python模块 用于接受一个HTML 或 XML 字符串,然后将其进行格式化,之后便可以使用模块提供的方法进行快速查找指定元素, # 从而是的在HTM ...

  3. PHP Tp5.0 PHPExcel 导出操作

    1.导出 excel 方法 2.导出方法(简单快速)

  4. 在Python中用Selenium执行JavaScript

    Selenium自己不带浏览器, 需要与第三方浏览器结合在一起使用.例如在Firefox上运行Selenium. PhantomJS是一个"无头"浏览器. 它会把网站加载到内存并执 ...

  5. Angular 创建组件

    创建组件 0 命令创建 1 创建组件 定义hello.component.ts组件 在app.module.ts中引用并添加到declarations声明中 在app.component.html中使 ...

  6. dubbo源码分析2——SPI机制中的SPI实现类的读取和预处理

    SPI机制中的SPI实现类的读取和预处理是由ExtensionLoader类的loadFile方法来完成的 loadFile方法的作用是读取dubbo的某个SPI接口的spi描述文件,然后进行缓存,缓 ...

  7. mysql死锁-查询锁表进程-分析锁表原因【转】

    查询锁表进程: 1.查询是否锁表 show OPEN TABLES where In_use > 0;   2.查询进程     show processlist   查询到相对应的进程===然 ...

  8. VB获取CAD属性值

    Dim myAcadApp As AutoCAD.AcadApplication, activeDoc As AutoCAD.AcadDocument, acMS As AutoCAD.AcadMod ...

  9. 题解-BOI 2004 Sequence

    Problem bzoj & Luogu 题目大意: 给定序列\(\{a_i\}\),求一个严格递增序列\(\{b_i\}\),使得\(\sum \bigl |a_i-b_i\bigr|\)最 ...

  10. codeforces 462div.2

    A A Compatible Pair standard input/output 1 s, 256 MB    x1916 B A Prosperous Lot standard input/out ...