代码之家  ›  专栏  ›  技术社区  ›  AntonyW

从C COM DLL返回S FALSE

  •  3
  • AntonyW  · 技术社区  · 16 年前

    我在IDL中定义了一个方法,如下所示:

    interface IMyFunc : IDispatch
    {
        [id(1), helpstring("method GetNextFunction")] HRESULT GetNextFunction(
            [in,out] long* lPos, [out, retval] BSTR* bstrName);
    }
    

    使用C++我总是实现如下:

    STDMETHODIMP CMyFunc::GetNextFunction(long *nID, long *lPos, BSTR *bstrName)
    {
        if ( function to return )
        {
            // setup return values;
            return S_OK;
        }
        else
        {
            // just exit
            return S_FALSE;
        }
    }
    

    现在我在C中实现了这一点,并在类型库上使用了tlbimp,最后得到了:

    public string GetNextFunction(ref int nID, ref int lPos)
    

    我理解这是因为[OUT,RESVAL]被用作返回类型而不是像C++中的HESREST。是否有一种简单的方法可以在不更改方法定义的情况下返回s_ok/s_false值?我能看到的唯一方法是,我必须使用ildasm/ilasm添加preservesig,这样我最终得到如下结果:

    public int GetNextFunction(ref int nID, ref int lPos, ref string bstrName)
    

    我想知道在不执行IL编译步骤的情况下是否还有其他方法。

    2 回复  |  直到 16 年前
        1
  •  2
  •   cpalmer    16 年前

    尝试通过 /PreserveSig 标记为tlbimp。应该加上 PreserveSigAttribute 它生成的方法。这是添加到最新版本的新功能。更多信息请访问 tlbimp CodePlex site .

        2
  •  1
  •   Community Mohan Dere    9 年前

    作为 mentioned in another answer ,您可以使用 /PreserveSig 用于指示要附加 PreserveSig attribute 到在生成的接口上导出的所有方法。

    如果不想将preservesig属性应用于所有方法,那么可以在C代码中轻松定义COM接口,然后应用preservesig属性并相应地修改函数的签名。然后,您可以在tlbimp中从您的co类中强制转换类实现,并根据需要使用接口定义。

    推荐文章