首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VC >

VC2008 一个关于WIA的LNK2001: unresolved external symbol异常

2012-01-30 
VC2008 一个关于WIA的LNK2001: unresolved external symbol错误我的环境是 VS2008 SP1 + XP SP3, 本来打算

VC2008 一个关于WIA的LNK2001: unresolved external symbol错误
我的环境是 VS2008 SP1 + XP SP3, 本来打算写一个基于WIA的摄像头程序,所以这两天就把MSDN里的"Win32 and COM"->"Graphics and Multimedia"->"Windows Image Acquisition"->"WIA" 完完全全的看了一遍。然后试着按它里面的 "WIA Tutorial"把样本代码直接复制出来,想罗列一下当前系统里的 WIA Device, 结果发现编译能通过,但是老是报 error LNK2001: unresolved external symbol _CLSID_WiaDevMgr2,分析了半天,"WIA.H" 也是指向的正确的 VS2008 6.0A 的SDK,没有问题,我的源码里也已经加了 "windows.h","wia.h", 但 unresolved external symbol 错误就是要出来,难道是 6.0A SDK 里的 wia.h 和 lib 文件不匹配?还是 WIA.H 有问题?

6.0A的SDK已经支持 Vista, 所以我看 wia.h 里根据 _WIN32_WINNT 来包含 wia_lh.h 或 wia_xp.h
#if (_WIN32_WINNT >= 0x0600) // Longhorn
#include <wia_lh.h>
#elif (_WIN32_WINNT >= 0x0501) // Windows XP
#include <wia_xp.h>
#endif


从windows.h 反向追踪到 sdkddkver.h, 里面定义的是 #define _WIN32_WINNT 0x0600, 所以可以确认是引用的 wia_lh.h, 在 wia_lh.h 里,我是可以看到
EXTERN_C const CLSID CLSID_WiaDevMgr2;

#ifdef __cplusplus

class DECLSPEC_UUID("B6C292BC-7C88-41ee-8B54-8EC92617E599")
WiaDevMgr2;
#endif

这种定义的, 所以就不知道为啥为有LINK ERROR.

源代码很短,基本上就是从 MSDN 里 copy 出来的, 用的是 console 程序, 哪位如果有跟我相同的编译环境,可以帮我试试,看是不是一样的报错,能帮我解决问题就最好了。

#include "stdafx.h"
#include <windows.h>
#include <wia.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
OSVERSIONINFOEX osvi;
BOOL bOsVersionInfoEx;
BOOL bOsVistaOrLater;
IWiaDevMgr**ppWiaDevMgr;
IWiaDevMgr2**ppWiaDevMgr2;
HRESULT hr;

ppWiaDevMgr = NULL;
ppWiaDevMgr2 = NULL;
//Identify the OS version, XP or earlier use WIA interface, Vista or later will use WIA2 interface
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
  return 1;

if ( osvi.dwMajorVersion >= 6 && osvi.dwMinorVersion > 0 )
bOsVistaOrLater = true;//Vista later
else if ( osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0 )
{
if( osvi.wProductType == VER_NT_WORKSTATION )
bOsVistaOrLater = true;//Vista
else bOsVistaOrLater = false;//Windows Server 2008
}
else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
bOsVistaOrLater = false;//Windows 2000
else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
bOsVistaOrLater = false;//Windows XP
else if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
bOsVistaOrLater = false;//Windows Server 2003
else
bOsVistaOrLater = false;//Windows 98/Me/NT, etc...

//Create WIA Device Manager Object
*ppWiaDevMgr = NULL;
*ppWiaDevMgr2 = NULL;
if ( bOsVistaOrLater = false )
hr = CoCreateInstance( CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr, (void**)ppWiaDevMgr );
else
hr = CoCreateInstance( CLSID_WiaDevMgr2, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr2, (void**)ppWiaDevMgr2 );

if (!SUCCEEDED(hr)) return 2;

IEnumWIA_DEV_INFO *pWiaEnumDevInfo = NULL;

if (bOsVistaOrLater = false)
hr = (*ppWiaDevMgr)->EnumDeviceInfo( WIA_DEVINFO_ENUM_LOCAL, &pWiaEnumDevInfo );
else
hr = (*ppWiaDevMgr2)->EnumDeviceInfo( WIA_DEVINFO_ENUM_LOCAL, &pWiaEnumDevInfo );

if (!SUCCEEDED(hr)) return 3;

while (S_OK == hr)
{
IWiaPropertyStorage *pWiaPropertyStorage = NULL;
hr = pWiaEnumDevInfo->Next( 1, &pWiaPropertyStorage, NULL );
if (hr == S_OK)
{
//
// Declare PROPSPECs and PROPVARIANTs, and initialize them to zero.
//
PROPSPEC PropSpec[11] = {0};
PROPVARIANT PropVar[11] = {0};

//
// How many properties are you querying for?


//
const ULONG c_nPropertyCount = sizeof(PropSpec)/sizeof(PropSpec[0]);

//
// Define which properties you want to read:
// Device ID. This is what you would use to create
// the device.
//
PropSpec[0].ulKind = PRSPEC_PROPID;
PropSpec[0].propid = WIA_DIP_DEV_ID;//Device ID

PropSpec[1].ulKind = PRSPEC_PROPID;
PropSpec[1].propid = WIA_DIP_DEV_NAME;//Device Name

PropSpec[2].ulKind = PRSPEC_PROPID;
PropSpec[2].propid = WIA_DIP_DEV_DESC;//Device Description

PropSpec[3].ulKind = PRSPEC_PROPID;
PropSpec[3].propid = WIA_DIP_DEV_TYPE;//Device Type

PropSpec[4].ulKind = PRSPEC_PROPID;
PropSpec[4].propid = WIA_DIP_BAUDRATE;//The current baud rate setting for the device

PropSpec[5].ulKind = PRSPEC_PROPID;
PropSpec[5].propid = WIA_DIP_DRIVER_VERSION;//The current DLL version of the WIA minidriver

PropSpec[6].ulKind = PRSPEC_PROPID;
PropSpec[6].propid = WIA_DIP_HW_CONFIG;//The type of connection that the device is using

PropSpec[7].ulKind = PRSPEC_PROPID;
PropSpec[7].propid = WIA_DIP_PORT_NAME;//The installed device's port name

PropSpec[8].ulKind = PRSPEC_PROPID;
PropSpec[8].propid = WIA_DIP_UI_CLSID;//The vendor-supplied class identifier (CLSID) for any user interface (UI)

PropSpec[9].ulKind = PRSPEC_PROPID;
PropSpec[9].propid = WIA_DIP_VEND_DESC;//The vendor description string for the WIA minidriver

PropSpec[10].ulKind = PRSPEC_PROPID;
PropSpec[10].propid = WIA_DIP_WIA_VERSION;//The number (as a string) of the current WIA version that is installed on the system

//
// Ask for the property values
//
hr = pWiaPropertyStorage->ReadMultiple( c_nPropertyCount, PropSpec, PropVar );

if (!SUCCEEDED(hr)) return 4;

//
  // Free the returned PROPVARIANTs
  //
  FreePropVariantArray( c_nPropertyCount, PropVar );

pWiaPropertyStorage->Release();
pWiaPropertyStorage = NULL;
}
}

return 0;
}

LINK 错误信息为:
1>Linking...
1>temp.obj : error LNK2001: unresolved external symbol _CLSID_WiaDevMgr2
1>temp.obj : error LNK2001: unresolved external symbol _IID_IWiaDevMgr2
1>temp.obj : error LNK2001: unresolved external symbol _CLSID_WiaDevMgr
1>temp.obj : error LNK2001: unresolved external symbol _IID_IWiaDevMgr
1>C:\Documents and Settings\Bryan\My Documents\Visual Studio 2008\Projects\temp\Debug\temp.exe : fatal error LNK1120: 4 unresolved externals

[解决办法]

探讨
引用:
我没用过wia
wia 是基于com的 没看到CoInitialize


我的理解是 CoCreateInstance 有类似功能,但 LNK ERROR 应该不关 Coinitialize 或 CoCreateInstance 的事。

热点排行