从 C# COM dll 返回 S_FALSE
我在 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 编译步骤。
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);
}
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;
}
}
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)
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)
I was wondering if there was some other way without doing the il compilation step.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
/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.正如另一个答案中提到的,您可以使用
/PreserveSig
标志,指示您要附加 PreserveSig 属性 到在生成的接口上导出的所有方法。如果您不想将 PreserveSig 属性应用于所有方法,则可以轻松地在 C# 代码中定义 COM 接口,然后应用 PreserveSig 属性并相应地修改函数的签名。然后,您可以从 TLBIMP 中的协同类中转换类实现,并根据需要使用您的接口定义。
As mentioned in another answer, you can use the
/PreserveSig
flag to indicate that you want to attach the PreserveSig attribute to all of the methods that are exported on the interfaces that are generated.If you don't want to apply the PreserveSig attribute to all of the methods, then you can easily define the COM interface in C# code and then apply the PreserveSig attribute and modify the signature of the functions accordingly. Then, you can cast the class implementations from your co classes in TLBIMP and work with your interface definition as you wish.