Windows平台下用C++代码取得机器的MAC地址并不是一件简单直接的事情。到目前为止,作者尚未发现有任何一个通用的100%的适用于所有Windows平台的方法可以稳定的取得MAC地址。而有些应用(比如MMORPG)则需要稳定的得到机器的MAC地址,解决方案往往是通过多种方法依次使用来提高成功率。
以下方法只会返回多网卡的第一个MAC地址。
网上有很多文章和源码来解决该问题,大多不全或有问题。本篇所有方法均经过整理调试,可直接使用。
作者也不喜欢满篇帖代码,本篇贴代码是方便直接使用,请读者谅解。
...
这个数据结构是这样的:
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};
#define h_addr h_addr_list[0]
这里是这个数据结构的详细资料:
struct hostent:
h_name – 地址的正式名称。
h_aliases – 空字节-地址的预备名称的指针。
h_addrtype –地...
1.#define指令定义了一个标识符及一个字符序列(即字符集合)。在源程序中每次遇到该标识符时,就用定义的字符序列替换它。标识符被称为宏名,替换过程称为宏替换,指令的一般形式为:
#define macro-name char-sequence
2.#error指令强迫编译器停止编译,它主要用于程序调试。#error指令的一般形式为:
#error error-message
3.#if, #else, #elif, #endif
#if e...
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
double d = 12.23;
int i = 101;
long l = 123023L;
if((fp = fopen("test", "wb")) == NULL)
{
printf("Cannot open file.\n");
exit(1);
}
fwrite(&d, sizeof(double), 1, fp);
fwrite(&i, sizeof(int), 1, fp);
fwrite(&a...
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
if(argc != 3)
{
printf("Usage: SEEK filename byte\n");
exit(1);
}
if((fp = fopen(argv[1], "rb")) == NULL)
{
printf("Cannot open file.\n");
exit(1);
}
if(fseek(fp, atol(argv[2]), SEEK_SET))
{
pri...
timer库的代码摘要如下:
class timer
{
public:
timer() { _start_time = std::clock(); }
void restart() { _start_time = std::clock(); }
double elapsed() const
{ return double(std::clock() - _start_time) / CLOCKS_PER_SEC; }
double elapsed_min() const
{ return double(1) / double(CLOCKS_P...
今天在同学的帮助下,成功的在vc6.0中安装和编译boost库,遇到不少问题,并解决之,把过程大概写一下,以便以后参阅:
(主要参考文章:http://h-lm.spaces.live.com/blog/cns!C523F565A10E3B66!749.entry
http://blog.csdn.net/liroket/archive/2009/06/10/4257308.aspx)
据说c6.0不支持目前比较新版本,如:boost1.36。这里给出VC6.0编译boost1.34.1的流程。
1....
使用remove(filename)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
char str[80];
if(argc != 2)
{
printf("usage: xerase <filename>\n");
exit(1);
}
printf("Erase %s? (Y/N):", argv[1]);
gets(str);
if(toupper(*str) == 'Y')
...
函数名与函数指针
一 通常的函数调用
一个通常的函数调用的例子:
//自行包含头文件
void MyFun(int x); //此处的申明也可写成:void MyFun( int );
int main(int argc, char* argv[])
{
MyFun(10); //这里是调用MyFun(10);函数
return 0;
}
void MyFun(int x) //这里定义一个MyFun函数
{
printf(“%d\n”,x);
}
...
C++格式化字符串(续)
2. 一些特殊规定字符
━━━━━━━━━━━━━━━━━━━━━━━━━━
字符 作用
──────────────────────────
\n 换行
\f 清屏并换页
\r 回车
\t Tab符
\xhh ...
1. GlobalAlloc函数和new()的区别。
1>new是标准的C++分配内存函数。GlobalAlloc是WIN下的API函数。
2>new分配内存同时会调用类等对象的构造函数。GlobalAlloc不回。
3>new分配的内存只能同进程使用,如(A进程new,B进程不能delete),GlobalAlloc能多进程使用,并删除。
2. GlobalAlloc分配的内存需要释放么?
所有动态内存分配都必须释放,GlobalAlloc分配的内存用GlobalFree进行释放。
3. GlobalAlloc和HeapAlloc的...
1、获取当前系统时间:
[cpp] view plaincopy
SYSTEMTIME systime;
GetLocalTime(&systime);
char str_time_name[MAX_PATH];
sprintf(str_time_name,"%d-%d-%d %d:%d:%d.jpg",systime.w...
1、消息炸弹
[cpp] view plaincopy
#define UNICODE
#define _UNICODE
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <Lm.h>
...
#include <windows.h>
//Win32窗口程序实例
//声明消息处理函数
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
char szClassName[]="windowsclass";
char szAppTitle[]="WinApi窗口实例";
i...