vc常用代码3
扫描二维码
随时随地手机看文章
//关机函数************************************************
void ShutDown(void)//2000 or NT
{
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize=sizeof OSVERSIONINFO;
GetVersionEx(&osv);
if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT)//VER_PLATFORM_WIN32_WINDOWS 98 Me用这个宏
{
HANDLE hProcess,hToken;
TOKEN_PRIVILEGES Privileges;
LUID luid;
hProcess=GetCurrentProcess();
OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);
Privileges.PrivilegeCount=1;
LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
Privileges.Privileges[0].Luid=luid;
Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);
}
ExitWindowsEx(EWX_POWEROFF,0);
}
//***************************************
//创建一个非模态对话框,如
//1声明父对话框
#include "Tx.h"
//2在头文件中改为CTx* pParent = NULL,并声明CTx *tx;
public:
CInputmr(CTx* pParent = NULL); // standard constructor
CTx *tx;
//3在CPP文件中改为CTx* pParent /*=NULL*/,并声明ID:CDialog(CInputmr::IDD, pParent)和tx(pParent)
CInputmr::CInputmr(CTx* pParent /*=NULL*/)
: CDialog(CInputmr::IDD, pParent),tx(pParent)
{
if(Create(CInputmr::IDD,pParent))//创建
ShowWindow(SW_SHOW);//显示窗口
//就可以使用 tx-> 了
}
常用网络数据包报头结构->以太网、ARP、IP、TCP、UDP、ICMP、DNS、UDP伪报头***************************************
#pragma pack(1)
typedef struct ethdr //以太网包头14字节
{
unsigned char destination_mac[6];//目标MAC 6字节
unsigned char source_mac[6];//源MAC 6字节
unsigned short type;//后面的协议类型2字节 为0806表示后面跟的包为ARP包
}ET_HEADER,*PETHDR;
typedef struct arphdr //arp协议28字节
{
unsigned short hard_tpye;//硬件类型2字节 通常为01 (以太网地址)
unsigned short protocol;//协议类型2字节 通常为80 (IP地址)
unsigned char hard_length;//硬件地址长度1字节 通常为6
unsigned char protocol_length;//协议地址长度1字节 通常为4 (IP协议)
unsigned short operation_type;//操作类型 1为ARP请求,2为ARP应答,3为RARP请求,4为RARP应答
unsigned char source_mac[6];//源物理地址
struct in_addr source_ip;//源IP地址
unsigned char destination_mac[6];//目的物理地址
struct in_addr destination_ip;//目的IP地址
}ARP_HEADER,*PARPHDR;
// IP Header -- RFC 791 IP数据报头
typedef struct tagIPHDR
{
u_char VIHL; // Version and IHL 版本4bit = 4 和 首部长度4bit = 5
u_char TOS; // Type Of Service 服务类型 1字节
short TotLen; // Total Length 总长度2字节,包括数据和报头
short ID; // Identification 标识符2字节
short FlagOff; // Flags and Fragment Offset 标志 3bit 和分段偏移量 13bit
u_char TTL; // Time To Live 生存期1字节,为经过路由器的总次数
u_char Protocol; // Protocol 协议类型1字节
u_short Checksum; // Checksum 首部(只是IP首部!!不然就错了,过不了网关!!)校验和2字节
struct in_addr source_ip; // Internet Address - Source 源IP地址
struct in_addr destination_ip; // Internet Address - Destination 目的IP地址
}IP_HEADER, *PIP_HEADER;
//TCP Header TCP数据报头
typedef struct tcp_hdr
{
unsigned short source_port; //源端口
unsigned short destination_port; //目的端口
unsigned long index; //32位序号
unsigned long makesuer_index; //32位确认序号
unsigned short header_length_and_flags; //首部长度和标志位
unsigned short window_size; //窗口大小
unsigned short checksum; //检查和
unsigned short exigency_pointer; //紧急指针
}TCP_HEADER;
//UDP Header --UDP数据报头
typedef struct udp_hdr
{
unsigned short source_port; //源端口
unsigned short destination_port; //目的端口
unsigned short length; //数据长度
unsigned short checksum; //带数据!检查和
} UDP_HEADER;
// ICMP Header - RFC 792 ICMP数据报头
typedef struct tagICMPHDR
{
u_char Type; // Type 类型
u_char Code; // Code 代码
u_short Checksum; // Checksum 校验和
u_short ID; // Identification 标识符
u_short Seq; // Sequence 序列号
char Data; // Data 数据(依情况而定)
}ICMP_HEADER, *PICMP_HEADER;
// ICMP Echo Request ICMP 请求数据报
typedef struct tagECHOREQUEST
{
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[32];
}IMCP_REQUEST, *PIMCP_REQUEST;
// ICMP Echo Reply ICMP 回响数据报
typedef struct tagECHOREPLY
{
IPHDR ipHdr;
ECHOREQUEST echoRequest;
char cFiller[256];
}ICMP_REPLY, *ICMP_REPLY;
typedef struct dns //DNS数据报:
{
unsigned short id;//标识,通过它客户端可以将DNS的请求与应答相匹配;
unsigned short flags;//标志:[QR | opcode | AA| TC| RD| RA | zero | rcode ]
unsigned short quests;//问题数目;
unsigned short answers;//资源记录数目;
unsigned short author;//授权资源记录数目;
unsigned short addition;//额外资源记录数目;
}DNS,*PDNS;
//在16位的标志中:QR位判断是查询/响应报文,opcode区别查询类型,AA判断是否为授权回答,TC判断
//是否可截断,RD判断是否期望递归查询,RA判断是否为可用递归,zero必须为0,rcode为返回码字段。
typedef struct psd //伪报头,用于计算UDP校验和
{
unsigned int source_ip; //源IP
unsigned int destination_ip; //目的IP
char mbz; // 0
char protocol; //协议 UDP = 17
unsigned short udp_length; //UDP 长度
}PSD,*PPSD;
//DNS查询数据报:
typedef struct query
{
unsigned short type; //查询类型,大约有20个不同的类型
unsigned short classes; //查询类,通常是A类既查询IP地址。
}QUERY,*PQUERY;
//DNS响应数据报:
typedef struct response
{
unsigned short name; //查询的域名
unsigned short type; //查询类型
unsigned short classes; //类型码
unsigned int ttl; //生存时间
unsigned short length; //资源数据长度
unsigned int addr; //资源数据
}RESPONSE,*PRESPONSE;
#pragma pack()
网络编程部分源码->校验和、隐藏IP方法、取得本地MAC********************************
//注意!!!!!!
//IP的检查和只是IP头部的20个字节,不然过不了网关!!!!
//而TCP和UDP和检查和是数据和头部都包括!!
//在计算检查和之前IP或UDP或TCP报头中 所有的成员 必须被初始化,包括 checksum!!!
//为了保证系统和机器的******兼容性******,在定义网络结构体或共用体时要加上
#pragma pack(1)
#pragma pack()
//如:
#pragma pack(1)
typedef struct response
{
unsigned short name;
unsigned short type;
unsigned short classes;
unsigned int ttl;
unsigned short length;
unsigned int addr;
}RESPONSE,*PRESPONSE;
#pragma pack()
//sizeof(RESPONSE)==16,如果不加#pragma pack那么sizeof(RESPONSE)==20
//局域网内相同网段下的IP地址,数据包直接发给该主机的MAC地址,不同网段下的,数据包发给网关,
//如:202.198.153.17->202.198.153.64 以太包目的地址为202.198.153.64 的MAC地址
//如:202.198.153.17->202.198.159.139 以太包目的地址为202.198.153.254(网关) 的MAC地址
//隐藏IP的方法************************************************************************
WSADATA wsd;
SOCKET s;
BOOL bOpt;
struct sockaddr_in remote;
USHORT *buf=NULL;
ECHOREQUEST icmpHdr;
int ret;
unsigned short iTotalSize,
iIPVersion,
iIPSize,
cksum = 0;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)//打开WSA网络函数
{
MessageBox("WSAStartup() failed: ");
return ;
}
s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_ICMP, NULL, 0,0);//建立SOCKET,注意第二个一定为SOCK_RAW
if (s == INVALID_SOCKET)
{
MessageBox("WSASocket() failed: ");
return ;
}
bOpt = TRUE;
//设置SOCKET属性
ret = setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt));
if (ret == SOCKET_ERROR)
{
MessageBox("setsockopt(IP_HDRINCL) failed: %d/n");
return ;
}
//修改IP包的值
iTotalSize = sizeof(icmpHdr) ;
iIPVersion = 4;
iIPSize = sizeof(IP_HDR) / sizeof(unsigned long);
icmpHdr.ipHdr.ip_verlen = (iIPVersion << 4) | iIPSize;
icmpHdr.ipHdr.ip_tos = 0;
icmpHdr.ipHdr.ip_totallength = htons(iTotalSize);
icmpHdr.ipHdr.ip_id = 0;
icmpHdr.ipHdr.ip_offset = 0;
icmpHdr.ipHdr.ip_ttl = 128;
icmpHdr.ipHdr.ip_protocol = 1; //ICMP
buf=(USHORT *)&icmpHdr;
icmpHdr.ipHdr.ip_checksum = checksum(buf,sizeof(IP_HDR));
icmpHdr.ipHdr.ip_srcaddr = inet_addr(m_source_ip);
icmpHdr.ipHdr.ip_destaddr = inet_addr(m_destinition);
icmpHdr.icmpHdr.Type=8;
icmpHdr.icmpHdr.Code=0;
icmpHdr.icmpHdr.ID=1;
icmpHdr.icmpHdr.Seq=1;
buf=(USHORT *)&icmpHdr;
icmpHdr.icmpHdr.Checksum=checksum(buf+sizeof(IP_HDR),sizeof(icmpHdr)-sizeof(IP_HDR));
icmpHdr.dwTime=GetTickCount();
remote.sin_family = AF_INET;
remote.sin_port = 0;
remote.sin_addr.s_addr = inet_addr(m_destinition);
struct timeval Timeout;
fd_set readfds;
readfds.fd_count = 1;
readfds.fd_array[0] = s;
Timeout.tv_sec = 1;
Timeout.tv_usec = 0;
int nRet;
int nAddrLen = sizeof(struct sockaddr_in);
ECHOREPLY echoReply;
CString cs;
for(int i=0;i<m_ping_times;i++)
{ //发送IP包
ret = sendto(s, (char *)&icmpHdr, iTotalSize, 0, (SOCKADDR *)&remote, sizeof(remote));
if (ret == SOCKET_ERROR)
MessageBox("sendto() failed:");
/*
select(1, &readfds, NULL, NULL, &Timeout);
//接收请求回应
nRet = recvfrom(s,
(LPSTR)&echoReply,
sizeof(ECHOREPLY),
0,
(LPSOCKADDR)&remote,
&nAddrLen);
//检查返回值
if (nRet == SOCKET_ERROR)
MessageBox("recvfrom()");
cs.Format("%d",echoReply.ipHdr.ip_ttl);
MessageBox(cs);
//返回发送的时间
*/
Sleep(m_separete);
}
closesocket(s) ;
WSACleanup() ;//关闭WSA
}
//校验和程序*****************************************************************************
USHORT CFffDlg::checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
//取得本地机器IP地址*********************************************************************************************
WORD wv=MAKEWORD(1,1);
WSADATA ws;
WSAStartup(wv,&ws);
char name[256];
gethostname(name,256);
HOSTENT *hos=gethostbyname(name);
CString cs;
for(int i=0;hos!=NULL&&hos->h_addr_list[i]!=NULL;i++)
{
cs=inet_ntoa(*(struct in_addr*)hos->h_addr_list[i]);
MessageBox(cs);
}
WSACleanup();
//取得本地MAC地址*********************************************************************************************
#include