当前位置:网站首页>MFC获取本机IP(网络通讯时用得多)
MFC获取本机IP(网络通讯时用得多)
2022-04-23 19:24:00 【ToneChip】
CString CUDPDlg::GetIP() //获取本地IP
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255]; // 本地主机名字符数组变量
CString m_ip; // IP地址字符串变量
PHOSTENT hostinfo; // hostent结构指针
wVersionRequested = MAKEWORD(2, 0); //连接应用程序与winsock Dll
if (WSAStartup(wVersionRequested, &wsaData) == 0)
{
if (gethostname(name, sizeof(name)) == 0) //得到本地主机名
{
if ((hostinfo = gethostbyname(name)) != NULL) //得到主机信息
{
m_ip = inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list); //转换得到IP地址
}
else m_ip = "IP检测失败";
}
WSACleanup(); // 中止winsock DLL的使用
}
return m_ip; //返回IP地址字符串
}
调用如下
CString localIP;
localIP = GetIP(); //得到本机IP
SetDlgItemText(IDC_IP1, localIP); //获取到的本机IP显示到界面
版权声明
本文为[ToneChip]所创,转载请带上原文链接,感谢
https://tonechip.blog.csdn.net/article/details/122943159
边栏推荐
- ArcMap publishing slicing service
- RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
- 浅谈c语言指针的强制转换
- binlog2sql 工具安装使用及问题汇总
- [报告] Microsoft :Application of deep learning methods in speech enhancement
- Data analysis learning directory
- Main differences between go and PHP
- JS to get the local IP address
- Executor、ExecutorService、Executors、ThreadPoolExecutor、Future、Runnable、Callable
- SSDB Foundation
猜你喜欢
为何PostgreSQL即将超越SQL Server?
[record] typeerror: this getOptions is not a function
Common SQL commands
NiO related Basics
SQL常用的命令
Oracle configuration st_ geometry
Matlab 2019 installation of deep learning toolbox model for googlenet network
Pdf reference learning notes
ArcMap publishing slicing service
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
随机推荐
Codeworks round 783 (Div. 2) d problem solution
Some speculation about the decline of adults' language learning ability
Go three ways to copy files
Garbage collector and memory allocation strategy
Grafana 分享带可变参数的链接
Modify the font size of hint in editext
Class loading process of JVM
開關電源設計分享及電源設計技巧圖解
ArcGIS JS API dojoconfig configuration
C1000k TCP connection upper limit test
Common processing of point cloud dataset
JVM的类加载过程
Web Security
Keysight has chosen what equipment to buy for you
MySQL lock
2021-2022-2 ACM集训队每周程序设计竞赛(8)题解
Regular expressions for judging positive integers
redis优化系列(三)解决主从配置后的常见问题
Solve the problem of invalid listview Click
Zero base to build profit taking away CPS platform official account