昨晚看了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. spark教程

    某大神总结的spark教程, 地址 http://litaotao.github.io/introduction-to-spark?s=inner

  2. C 和 C++ 一些基础

    位运算: Part1: #include <iostream> using namespace std; int main(int argc, char *argv[]) { //unsi ...

  3. 关于 DELPHI DATASNAP 的文章集

    关于 DELPHI  DATASNAP 的文章集: 1.墨者工作室  DataSnap基础 https://wenku.baidu.com/view/78715605cc1755270722088b. ...

  4. Liunx 网络神器之抓包 --tcpdump

    作者:邓聪聪 简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中 ...

  5. HTTP协议06-报文首部

    HTTP报文格式 ​ HTTP协议的请求和响应报文中必定包含HTTP首部.首部内容为客户端和服务器分别处理请求和响应提供所需要的信息. 1)HTTP请求报文 在请求中,HTTP报文由方法.URI.HT ...

  6. win7经常出现“关闭xxxx前您必须关闭所有会话框”

    这可能是windows的一个BUG,在没有关闭输入法的状态下它不默认你关闭了所有窗口,只要把输入法切换回默认的英文输入法就可以正常关闭了

  7. SpringMVC拦截器(慕课网)

    拦截器:通过统一拦截从浏览器发往服务器的请求来完成功能的增强 使用场景:解决请求的共性问题 如:乱码.权限验证 基本工作原理:拦截器和过滤器的工作原理相似 乱码问题:使用Spring过滤器(Filte ...

  8. MySQL数据库的一些方法使用

    substring_index(windSpeed,)/3.6 as windSpeed 可将 .8公里.0m/s 进行拆分 嵌套使用replace方法 replace( replace( repla ...

  9. thymeleaf:日常使用总结

    1.th:href 获得当前的根路径 th:href="@{/}" 2.select输出并自动选中 <select class="form-control sele ...

  10. Linux之 nginx-redis-virtualenv-mysql

    mysql maraidb相关 .yum安装好,启动 安装: yum install mariadb-server mariadb 启动mabiadb: systemctl start mariadb ...