【问题标题】:passing a pointer to a struct, to a fuction将指向结构的指针传递给函数
【发布时间】:2021-07-17 17:07:46
【问题描述】:

我的 C++ 技能充其量是业余的,我需要编写一些代码以通过制造商提供的 .dll 与第 3 方传感器进行交互

因此,功能定义是由制造商提供的,但我真的不确定实现它们的正确方法。

根据 API 文档,我有两个结构,根据该文档定义,但我不知道如何将结构的指针传递给所需的函数 - HPK_GetSensorInformation(DeviceHandle, PUNIT_INTEGRATION_PARAMETER 积分参数, PUNIT_SENSOR_INFORMATION 传感器信息 );.

任何帮助将不胜感激。

谢谢!

附上代码:

#include <iostream>
#include <windows.h>
#include "CMOS_USB.h"

using namespace std;

int main(){

    typedef struct _tag_UnitIntegrationParameter {
    unsigned short Xray_Start_Threshold = 10;
    unsigned short Integration_Stop_Threshold = 20;
    double Integration_Time = 100;
    } UNIT_INTEGRATION_PARAMETER, *PUNIT_INTEGRATION_PARAMETER;

    typedef struct _tag_UnitSensorInformation {
        UNIT_INTEGRATION_PARAMETER IntegParam;
        unsigned short XXXX;
        unsigned char YY;
        unsigned char FW;
    } UNIT_SENSOR_INFORMATION,*PUNIT_SENSOR_INFORMATION;

    unsigned short ProductID = 0x4400;
    HANDLE DeviceHandle;
    HANDLE PipeHandle; 
    unsigned long sensor_return_status;



    DeviceHandle = WINAPI USB_OpenDevice(ProductID);

    PipeHandle = WINAPI USB_OpenPipe(DeviceHandle);

    sensor_return_status =  WINAPI HPK_GetSensorInformation(DeviceHandle,
        PUNIT_INTEGRATION_PARAMETER IntegParam,
        PUNIT_SENSOR_INFORMATION SensorInfo
    );

    WINAPI USB_CloseDevice(DeviceHandle);

    return 0;
}

【问题讨论】:

    标签: c++ pointers struct


    【解决方案1】:

    我无法帮助您处理任何特定于 Windows 的内容,但使用通用 C++:

    HPK_GetSensorInformation(DeviceHandle, &UNIT_INTEGRATION_PARAMETER, &UNIT_SENSOR_INFORMATION );.
    

    &amp; 表示“传递此结构的地址”。也就是说,它传递了一个指针。

    【讨论】:

    • 感谢您的回复!是的,我最后一次使用 Windows 是在 Vista 还是一件事的时候……但工作是一份工作。这给出了以下错误 - xray_detector.cpp:46:75: error: expected primary-expression before ‘,’ token I HPK_GetSensorInformation(DeviceHandle, &UNIT_INTEGRATION_PARAMETER, &UNIT_SENSOR_INFORMATION); ^ xray_detector.cpp:46:102: 错误: ')' 标记形成之前的预期主表达式(DeviceHandle, &UNIT_INTEGRATION_PARAMETER, &UNIT_SENSOR_INFORMATION);
    【解决方案2】:

    您看起来是在复制这些结构的定义,而不是创建一个。

    #include <iostream>
    #include <windows.h>
    #include "CMOS_USB.h"
    
    int main(){
    
        unsigned short ProductID = 0x4400;
        UNIT_SENSOR_INFORMATION SensorInformation;
    
        HANDLE DeviceHandle = USB_OpenDevice(ProductID);
    
        // not used?
        HANDLE PipeHandle = USB_OpenPipe(DeviceHandle);
    
        unsigned long sensor_return_status = HPK_GetSensorInformation(DeviceHandle, &SensorInformation.IntegParam, &SensorInformation);
    
        USB_CloseDevice(DeviceHandle);
    
        return 0;
    }
    

    【讨论】:

    • 太棒了,非常感谢!编译。我如何去改变其中一个结构中的值?例如,X 射线开始阈值。这很可能是一个愚蠢的问题,所以我感谢您的耐心等待。我是一个蟒蛇人...
    • @BenBird SensorInformation.IntegParam.Xray_Start_Threshold = some_value; struct 就像一个 python 类(虽然你不能在运行时添加或删除成员)
    • 太棒了。非常感谢你的直升机。我开始担心了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多