
// 获取本机IP.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include
#include
#include "iphlpapi.h"
#pragma comment(lib,"WS2_32")
#pragma comment(lib,"iphlpapi.lib")
#define INVALID_ADDRESS -1
#define DEFAULT_DESTINATION "121.14.89.14"
DWORD RetrieveByIndex(DWORD index)
{
PMIB_IPADDRTABLE pAddrTable(NULL);
PMIB_IPADDRROW pAddrRow(NULL);
ULONG ulSize(0);
DWORD ret=INVALID_ADDRESS;
char *pBuffer=NULL;
DWORD dwResult=GetIpAddrTable((PMIB_IPADDRTABLE)(CHAR*)pBuffer,&ulSize,TRUE);
pBuffer= new char[ulSize];
if(!pBuffer) return INVALID_ADDRESS;
if(dwResult==NO_ERROR)
{
pAddrTable=(PMIB_IPADDRTABLE)(char*)pBuffer;
for(DWORD x=0; x
{
pAddrRow=(PMIB_IPADDRROW)&(pAddrTable->table[x]);
if(pAddrRow->dwIndex==index)
{
ret=pAddrRow->dwAddr;
delete[]pBuffer;
return ret;
}
}
delete[]pBuffer;
return INVALID_ADDRESS;
}
DWORD RetrieveBestInterface(void)
{
DWORD dwBestIndex=INVALID_ADDRESS;
DWORD dwResult=GetBestInterface(inet_addr(DEFAULT_DESTINATION),&dwBestIndex);
if(dwResult==NO_ERROR)
{
return RetrieveByIndex(dwBestIndex);
}
return INVALID_ADDRESS;
}
int main(int argc, char* argv[])
{
WSADATA ws;
WSAStartup(MAKEWORD(2,2),&ws);
DWORD dwIP=RetrieveBestInterface();
in_addr in;
in.S_un.S_addr=dwIP;
printf("%s\n",inet_ntoa(in));
return 0;
}
请帮我回答一下这里的问题:
E:\Program Files\软件\VC6.0\MSDev98\MyProjects\获取本机IP\获取本机IP.cpp(37) : error C2601: 'RetrieveBestInterface' : local function definitions are illegal
E:\Program Files\软件\VC6.0\MSDev98\MyProjects\获取本机IP\获取本机IP.cpp(47) : error C2601: 'main' : local function definitions are illegal
E:\Program Files\软件\VC6.0\MSDev98\MyProjects\获取本机IP\获取本机IP.cpp(56) : fatal error C1004: unexpected end of file found
检查下括号,函数声明是不是写对了,这种问题比较初级……