从C#COM dll返回S_FALSE [英] Returning S_FALSE from a C# COM dll

查看:124
本文介绍了从C#COM dll返回S_FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a method defined in IDL as follows :

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

我总是使用C ++来实现此目标,

Using C++ I always implemented this as follows :

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并最终得到了:

Now I am implementing this in C# and have used tlbimp on the type library and ended up with :

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

我知道这是因为[out,retval]用作返回类型,而不是C ++中的HRESULT.有没有一种简单的方法就可以在不更改方法定义的情况下返回S_OK/S_FALSE值?我能看到的唯一方法是我必须使用ildasm/ilasm添加preservesig,所以最终得到这样的结果:

I understand that this is because [out, retval] is used as the return type instead of the HRESULT as in C++. Is there a simple way to return the S_OK / S_FALSE values without changing the method definition? The only way I can see is that I have to use ildasm / ilasm to add preservesig so I end up with something like this :

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

我想知道是否还有其他方法可以不执行il编译步骤.

I was wondering if there was some other way without doing the il compilation step.

推荐答案

尝试将/PreserveSig标志传递给tlbimp.那应该在方法中添加 PreserveSigAttribute 它产生.这是添加到最新版本的新功能.有关更多信息,请访问 tlbimp CodePlex网站.

Try passing the /PreserveSig flag to tlbimp. That should add the PreserveSigAttribute to the methods it generates. This is a new feature added to the latest version. More info is at the tlbimp CodePlex site.

这篇关于从C#COM dll返回S_FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