使用tlbimp.exe从类型库重新生成interop.dll时,打开数组声明丢失 [英] Open array declaration lost when regenerating interop.dll with tlbimp.exe from type-lib

查看:661
本文介绍了使用tlbimp.exe从类型库重新生成interop.dll时,打开数组声明丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从类型库中重新生成 interop.dll 。因此我启动了

I need to regenerate the interop.dll from a type-library. Therefore I fired up

TlbImp.exe" foo.tlb /out:interop.dll

但是与原来的 interop.dll 相反,我发现了对象浏览器(见下文)

But in contrast to the original interop.dll, I found out with the object browser (see below)

void SetNodeArr(int Size, FOO_NODE[] pArray)       // original interop.dll

SetNodeArr 的方法参数的open数组丢失:

that the open array for the method-parameter for SetNodeArr is lost:

void SetNodeArr(int Size, ref FOO_NODE pArray)     // regenrated interop.dll

我已经尝试过 TlbImp.exe ,但没有运气。

I've tried some of the options of TlbImp.exe, but with no luck.

/ strong>需要什么调整才能使参数 pArray 声明为开放数组而不是简单引用?

Q What tweak do I need to get parameter pArray declared as an open array instead of a simple reference?

推荐答案

这是一个很常见的错误,你可以通过从Visual Studio命令提示符运行OleView.exe来看到底层的问题。使用File + View Typelib并选择.tlb。你应该看到像:

This is a pretty normal mishap. You can see the underlying problem by running OleView.exe from the Visual Studio Command Prompt. Use File + View Typelib and select the .tlb. You should see something like:

   void SetNodeArr(int Size, FOO_NODE* pArray); 

指针是问题,它是不明确的。这可能意味着参数是一个数组,或者它可能意味着它是一个通过引用传递的结构。

The pointer is the problem, it is ambiguous. It could mean that the argument is an array or it could mean that it is a struct that's passed by reference.

对于COM服务器,你会看到这样的声明设计用于从C或C ++程序使用。

You'd see declarations like this for COM servers that were designed to be used from a C or C++ program. The kind of language where an array decays to a pointer to the first element when it is passed as an argument.

在将数组视为对象的语言中无法使用这种语言,例如.NET语言。一个友好的COM服务器支持许多不同种类的语言,将该参数声明为SAFEARRAY。现在它是无歧义的参数的类型,可以是,总是一个数组。不需要 Size 参数,一个安全的数组知道自己的大小。它是一个对象。

That cannot work in languages that treat arrays as objects, like the .NET languages. A friendly COM server that supports many different kind of languages will declare this argument as a SAFEARRAY instead. Now it is unambiguous what the type of the argument can be, that's always an array. Does not need the Size argument either, a safe array knows its own size. It is an object.

所以Tlbimp.exe没有机会猜到正确的翻译,它总是踢结构通过引用,最安全的选择。因此 ref FOO_NODE 。没有什么你可以做的,以使它任何明智的,当你困扰着COM服务器的实现方式。

So Tlbimp.exe doesn't stand a chance to guess at the proper translation, it always punts for struct-passed-by-reference, the safest choice. Thus ref FOO_NODE. There isn't anything you can do to make it any wiser about it either when you are stuck with the way the COM server is implemented.

修复互操作库是可能的,则该过程在 MSDN库。使用ildasm.exe反编译汇编,编辑IL以修复声明(首先使用示例C#代码查看它应该是什么样子),将humpty-dumpty与ilasm.exe重新放在一起

Fixing the interop library is possible, the procedure is described in the MSDN library. Decompile the assembly with ildasm.exe, edit the IL to fix the declaration (try it first with sample C# code to see what it should look like), put humpty-dumpty back together again with ilasm.exe

这篇关于使用tlbimp.exe从类型库重新生成interop.dll时,打开数组声明丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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