我试图通过Microsoft的这个代码示例来调查Windows机器上的可用设备。但是存在与_main()相关的链接错误。
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//
// Call function with null to begin with,
// then use the returned buffer size (doubled)
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
// Double the returned buffersize to correct
// for underlying legacy CM functions that
// return an incorrect buffersize value on
// DBCS/MBCS systems.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_DEVICEDESC,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = LocalAlloc(LPTR,buffersize * 2);
}
else
{
// Insert error handling here.
break;
}
}
printf("Result:[%s]\n",buffer);
if (buffer) LocalFree(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return 1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
由于某些原因,存在链接错误:
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiDestroyDeviceInfoList@4 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetDeviceRegistryPropertyW@28 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiEnumDeviceInfo@12 referenced in function _main
1>device.obj : error LNK2019: unresolved external symbol __imp__SetupDiGetClassDevsW@16 referenced in function _main
1>c:\users\visual studio 2010\Projects\usb\Debug\usb.exe : fatal error LNK1120: 4 unresolved externals
它们是关于什么的?根本没有_main()。
答案 0 :(得分:0)
首先,链接器错误不是缺少main
,而是关于main
引用(调用)的其他函数。
您在链接器错误中提到的功能来自库: Setupapi.lib ,您需要将其包含在项目的链接器设置(输入)中。
为什么VS重新开放?
可能只是因为你改变了配置(错误地)。例如,从Win32
到x64
,和/或从Debug
到Release
(或任何组合)。其他配置没有引用此库添加。