windows 什么时候返回 E_POINTER,什么时候返回 E_INVALIDARG?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1426672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:08:12  来源:igfitidea点击:

When return E_POINTER and when E_INVALIDARG?

windowscompointerscom-interop

提问by sharptooth

COM interfaces methods can return various HRESULTvalues to signal invalid argument values passed. When do I return E_POINTERand when E_INVALIDARG?

COM 接口方法可以返回各种HRESULT值来表示传递的无效参数值。我什么E_POINTER时候回来,什么时候回来E_INVALIDARG

As I understand if a method receives an index in an encapsulated collection and it is out of bounds that is E_INVALIDARG. If a method receives an Interface**pointer where it is meant to store a pointer to a newly created object that's E_POINTER.

据我了解,如果一个方法在封装的集合中接收一个索引并且它超出了E_INVALIDARG. 如果一个方法接收到一个Interface**指针,用于存储指向新创建的对象的指针,该对象是E_POINTER.

HRESULT CImpl::GetItem( long index; Interface** result )
{
    if( result == 0 ) {
        return E_POINTER;
    }
    if( index < 0 || index >= internalArray.size() ) {
        return E_INVALIDARG;
    }
    *result = CreateWrapperObject( internalArray[index] );
    return S_OK;
}

But what if it receives a WCHAR*buffer with a file name as an "in" parameter and this WCHAR*is null? Is this E_POINTERor E_INVALIDARG?

但是如果它接收一个WCHAR*带有文件名作为“in”参数的缓冲区并且它WCHAR*是空的呢?这是E_POINTER还是E_INVALIDARG

Or a method receives a pointer to some struct and is expected to fill the struct through that pointer and this pointer is null - is this E_POINTERor E_INVALIDARG?

或者一个方法接收一个指向某个结构的指针,并期望通过该指针填充该结构并且该指针为空 - 这是E_POINTER还是E_INVALIDARG

HRESULT CImpl::SaveToFile( WCHAR* fileName )
{
    if( fileName == 0 ) {
       return // what to return here?
    }
    //... do actual work here
}

HRESULT CImpl::GetAttributes( Attributes* to )
{
    if( to == 0 ) {
       return // what to return here?
    }
    attributes->IsCool = getIsCool();
    attributes->Color = RGB( 0, 255, 0 );
    return S_OK;
}

What are the rules for when to return E_POINTERand when E_INVALIDARGwhen checking pointer type parameters?

何时返回E_POINTER以及E_INVALIDARG何时检查指针类型参数的规则是什么?

回答by Shay Erlichmen

You return E_POINTERwhen the reference to the pointer of an outparameter is null, this consider to be an error code that indicates a bug in the program or in the interop layer.

返回E_POINTER时,引用到的指针参数为空,这样认为是一个错误代码,指明在程序或在互操作层的错误。

You return E_INVALIDARGwhen there is an application level on with the parameter for example out of range issue or parameter that collide with each other.

E_INVALIDARG当应用程序级别与参数(例如超出范围的问题或相互冲突的参数)时,您将返回。

In Your case, in SaveToFile(...)you should return E_INVLIADARGsince it is not valid to pass empty file name, and in GetAttributes(...)you should return E_POINTER(if it's an out param) because you cannot fill the value.

在您的情况下,SaveToFile(...)您应该返回,E_INVLIADARG因为传递空文件名无效,并且GetAttributes(...)您应该返回E_POINTER(如果它是一个 out 参数),因为您无法填充该值。

And yes, we all L-O-V-E com :)

是的,我们都喜欢 com :)