发布网友 发布时间:2024-10-24 02:43
共2个回答
热心网友 时间:1天前
可以用直接获取的插件
热心网友 时间:1天前
MFC编程实现主机名及本地IP地址的获取,建立一对话框工程,假设名为IP,其有一个按钮响应的程序:如OnButton1();
BOOL CIPDlg::OnInitDialog()
{
CDialog::OnInitDialog();
AfxSocketInit(NULL);//支持Socket.若在向导是没选Support Socket,这就的加.还要加#include <afxsock.h>在StdAfx.h中.
.......
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CIPDlg::OnButton1()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
AfxMessageBox(name);//name里是本机名
AfxMessageBox(ip); //ip中是本机IP
}