看老妖挺闲的,请教个本该是很简单的问题:编译时链接失败
看老妖真够闲的,请教一个链接错误:
[ILINK32 Error] Error: Unresolved external '_DhcpEnumSubnets' referenced from D:\PROJECTS\DHCP\DEBUG\WIN32\FILE1.OBJ
[ILINK32 Error] Error: Unresolved external '_DhcpEnumSubnetClients' referenced from D:\PROJECTS\DHCP\DEBUG\WIN32\FILE1.OBJ
使用的方法是 DHCP 的函数:
头文件是:#include <Dhcpsapi.h>
链接库是:Dhcpsapi.lib
动态库是:Dhcpsapi.dll
我尝试过把 Dhcpsapi.lib 直接加入到工程中,失败了。
也试过了:
#pragma comment (lib, "Dhcpsapi.lib")
或
#pragma link "Dhcpsapi.lib"
也失败了。
然后,再试试用 coff2omf.exe 看看是不是 .lib 的结构是否有误。
最后使出了杀手锏:用 implib 把 Dhcpsapi.dll 重新引出了一个新的 .lib
结果都失败了。
但是用编辑器打开 .lib 看看,咦?这方法在各个 .lib 中都存在。
我晕了......
还有一个多半能成功的方法是动态引入这两个方法,我还没有试验过.....
顺便把Source贴出来供大家探讨,我的目的是从Dhcp中取得所有的IP绑定信息:
//---------------------------------------#include <vcl.h>#include <Dhcpsapi.h>#include <Winsock2.h>#include <stdio.h>#pragma hdrstop#include <tchar.h>//#pragma comment (lib, "Dhcpsapi.lib")#pragma link "Dhcpsapi.lib"//---------------------------------------#pragma argsusedint _tmain(int argc, _TCHAR* argv[]){ wchar_t wcIP[255]; DHCP_RESUME_HANDLE hResumeHandle = 0; DWORD PrefMax = 255;//预计的最大租约个数 DWORD dwRead; DWORD dwTotal; LPDHCP_IP_ARRAY IPArray = NULL; DWORD dwRet = DhcpEnumSubnets(wcIP, &hResumeHandle, PrefMax, &IPArray , &dwRead, &dwTotal); for (DWORD i = 0; i < dwRead; i ++) { DWORD ip = IPArray->Elements[i];//子网地址 DHCP_RESUME_HANDLE hRH = 0; LPDHCP_CLIENT_INFO_ARRAY clientArray = NULL; DWORD PrefMaxClient = 65535; DWORD ClientsRead, ClientsTotal; DhcpEnumSubnetClients(wcIP, ip, &hRH, PrefMaxClient, &clientArray,&ClientsRead, &ClientsTotal); for (DWORD j = 0; j < ClientsRead; j ++) { //输出客户地址 //DHCP_IP_ADDRESS clientIP = clientArray->Clients[j]->ClientIpAddress; //这个是主机机名, 宽字串的:clientArray->Clients[j]->ClientName //MAC地址:一般是6个 clientArray->Clients[j]->ClientHardwareAddress->Data//// DWORD dwIP = inet_addr( "201.114.139.29 ");// struct sockaddr_in local;// local.sin_family = AF_INET;// local.sin_addr.s_addr = clientArray->Clients[j]->ClientIpAddress;//// char* IP = inet_ntoa(local.sin_addr);// char* MAC = clientArray->Clients[j]->ClientHardwareAddress.Data;//// printf("%s, %s\n", IP, MAC); } } system("PAUSE"); return 0;}//---------------------------------------