首页 > 解决方案 > 将指向结构的指针传递给函数

问题描述

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

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

根据 API 文档,我有两个结构,根据该文档定义,但我无法弄清楚如何将结构的指针传递给所需的函数 - HPK_GetSensorInformation(DeviceHandle, PUNIT_INTEGRATION_PARAMETER IntegParam, PUNIT_SENSOR_INFORMATION SensorInfo);。

任何帮助将不胜感激。

谢谢!

附上代码:

#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++pointersstruct

解决方案


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

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

说“传递这个&结构的地址”。也就是说,它传递一个指针。


推荐阅读