c++ - 如何将 DeviceIOControl 移植到 ioctl?

标签 c++ c linux windows porting

根据我的理解,DeviceIOControl和ioctl是相同的功能。它们都向硬件发送控制代码并返回响应。为了重用代码,我尝试创建一个能够以跨平台方式工作的函数。因此,我决定使用 DeviceIOControl api,因为它是固定且特定的。问题是:如何将 ioctl 映射到它?

我目前有:

int DeviceIoControl_issueCommand(DeviceHandle handle, int command, void *input, ssize_t sizeof_input, void *output, ssize_t sizeof_output, uint32_t *bytes_written){
#if SYSTEMINFORMATION_ISWINDOWS
    int result = DeviceIoControl(handle,command,input,sizeof_input,output,sizeof_output,bytes_written,0);
    if (result == 0){
        result = -1; //-1 is the new error return
    }
    return result;
#else
    int result = ioctl(handle, command, input); //this doesnt work!
    return result;
#endif
}

非常感谢任何帮助!

最佳答案

如果没有DeviceIoControl_issueCommand中的大量内部翻译,您所问的问题是不可能的。函数调用完全不同,需要不同的参数和数据。您可以通过声明 IOControl 类并为您想要支持的每种 IO 功能类型添加成员函数来解决此问题。

class IOControl
{
public:
    void DoIoControlX();
    void DoIoControlY(int param1, int param2);
};

然后为您需要支持的每个平台提供实现。一种用于 Windows DeviceIOControl 调用,另一种用于支持 ioctl

的系统

关于c++ - 如何将 DeviceIOControl 移植到 ioctl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16113062/

相关文章:

正则表达式屏蔽文件中的特殊字符密码

c++ - C++ 中不稳定错误的最常见原因是什么?

c++ - Xcode 5 LLVM printf with %s format specifier not working in debug console

c - C中十六进制字符串到字节数组

c - 无符号字符困扰我的代码

linux - 如何在linux后台自动启动和推进应用程序中的几个步骤?

linux - 将 TCP 流重定向到另一个客户端

c++ - 在没有游戏引擎/渲染窗口的情况下将游戏服务器线程循环限制为 30FPS

c++ - 如何用c++和Opencv编写高斯混合模型

c - DLL 是否始终具有相同的基地址?