Windows VC++2010链接错误_main



我试图通过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:usersvisual studio 2010ProjectsusbDebugusb.exe : fatal error LNK1120: 4 unresolved externals

它们是关于什么的?根本没有_main()。

首先,链接器错误不是因为缺少main,而是因为从main引用(调用)的其他函数。

链接器错误中提到的函数来自库:Setupapi.lib,您需要将其包含在项目的链接器设置(Input)中。

为什么它在VS重新打开时起作用

可能只是因为您(错误地)更改了配置。例如从Win32x64和/或从DebugRelease(或它们的任意组合)。其他配置没有添加对此库的引用。

最新更新