通过 lambda 在空指针处赋值
Assigning a value at void pointer via 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 = ¬ifyData;
return E_FAIL;
}));
但也不能体现新的价值。 返回的字节看起来没问题。
帮助将不胜感激。谢谢!
所以我现在能够让它工作。
看起来 void 指针不能直接分配,因为它没有"真实"类型。 所以我通过使用 memcpy 并在 lambda 中强制转换来分配一个值:
memcpy((stDriverNotify*)outBuffer, ¬ifyData, sizeof(stDriverNotify));
相关文章:
- 全局作用域中函数指针的赋值
- 错误:在为指针赋值时,void值没有被忽略
- 关于 c++ 函数中指针赋值的简单问题
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- VkSurfaceKHR 指针的值在函数调用后更改,无需任何显式赋值
- 赋值到类型和空大括号. 语法说明
- 通过 lambda 在空指针处赋值
- 从字符串数组打印其中一个字符串时出现空指针赋值错误
- 指向指针赋值的指针
- shared_ptr空指针和赋值
- 将指针赋值给指针(以及将指针传递给类)时使用delete的混淆
- 无效的转换?将const指针赋值给指针
- 我给一个指针赋值,它把自己空了
- 将const指针赋值给指针
- 是否应该将构造函数体中赋值的指针成员初始化为NULL ?
- 赋值包括指针的结构体
- 将此指针赋值给指针的右值引用
- 指针赋值与指针算术
- 如何将类对象赋值给指针
- 对象指针赋值的指针向量中的一个元素