COM 参数有in, out ,retval.

本文详细解析了COM接口中参数类型[in]、[out]及[out,retval]的作用与区别。介绍了这些参数如何影响函数调用及返回值处理,并通过具体实例展示了它们在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

COM 参数有in, out ,retval. 其主要作用:
  In 输入参数,它的值不被返回  
  Out 输出参数,必须是一个成员指针,将返回结果  
  RetVal 返回值,返回的值不能显示到用户向导中

[in]类型表明参数是一个输入参数,所以这个参数不会向外界返回结果
[out]类型表明参数是个输出参数,所以这个参数会向外界返回结果,而且传入参数时,可以将变量设置为NULL
[out,retval]类型表明参数是个输出参数,且会返回类中数据成员的值

那么从[out],[retval]类型的参数可以获取参数改变后的值,则函数的执行结果从函数的返回类型HRESULT来获得
   
  就Out,RetVal,都是返回值,具体区别在哪?
  • RetVal只是Out的一种特殊情况,对于wrapper class在碰到RetVal时,会将其作为一个返回值来处理,对调用方就可以直接拿它当函数的返回值,而不需要再用一个传出参数来接收返回的信息。
  • 在一个过程或函数当中RetVal,最多只能有一个。但是Out/in可以有多个(当然也不是无限的)。
  • [out,retval]表示参数方向是输出,同时可以作为函数运算结果的返回值。一个函数中,可以有多个[in]、[out],但[retval]只能有一个,并且要和[out]组合后在最后一个位置。
  • [in]类型表明参数是一个输入参数,所以这个参数不会向外界返回结果
    [out]类型表明参数是个输出参数,所以这个参数会向外界返回结果,而且传入参数时,可以将变量设置为NULL
    [out,retval]类型表明参数是个输出参数,且会返回类中数据成员的值

    那么从[out],[retval]类型的参数可以获取参数改变后的值,则函数的执行结果从函数的返回类型HRESULT来获得
    [in]类型表明参数是一个输入参数,所以这个参数不会向外界返回结果
    [out]类型表明参数是个输出参数,所以这个参数会向外界返回结果,而且传入参数时,可以将变量设置为NULL
    [out,retval]类型表明参数是个输出参数,且会返回类中数据成员的值
  • 那么从[out],[retval]类型的参数可以获取参数改变后的值,则函数的执行结果从函数的返回类型HRESULT来获得

使用ATL创建COM组件时,需要标明参数的类型为[in]、[out]或者[out,retval]。
通过Init方法返回一个BOOL型变量,
在IDL中的代码是:
HRESULT Init([out,retval]BOOL* result);
函数体为:
STDMETHODIMP CBrowserAD::Init(BOOL *result)
{
...
*result = true
return S_OK;
}
在COM客户端,这样使用的:
首先定义 BOOL* t;

得到Init函数中的返回值*result:

