我的方法是(应该是很多人都在用的方法)
char szHostName[128];
gethostname(szHostName, 128);
//...
pHost = gethostbyname(szHostName);
//...可以是可以,但是调试的时候 1 句 gethostname 就会导致输出"极有可能导致异常"的信息警告:
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).如果按F5 debug,经过获取IP地址的代码后,Output窗口里没有上述信息的,请告诉我,送上高分讨论:)
char szHostName[128];
gethostname(szHostName, 128);
//...
pHost = gethostbyname(szHostName);
//...可以是可以,但是调试的时候 1 句 gethostname 就会导致输出"极有可能导致异常"的信息警告:
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).如果按F5 debug,经过获取IP地址的代码后,Output窗口里没有上述信息的,请告诉我,送上高分讨论:)
解决方案 »
- 在视图中点击鼠标时,如何能动态创建多个静态文本控件?并且可以实现控件的拖动?
- 用richedit实现了语法高亮功能,但窗口闪烁,如何解决?
- 使用TAB控件显示对话框前,对话框大小可以改变吗?代码如下~~
- 在一个工程当中,seting-library-ouput file name是***.lib,请问这是用那种工程类型生成的
- VC6.0在XPsp2下无法安装?~~~
- 怎样取得游戏窗口句柄
- *******我的窗体怎么凹陷进去了?是不是FormView的问题?*******
- 怎么播放wav文件的波形??????????????????(500分)(在线等待)
- 救急!.dwl和.nlc文件如何浏览?(100分,大侠们帮帮忙!)
- MFC中如何将对话框中的数据传递到文档中
- OnTimer()函数中的奇怪问题
- 100分弱问一下问题
gethostname(name,sizeof(kk));
AfxMessageBox(kk);
struct hostent *pHostEnt;
pHostEnt = gethostbyname(name);
if( pHostEnt != NULL ){
sprintf( Hostaddress,"%d.%d.%d.%d",
( pHostEnt->h_addr_list[0][0]&0x00ff ),
( pHostEnt->h_addr_list[0][1]&0x00ff ),
( pHostEnt->h_addr_list[0][2]&0x00ff ),
( pHostEnt->h_addr_list[0][3]&0x00ff ) );
}
AfxMessageBox(Hostaddress);
#include <wsipx.h>
#include <wsnwlink.h>
#include <stdio.h>int main()
{
////////////////
// 初始化 Windows sockets API. 要求版本为 version 1.1
//
WORD wVersionRequested = MAKEWORD(1, 1);
WSADATA wsaData;
if (WSAStartup(wVersionRequested, &wsaData)) {
printf("WSAStartup failed %s\n", WSAGetLastError());
return -1;
} //////////////////
// 获得主机名.
//
char hostname[256];
int res = gethostname(hostname, sizeof(hostname));
if (res != 0) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
printf("hostname=%s\n", hostname);
////////////////
// 根据主机名获取主机信息.
//
hostent* pHostent = gethostbyname(hostname);
if (pHostent==NULL) {
printf("Error: %u\n", WSAGetLastError());
return -1;
}
//////////////////
// 解析返回的hostent信息.
//
hostent& he = *pHostent;
printf("name=%s\naliases=%s\naddrtype=%d\nlength=%d\n",
he.h_name, he.h_aliases, he.h_addrtype, he.h_length);
sockaddr_in sa;
for (int nAdapter=0; he.h_addr_list[nAdapter]; nAdapter++) {
memcpy ( &sa.sin_addr.s_addr, he.h_addr_list[nAdapter],he.h_length);
// 输出机器的IP地址.
printf("Address: %s\n", inet_ntoa(sa.sin_addr)); // 显示地址串
}
//////////////////
// 终止 Windows sockets API
//
WSACleanup();
return 0;
}
WSAStartup(MAKEWORD(2,0), &wsaData);没有用,一样会有输出的,难道你们的不会吗?请仔细看一下Output窗口的调试信息
Loaded 'C:\WINNT\system32\shlwapi.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\DHCPCSVC.DLL', no matching symbolic information found.
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
Loaded 'C:\WINNT\system32\winrnr.dll', no matching symbolic information found.
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
Loaded 'C:\WINNT\system32\rasadhlp.dll', no matching symbolic information found.
...我的问题并不在获取IP地址本身,而是在获取IP地址的时候引发的访问异常警告:(
就是有很多Loaded 'C:\WINNT\system32\rasadhlp.dll', no matching .... 的那些,谢谢了
CString GetLocalHostName()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
CString csSU;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
// Couldn't find a usable WinSock DLL.
csSU="没有找到可用的Winsock DLL!";
return csSU;
}
// Confirm that the WinSock DLL supports 2.0.
// Note that if the DLL supports versions greater
// than 2.0 in addition to 2.0, it will still return
// 2.0 in wVersion since that is the version we
// requested.
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
// Couldn't find a usable WinSock DLL.
csSU="没有找到可用的Winsock DLL!";
WSACleanup( );
return csSU;
}
char szHostName[256];
int nRetCode;
CString sHostName;
nRetCode = gethostname(szHostName, sizeof(szHostName));
if (nRetCode != 0)
{
// An error has occurred
sHostName = _T("Not available");;
return sHostName;
}
nRetCode = WSACleanup();
if (nRetCode != 0)
{
// An error occured.
sHostName = _T("发生错误!");;
return sHostName;
}
CString csHost=szHostName;
return (csHost);
}
取得IP:
CString GetLocalIP()
{ WORD wVersionRequested;
WSADATA wsaData;
int err;
CString csSU;
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
// Couldn't find a usable WinSock DLL.
csSU="没有找到可用的Winsock DLL!";
return csSU;
}
// Confirm that the WinSock DLL supports 2.0.
// Note that if the DLL supports versions greater
// than 2.0 in addition to 2.0, it will still return
// 2.0 in wVersion since that is the version we
// requested.
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
// Couldn't find a usable WinSock DLL.
csSU="没有找到可用的Winsock DLL!";
WSACleanup( );
return csSU;
}
char szHostName[256];
int nRetCode;
CString sHostName;
nRetCode = gethostname(szHostName, sizeof(szHostName));
if (nRetCode != 0)
{
// An error has occurred
sHostName = _T("Not available");;
return sHostName;
}
CString sIPAddress;CString _HostName;
struct hostent FAR *lpHostEnt = gethostbyname (szHostName);
if (lpHostEnt == NULL)
{
// An error occurred.
sIPAddress = _T("");
_HostName = _T("发生错误!");
return _HostName;
}
LPSTR lpAddr = lpHostEnt->h_addr_list[0];
if (lpAddr)
{
struct in_addr inAddr;
memmove (&inAddr, lpAddr, 4);
sIPAddress = inet_ntoa (inAddr);
if (sIPAddress.IsEmpty())
{
_HostName = _T("Not available!");
sIPAddress = _T("Not available");
AfxMessageBox(_HostName);
return _HostName;
}
}
nRetCode = WSACleanup();
if (nRetCode != 0)
{
// An error occured.
sHostName = _T("发生错误!");;
return sHostName;
}
return (sIPAddress);
}
………………………………………………
我的问题是如何避免调试输出窗口的诸多警告信息
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).这些只是警告信息,这些代码本身的运行一点问题都没有,一点问题都没有,一切正常
……………………要不有人和我解释一下
First-chance exception in test.exe (KERNEL32.DLL): 0x000006BA: (no name).
^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^
到底是什么回事也可以谢谢
char szHostName[256];
gethostname(szHostName, sizeof(szHostName));gethostname()用szHostName作为指针,在其指向的内存写入不定长的数据。有的编译环境认为这种写入内存的方法可能会导致异常,所以会提出First-chance exception的警告。