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

WIA Run-Time Check Failure #0 C++ (Windows Image Acquisition)

我尝试使用 WIA 将数据发送到相机。 我需要用 C++ 开发它,所以我在 https://docs.microsoft.com/en-us/windows/win32/wia/-wia-wia-tutorial 上使用了官方的 "tutorial" 我知道另一个与 C# 一起工作的项目,我想在 C++ 上做同样的事情 (C# 项目于:https://github.com/pixeltris/SonyAlphaUSB

当我想使用 Escape 方法时 https://docs.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://docs.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

现在 returns 未实现错误(但这目前对我来说不是那么重要)

更大的问题是

ppWiaExtra->Escape

returns E_FAIL 那个是未指定的,我认为这又是一些 pointer 问题或者我对 sizeof

的使用有问题
IWiaItemExtras* ppWiaExtra = (IWiaItemExtras*)ppWiaDevice;

这行不正确;您不能仅仅通过转换指针来获得不同的 COM 接口。这就是 QueryInterface 的用途:

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