用2的方法,类型转换一下就可以了
*t = (BOOL)m_BrowserAD->Init();
*t = (BOOL)m_BrowserAD->Init();
``` /** @file Library for Phytium platform. Copyright (C) 2020, Phytium Technology Co Ltd. All rights reserved.<BR> SPDX-License-Identifier: BSD-2-Clause-Patent **/ #include <Library/ArmPlatformLib.h> #include <Library/DebugLib.h> #include <Library/IoLib.h> #include <Library/PcdLib.h> #include <Ppi/ArmMpCoreInfo.h> ARM_CORE_INFO mPhytiumMpCoreInfoTable[] = { { 0x0, 0x0, // Cluster 0, Core 0 // MP Core MailBox Set/Get/Clear Addresses and Clear Value (EFI_PHYSICAL_ADDRESS)0, (EFI_PHYSICAL_ADDRESS)0, (EFI_PHYSICAL_ADDRESS)0, (UINT64)0xFFFFFFFF } }; /* This function geted the current Boot Mode. This function returns the boot reason on the platform. @return Return the current Boot Mode of the platform. */ EFI_BOOT_MODE ArmPlatformGetBootMode ( VOID ) { return BOOT_WITH_FULL_CONFIGURATION; } /** Initialize controllers that must setup in the normal world. This function is called by the ArmPlatformPkg/Pei or ArmPlatformPkg/Pei/PlatformPeim in the PEI phase. @retval EFI_SUCCESS ArmPlatformInitialize() is executed successfully. **/ RETURN_STATUS ArmPlatformInitialize ( IN UINTN MpId ) { return RETURN_SUCCESS; } /** This function Inited the system (or sometimes called permanent) memory. This memory is generally represented by the DRAM. @param[in] None. @retval None. **/ VOID ArmPlatformInitializeSystemMemory ( VOID ) { // Nothing to do here } /** This function geted the information of core. @param[out] CoreCount The count of CoreInfoTable. @param[out] ArmCoreTable The pointer of CoreInfoTable. @retval EFI_SUCCESS PrePeiCoreGetMpCoreInfo() is executed successfully. **/ EFI_STATUS PrePeiCoreGetMpCoreInfo ( OUT UINTN *CoreCount, OUT ARM_CORE_INFO **ArmCoreTable ) { *CoreCount = PcdGet32 (PcdCoreCount); *ArmCoreTable = mPhytiumMpCoreInfoTable; return EFI_SUCCESS; } // // Needs to be declared in the file. Otherwise gArmMpCoreInfoPpiGuid is // undefined in the contect of PrePeiCore // EFI_GUID mArmMpCoreInfoPpiGuid = ARM_MP_CORE_INFO_PPI_GUID; ARM_MP_CORE_INFO_PPI mMpCoreInfoPpi = { PrePeiCoreGetMpCoreInfo }; EFI_PEI_PPI_DESCRIPTOR gPlatformPpiTable[] = { { EFI_PEI_PPI_DESCRIPTOR_PPI, &mArmMpCoreInfoPpiGuid, &mMpCoreInfoPpi } }; /** This function geted the information of Ppitable. @param[out] PpiListSize The size of Ppitable. @param[out] PpiList The pointer of Ppitable. @retval None. **/ VOID ArmPlatformGetPlatformPpiList ( OUT UINTN *PpiListSize, OUT EFI_PEI_PPI_DESCRIPTOR **PpiList ) { *PpiListSize = sizeof (gPlatformPpiTable); *PpiList = gPlatformPpiTable; }```分析一下
03-26
/** * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the * following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following * disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @file mcs_pid_ctrl.c * @author MCU Algorithm Team * @brief This file provides functions of general PID controller */ #include "mcs_pid_ctrl.h" #include "mcs_math.h" #include "mcs_assert.h" /** * @brief Reset all member variables of PID controller to zero. * @param pidHandle PID controller struct handle. * @retval None. */ void PID_Reset(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Reset the PID parameter. */ pidHandle->error = 0.0f; pidHandle->errorLast = 0.0f; pidHandle->feedforward = 0.0f; pidHandle->integral = 0.0f; pidHandle->saturation = 0.0f; pidHandle->differ = 0.0f; pidHandle->kp = 0.0f; pidHandle->ki = 0.0f; pidHandle->kd = 0.0f; pidHandle->ns = 0.0f; pidHandle->ka = 0.0f; /* Reset the Limiting Value. */ pidHandle->upperLimit = 0.0f; pidHandle->lowerLimit = 0.0f; } /** * @brief Clear historical values of PID controller. * @param pidHandle PID controller struct handle. * @retval None. */ void PID_Clear(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Clear historical values of PID controller. */ pidHandle->differ = 0.0f; pidHandle->integral = 0.0f; pidHandle->saturation = 0.0f; pidHandle->feedforward = 0.0f; pidHandle->error = 0.0f; pidHandle->errorLast = 0.0f; } /** * @brief Execute simplified PI controller calculation, static clamping, no feedfoward. * @param pidHandle PI controller struct handle. * @retval PI control output. */ float PI_Exec(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Proportional Item */ float p = pidHandle->kp * pidHandle->error; /* Integral Item */ float i = pidHandle->ki * pidHandle->error + pidHandle->integral; i = Clamp(i, pidHandle->upperLimit, pidHandle->lowerLimit); pidHandle->integral = i; /* static clamping and output calculaiton */ float val = p + i; float out = Clamp(val, pidHandle->upperLimit, pidHandle->lowerLimit); return out; } /** * @brief Execute PID controller calculation. dynamic clamping, feedfoward compenstaion * @param pidHandle PID controller struct handle. * @retval PID control output. */ float PID_Exec(PidHandle *pidHandle) { MCS_ASSERT_PARAM(pidHandle != NULL); /* Proportional Item */ float p = pidHandle->kp * pidHandle->error; /* Integral Item */ float i = pidHandle->ki * (pidHandle->error - pidHandle->ka * pidHandle->saturation) + pidHandle->integral; i = Clamp(i, Max(0.0f, pidHandle->upperLimit), Min(0.0f, pidHandle->lowerLimit)); pidHandle->integral = i; /* Differential Item */ float d = pidHandle->kd * pidHandle->ns * (pidHandle->error - pidHandle->errorLast) - \ pidHandle->differ * (pidHandle->ns - 1.0f); pidHandle->errorLast = pidHandle->error; pidHandle->differ = d; /* Output value update and saturation value calculation */ float val = p + i + d + pidHandle->feedforward; float out = Clamp(val, pidHandle->upperLimit, pidHandle->lowerLimit); pidHandle->saturation = val - out; return out; } /** * @brief Set the proportional parameter kp of PID controller. * @param pidHandle PID controller struct handle. * @param kp The proportional parameter. * @retval None. */ void PID_SetKp(PidHandle *pidHandle, float kp) { MCS_ASSERT_PARAM(pidHandle != NULL); MCS_ASSERT_PARAM(kp >= 0.0f); pidHandle->kp = kp; } /** * @brief Set the integral parameter ki of PID controller. * @param pidHandle PID controller struct handle. * @param ki The integral parameter. * @retval None. */ void PID_SetKi(PidHandle *pidHandle, float ki) { MCS_ASSERT_PARAM(pidHandle != NULL); MCS_ASSERT_PARAM(ki >= 0.0f); pidHandle->ki = ki; } /** * @brief Set the derivative parameter kd of PID controller. * @param pidHandle PID controller struct handle. * @param kd The derivative parameter. * @retval None. */ void PID_SetKd(PidHandle *pidHandle, float kd) { MCS_ASSERT_PARAM(pidHandle != NULL); MCS_ASSERT_PARAM(kd >= 0.0f); pidHandle->kd = kd; }
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值