通过 lambda 在空指针处赋值



在我的测试中,我想填充内容"outBuffer":

stDriverNotify* notifyData = new stDriverNotify;
notifyData->NotifyType = eDriverNotify_InputRedirect; 
EXPECT_CALL(*hidDeviceMock_, DeviceControl(_, _, _, _, _, _, _))
.WillRepeatedly(Invoke([&](ULONG control, PVOID inBuffer, ULONG inSize, 
PVOID outBuffer, ULONG outSize, PULONG bytesReturned, LPOVERLAPPED ov)
{
*bytesReturned = 32;
outBuffer = notifyData;
return E_FAIL;
}));

但是,notifyData 的 notifyType 未更新为 eDriverNotify_InputRedirect。 你明白为什么吗?

我也试过这个:

stDriverNotify notifyData;
notifyData.NotifyType = eDriverNotify_InputRedirect; 
EXPECT_CALL(*hidDeviceMock_, DeviceControl(_, _, _, _, _, _, _))
.WillRepeatedly(Invoke([&](ULONG control, PVOID inBuffer, ULONG inSize, 
PVOID outBuffer, ULONG outSize, PULONG bytesReturned, LPOVERLAPPED ov)
{
*bytesReturned = 32;
outBuffer = &notifyData;
return E_FAIL;
}));

但也不能体现新的价值。 返回的字节看起来没问题。

帮助将不胜感激。谢谢!

所以我现在能够让它工作。

看起来 void 指针不能直接分配,因为它没有"真实"类型。 所以我通过使用 memcpy 并在 lambda 中强制转换来分配一个值:

memcpy((stDriverNotify*)outBuffer, &notifyData, sizeof(stDriverNotify));

最新更新