我在 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, retval] 被用作返回类型,而不是像 C++ 中的 HRESULT 。有没有一种简单的方法可以在不更改方法定义的情况下返回 S_OK / S_FALSE 值?我能看到的唯一方法是我必须使用 ildasm / ilasm 来添加preservesig,所以我最终会得到这样的结果:
public int GetNextFunction(ref int nID, ref int lPos, ref string bstrName)
我想知道是否有其他方法无需执行 il 编译步骤。