📄 vc常用源码.htm
字号:
取得开始菜单或桌面的PIDL<BR>SHGetSpecialFolderLocation(AfxGetApp()->m_pMainWnd->m_hWnd,CSIDL_DESKTOP,&pidlBeginAt)
;<BR>// 取得新建文件夹的父文件夹<BR>if( !BrowseForFolder(pidlBeginAt ,
<BR>&pidlDestination,<BR>"请选择图片目录的位置:"))<BR>{<BR>return ;<BR>}<BR>//
把PIDL转换为路径名<BR>SHGetPathFromIDList( pidlDestination, szPath);
<BR>//添加如下函数<BR>BOOL BrowseForFolder(<BR>LPITEMIDLIST
pidlRoot,//浏览开始处的PIDL<BR>LPITEMIDLIST
*ppidlDestination,<BR>//浏览结束时所选择的PIDL<BR>LPCSTR
lpszTitle)//浏览对话框中的提示文字<BR>{ <BR>BROWSEINFO BrInfo ;<BR>ZeroMemory(
&BrInfo, sizeof(BrInfo)) ;<BR>BrInfo.hwndOwner = HWND_DESKTOP
;<BR>BrInfo.pidlRoot = pidlRoot ;<BR>BrInfo.lpszTitle = lpszTitle
;<BR>//浏览文件夹<BR>*ppidlDestination=
SHBrowseForFolder(&BrInfo);<BR>//用户选择了取消按钮<BR>if(NULL ==
*ppidlDestination)<BR>return FALSE ;/**/<BR>return TRUE ;<BR>}</FONT></P>
<P><FONT color=#006633
size=2>//读写INI/ini文件*************************************************</FONT></P>
<P><FONT color=#006633 size=2>//写入值 字段名 变量名 值 带目录文件名
<BR>WritePrivateProfileString("目录","path",cs, regpath);<BR>//写入结构体 字段名 变量名
值 大小
带目录文件名<BR>WritePrivateProfileStruct("字体","font",&LF,sizeof(LOGFONT),regpath);//结构体<BR>//读入字符
字段名 变量名 默认值 字符缓冲区 长度 带目录文件名 <BR>GetPrivateProfileString("目录","path","",
buffer.GetBuffer(260),260,regpath);<BR>//读入整数值 字段名 变量名 默认值
带目录文件名<BR>GetPrivateProfileInt("colors","red",255, regpath);<BR>//读入结构体
字段名 变量名 值 大小 带目录文件名
<BR>GetPrivateProfileStruct("字体","font",&LF,sizeof(LOGFONT),regpath);</FONT></P>
<P><FONT color=#006633
size=2>//位图操作,画图*****************************************************</FONT></P>
<P><FONT color=#006633 size=2>CClientDC client(this);<BR>BITMAP
bmpInfo;<BR>CDC memdc;<BR>CBitmap
picture;<BR>memdc.CreateCompatibleDC(pdc);<BR>memdc.SelectObject(&picture);<BR>CRect
re;<BR>GetClientRect(&re);<BR>client.BitBlt(0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,&memdc,0,0,SRCCOPY);<BR>client.SetBkMode(TRANSPARENT);<BR>client.SetTextColor(RGB(red,green,blue));<BR>CFont
font;<BR>CFont
*oldfont;<BR>font.CreateFontIndirect(&LF);<BR>oldfont=client.SelectObject(&font);<BR>client.DrawText("",//注意这个字符串里如果只有一连串的字母或数字,没有空格或中文或标点符号,且总长度超过距形宽度,则不能自动换行!!<BR>&re,DT_CENTER
|DT_WORDBREAK);<BR>client.SelectObject(oldfont);</FONT></P>
<P><FONT color=#006633
size=2>//打开EXE/exe文件**********************************************</FONT></P>
<P><FONT color=#006633 size=2>ShellExecute(GetSafeHwnd(),<BR>"open",
<BR>"http://home.jlu.edu.cn/~ygsoft",//改这个文件名就可以了<BR>NULL,<BR>NULL,SW_SHOWNORMAL);<BR>//or<BR>LPITEMIDLIST
pidlBeginAt;<BR>//
取得桌面的PIDL<BR>SHGetSpecialFolderLocation(AfxGetApp()->m_pMainWnd->m_hWnd,<BR>CSIDL_DRIVES//这个标志为我的电脑<BR>,&pidlBeginAt)
;<BR>SHELLEXECUTEINFO
exe;<BR>ZeroMemory(&exe,sizeof(SHELLEXECUTEINFO));<BR>exe.cbSize=sizeof(SHELLEXECUTEINFO);<BR>exe.fMask=SEE_MASK_IDLIST;<BR>exe.lpVerb="open";<BR>exe.nShow=SW_SHOWNORMAL;<BR>exe.lpIDList=pidlBeginAt;<BR>ShellExecuteEx(&exe);</FONT></P>
<P><FONT color=#006633
size=2>//取得函数不能正常返回的原因字符***************************</FONT></P>
<P><FONT color=#006633 size=2>LPVOID lpMsgBuf;<BR>FormatMessage(
<BR>FORMAT_MESSAGE_ALLOCATE_BUFFER | <BR>FORMAT_MESSAGE_FROM_SYSTEM |
<BR>FORMAT_MESSAGE_IGNORE_INSERTS,<BR>NULL,<BR>GetLastError(),<BR>MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT), // Default language<BR>(LPTSTR)
&lpMsgBuf,<BR>0,<BR>NULL <BR>);<BR>// Process any inserts in
lpMsgBuf.<BR>// ...<BR>// Display the
string.<BR>MessageBox((LPCTSTR)lpMsgBuf);<BR>// Free the
buffer.<BR>LocalFree( lpMsgBuf );</FONT></P>
<P><FONT color=#006633
size=2>//关机函数************************************************</FONT></P>
<P><FONT color=#006633 size=2>void ShutDown(void)//2000 OR
NT<BR>{<BR>OSVERSIONINFO osv;<BR>osv.dwOSVersionInfoSize=sizeof
OSVERSIONINFO;<BR>GetVersionEx(&osv);<BR>if(osv.dwPlatformId==VER_PLATFORM_WIN32_NT)//VER_PLATFORM_WIN32_WINDOWS
98 Me用这个宏<BR>{<BR>HANDLE hProcess,hToken;<BR>TOKEN_PRIVILEGES
Privileges;<BR>LUID
luid;<BR>hProcess=GetCurrentProcess();<BR>OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);<BR>Privileges.PrivilegeCount=1;<BR>LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);<BR>Privileges.Privileges[0].Luid=luid;<BR>Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;<BR>AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);<BR>}<BR>ExitWindowsEx(EWX_POWEROFF,0);<BR>}</FONT></P>
<P><FONT color=#006633
size=2>//***************************************</FONT></P>
<P><FONT color=#006633 size=2>//创建一个非模态对话框,如 <BR>//1声明父对话框<BR>#include
"Tx.h"<BR>//2在头文件中改为CTx* pParent = NULL,并声明CTx
*tx;<BR>public:<BR>CInputmr(CTx* pParent = NULL); // standard
constructor<BR>CTx *tx;<BR>//3在CPP文件中改为CTx* pParent
/*=NULL*/,并声明ID:CDialog(CInputmr::IDD,
pParent)和tx(pParent)<BR>CInputmr::CInputmr(CTx* pParent /*=NULL*/)<BR>:
CDialog(CInputmr::IDD,
pParent),tx(pParent)<BR>{<BR>if(Create(CInputmr::IDD,pParent))//创建<BR>ShowWindow(SW_SHOW);//显示窗口<BR>//就可以使用
tx-> 了<BR>}</FONT></P>
<P></P>
<P><BR><FONT color=#006633
size=2>常用网络数据包报头结构->以太网、ARP、IP、TCP、UDP、ICMP、DNS、UDP伪报头***************************************</FONT></P>
<P><FONT color=#006633 size=2>#pragma pack(1) <BR>typedef struct ethdr
//以太网包头14字节<BR>{<BR>unsigned char destination_mac[6];//目标MAC
6字节<BR>unsigned char source_mac[6];//源MAC 6字节<BR>unsigned short
type;//后面的协议类型2字节 为0806表示后面跟的包为ARP包<BR>}ET_HEADER,*PETHDR;</FONT></P>
<P><FONT color=#006633 size=2>typedef struct arphdr
//arp协议28字节<BR>{<BR>unsigned short hard_tpye;//硬件类型2字节 通常为01
(以太网地址)<BR>unsigned short protocol;//协议类型2字节 通常为80 (IP地址)<BR>unsigned char
hard_length;//硬件地址长度1字节 通常为6<BR>unsigned char protocol_length;//协议地址长度1字节
通常为4 (IP协议)<BR>unsigned short operation_type;//操作类型
1为ARP请求,2为ARP应答,3为RARP请求,4为RARP应答<BR>unsigned char
source_mac[6];//源物理地址<BR>struct in_addr source_ip;//源IP地址<BR>unsigned char
destination_mac[6];//目的物理地址<BR>struct in_addr
destination_ip;//目的IP地址<BR>}ARP_HEADER,*PARPHDR;</FONT></P>
<P><FONT color=#006633 size=2><BR>// IP Header -- RFC 791
IP数据报头<BR>typedef struct tagIPHDR<BR>{<BR>u_char VIHL; // Version and IHL
版本4bit = 4 和 首部长度4bit = 5<BR>u_char TOS; // Type Of Service 服务类型
1字节<BR>short TotLen; // Total Length 总长度2字节,包括数据和报头<BR>short ID; //
Identification 标识符2字节<BR>short FlagOff; // Flags and Fragment Offset 标志
3bit 和分段偏移量 13bit<BR>u_char TTL; // Time To Live
生存期1字节,为经过路由器的总次数<BR>u_char Protocol; // Protocol 协议类型1字节<BR>u_short
Checksum; // Checksum 首部(只是IP首部!!不然就错了,过不了网关!!)校验和2字节<BR>struct in_addr
source_ip; // Internet Address - Source 源IP地址<BR>struct in_addr
destination_ip; // Internet Address - Destination 目的IP地址<BR>}IP_HEADER,
*PIP_HEADER;</FONT></P>
<P><FONT color=#006633 size=2>//TCP Header TCP数据报头<BR>typedef struct
tcp_hdr<BR>{<BR>unsigned short source_port; //源端口<BR>unsigned short
destination_port; //目的端口<BR>unsigned long index; //32位序号<BR>unsigned long
makesuer_index; //32位确认序号<BR>unsigned short header_length_and_flags;
//首部长度和标志位<BR>unsigned short window_size; //窗口大小<BR>unsigned short
checksum; //检查和<BR>unsigned short exigency_pointer;
//紧急指针<BR>}TCP_HEADER;</FONT></P>
<P><FONT color=#006633 size=2>//UDP Header --UDP数据报头</FONT></P>
<P><FONT color=#006633 size=2>typedef struct udp_hdr<BR>{<BR>unsigned
short source_port; //源端口<BR>unsigned short destination_port;
//目的端口<BR>unsigned short length; //数据长度<BR>unsigned short checksum;
//带数据!检查和<BR>} UDP_HEADER;</FONT></P>
<P><FONT color=#006633 size=2><BR>// ICMP Header - RFC 792
ICMP数据报头<BR>typedef struct tagICMPHDR<BR>{<BR>u_char Type; // Type
类型<BR>u_char Code; // Code 代码<BR>u_short Checksum; // Checksum
校验和<BR>u_short ID; // Identification 标识符<BR>u_short Seq; // Sequence
序列号<BR>char Data; // Data 数据(依情况而定)<BR>}ICMP_HEADER,
*PICMP_HEADER;</FONT></P>
<P><FONT color=#006633 size=2><BR>// ICMP Echo Request ICMP
请求数据报<BR>typedef struct tagECHOREQUEST<BR>{<BR>ICMPHDR icmpHdr;<BR>DWORD
dwTime;<BR>char cData[32];<BR>}IMCP_REQUEST, *PIMCP_REQUEST;</FONT></P>
<P><FONT color=#006633 size=2><BR>// ICMP Echo Reply ICMP 回响数据报<BR>typedef
struct tagECHOREPLY<BR>{<BR>IPHDR ipHdr;<BR>ECHOREQUEST
echoRequest;<BR>char cFiller[256];<BR>}ICMP_REPLY, *ICMP_REPLY;</FONT></P>
<P><FONT color=#006633 size=2><BR>typedef struct dns
//DNS数据报:<BR>{<BR>unsigned short
id;//标识,通过它客户端可以将DNS的请求与应答相匹配;<BR>unsigned short flags;//标志:[QR | opcode |
AA| TC| RD| RA | zero | rcode ]<BR>unsigned short
quests;//问题数目;<BR>unsigned short answers;//资源记录数目;<BR>unsigned short
author;//授权资源记录数目;<BR>unsigned short
addition;//额外资源记录数目;<BR>}DNS,*PDNS;<BR>//在16位的标志中:QR位判断是查询/响应报文,opcode区别查询类型,AA判断是否为授权回答,TC判断<BR>//是否可截断,RD判断是否期望递归查询,RA判断是否为可用递归,zero必须为0,rcode为返回码字段。</FONT></P>
<P><FONT color=#006633 size=2>typedef struct psd
//伪报头,用于计算UDP校验和<BR>{<BR>unsigned int source_ip; //源IP<BR>unsigned int
destination_ip; //目的IP<BR>char mbz; // 0<BR>char protocol; //协议 UDP =
17<BR>unsigned short udp_length; //UDP 长度<BR>}PSD,*PPSD;</FONT></P>
<P><FONT color=#006633 size=2>//DNS查询数据报:<BR>typedef struct
query<BR>{<BR>unsigned short type; //查询类型,大约有20个不同的类型<BR>unsigned short
classes; //查询类,通常是A类既查询IP地址。<BR>}QUERY,*PQUERY;</FONT></P>
<P><FONT color=#006633 size=2>//DNS响应数据报:<BR>typedef struct
response<BR>{<BR>unsigned short name; //查询的域名<BR>unsigned short type;
//查询类型<BR>unsigned short classes; //类型码<BR>unsigned int ttl;
//生存时间<BR>unsigned short length; //资源数据长度<BR>unsigned int addr;
//资源数据<BR>}RESPONSE,*PRESPONSE;</FONT></P>
<P><FONT color=#006633 size=2>#pragma pack()<BR></FONT></P>
<P><FONT color=#006633
size=2>网络编程部分源码->校验和、隐藏IP方法、取得本地MAC********************************</FONT></P>
<P><FONT color=#006633 size=2>//注意!!!!!!
<BR>//IP的检查和只是IP头部的20个字节,不然过不了网关!!!!<BR>//而TCP和UDP和检查和是数据和头部都包括!!<BR>//在计算检查和之前IP或UDP或TCP报头中
所有的成员 必须被初始化,包括 checksum!!!</FONT></P>
<P><FONT color=#006633
size=2><BR>//为了保证系统和机器的******兼容性******,在定义网络结构体或共用体时要加上<BR>#pragma
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -