WIA运行时检查失败#0 C++(Windows图像采集)



我尝试使用WIA向相机发送数据。我需要在C++中开发这个,所以我使用了上的官方"教程"https://learn.microsoft.com/en-us/windows/win32/wia/-wia-wia-tutorial我知道另一个使用C#的项目,我想在C上也这样做++(C#项目:https://github.com/pixeltris/SonyAlphaUSB)

当我想使用Escape方法时https://learn.microsoft.com/en-us/windows/win32/api/wia_xp/nf-wia_xp-iwiaitemextras-escape我有

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.
This is usually a result of calling a function declared with one calling
convention with a function pointer declared with a different calling convention.

错误,我希望你能帮助我解决这个问题。我的代码如下:

//initialize wia
HRESULT h = CoInitialize(NULL);
IWiaDevMgr* pWiaDevMgr = NULL;
//create wia device manager
HRESULT hr = CreateWiaDeviceManager(&pWiaDevMgr);
//show connected devices and get deviceId
BSTR bstrDeviceID = SysAllocString(L"");
HRESULT hr2 = EnumerateWiaDevices(pWiaDevMgr, &bstrDeviceID);
//create device with device id
IWiaItem* ppWiaDevice;
HRESULT hr3 = CreateWiaDevice(pWiaDevMgr, bstrDeviceID, &ppWiaDevice);
//case IWiaItem to IWiaItemExtras
IWiaItemExtras* ppWiaExtra = (IWiaItemExtras*)ppWiaDevice;
//try to send data
DWORD dwEscapeCode = 256;
BYTE* lpInData = new unsigned char[37]{ 0x01, 0x92, 0x00 , 0x00 , 0x00, 0x00, 0x00 , 0x00 , 0x00 , 0x01 ,
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00, 0x00, 0x00, 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x03 ,
0x00 ,   0x00 , 0x00 , 0x03 , 0x00 , 0x00 , 0x00 }; 
DWORD cbInDataSize = sizeof(lpInData);
BYTE* pOutData = new unsigned char[4]{ 0x00 , 0x00,0x00, 0x00};
DWORD dwOutDataSize = sizeof(pOutData);
DWORD pdwActualDataSize = NULL;
BSTR bstre = SysAllocString(L"");
//just to test if ppWiaExtra is working
HRESULT hr6 = ppWiaExtra->GetExtendedErrorInfo(&bstre); //works
//try to send data with Escape Method
//see https://learn.microsoft.com/en-us/windows/win32/api/wia_xp/nf-wia_xp-iwiaitemextras-escape
HRESULT hr5 = ppWiaExtra->Escape(dwEscapeCode, lpInData, cbInDataSize, pOutData, dwOutDataSize, &pdwActualDataSize); //Run-Time Check Failure #0

我使用的方法与上的示例代码中的方法相同https://github.com/microsoft/Windows-classic-samples/blob/master/Samples/Win7Samples/multimedia/wia/wiassamp/wiassamp.cpp

逃生方法是:

virtual /* [helpstring] */ HRESULT STDMETHODCALLTYPE Escape( 
/* [in] */ DWORD dwEscapeCode,
/* [size_is][in] */ __RPC__in_ecount_full(cbInDataSize) BYTE *lpInData,
/* [in] */ DWORD cbInDataSize,
/* [length_is][size_is][out] */ __RPC__out_ecount_part(dwOutDataSize, pdwActualDataSize ? *pdwActualDataSize : dwOutDataSize) BYTE *pOutData,
/* [in] */ DWORD dwOutDataSize,
/* [out] */ __RPC__out DWORD *pdwActualDataSize) = 0;

那么,我如何才能找到显示错误的实际问题,以及如何解决它呢?

(我使用WIA1,因为WIA2找不到设备(

STDMETHODCALLTYPE 

在winnt.h中定义如下:

#if defined(_WIN32) || defined(_MPPC_)
// Win32 doesn't support __export
#ifdef _68K_
#define STDMETHODCALLTYPE       __cdecl
#else
#define STDMETHODCALLTYPE       __stdcall
#endif
#define STDMETHODVCALLTYPE      __cdecl
#define STDAPICALLTYPE          __stdcall
#define STDAPIVCALLTYPE         __cdecl
#else
#define STDMETHODCALLTYPE       __export __stdcall
#define STDMETHODVCALLTYPE      __export __cdecl
#define STDAPICALLTYPE          __export __stdcall
#define STDAPIVCALLTYPE         __export __cdecl
#endif

编辑1:

GetExtendedErrorInfo

现在返回Not Implemented Error(但这对我来说目前不那么重要(

更大的问题是

ppWiaExtra->Escape

返回E_FAIL,而该值未指定,我认为这又是pointer的问题,或者是我使用sizeof的问题

IWiaItemExtras* ppWiaExtra = (IWiaItemExtras*)ppWiaDevice;

此行不正确;你不能仅仅通过投射指针来获得不同的COM接口。这就是QueryInterface的用途:

IWiaItemExtras* ppWiaExtra;
HRESULT result = ppWiaDevice->QueryInterface(IID_PPV_ARGS(&ppWiaExtra));

最新更新