自编写的Atl控件,在MFC对话框或C#中直接引用,自定义所有方法的返回值由HRESULT变为void型,怎样修改可以避免返回值类型任意修改。
Atl控件的返回值如:
HRESULT FT_StartRefresh();
引用后方法变为:
void FT_StartRefresh();
自编写的Atl控件,在MFC对话框或C#中直接引用,自定义所有方法的返回值由HRESULT变为void型,怎样修改可以避免返回值类型任意修改。
Atl控件的返回值如:
HRESULT FT_StartRefresh();
引用后方法变为:
void FT_StartRefresh();
该回答引用ChatGPT
在 ATL 中,方法的返回值类型是在接口定义中指定的。如果您在 ATL 控件中将返回值类型从 HRESULT 更改为 void,则在该控件的所有客户端代码中,该方法的返回值类型将变为 void。
如果您希望避免这种情况,可以使用 ATL 的属性宏来指定返回值类型。例如,使用ATL_NO_VTABLE和_ATL_FUNC_INFO宏来定义接口,并在函数声明中使用ATL_NOINLINE宏可以确保返回类型在客户端代码中正确地保持不变,如下所示:
// Sample interface definition with specified return value type
[object, uuid(F0123456-789A-BCDE-F012-3456789ABCDEF)]
__interface IMyInterface : IDispatch
{
// Use the ATL_NO_VTABLE macro to disable the vtable
// for the interface and ensure that the interface
// is not virtual.
ATL_NO_VTABLE HRESULT MyMethod([in] int arg1, [in] int arg2, [out, retval] long* retVal);
};
// Sample method implementation with specified return value type
// Use the ATL_NOINLINE macro to disable inline expansion of the method
ATL_NOINLINE STDMETHODIMP CMyObject::MyMethod(int arg1, int arg2, long* retVal)
{
// Implementation code goes here
return S_OK;
}
// Use the _ATL_FUNC_INFO macro to specify the function information for the method
BEGIN_COM_MAP(CMyObject)
COM_INTERFACE_ENTRY(IMyInterface)
END_COM_MAP()
IMPL_MYMETH(_ATL_FUNC_INFO(MyMethod), CMyObject, MyMethod, HRESULT (int, int, long*))
在这个示例中,IMyInterface 接口使用 ATL_NO_VTABLE 宏来指定不使用虚表,并且 MyMethod 方法使用 [out, retval] 标记来指定返回值类型为 long。在 MyMethod 的实现中,使用 ATL_NOINLINE 宏来禁用内联扩展。在 BEGIN_COM_MAP 中,将 IMyInterface 接口添加到 COM 映射中,并使用 IMPL_MYMETH 宏指定方法的函数信息和实现。
使用 ATL_NOINLINE 和 _ATL_FUNC_INFO 宏可以确保返回值类型在客户端代码中正确地保持不变。