C# #import of .NET out-of-proc 服务器的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:44:50  来源:igfitidea点击:

Problems with #import of .NET out-of-proc server

提问by jm.

In C++ program, I am trying to #import TLB of .NET out-of-proc server.

在 C++ 程序中,我试图#import .NET 进程外服务器的 TLB。

I get errors like:

我收到如下错误:

z:\server.tlh(111) : error C2146: syntax error : missing ';' before identifier 'GetType'

z:\server.tlh(111) : error C2501: '_TypePtr' : missing storage-class or type specifiers

z:\server.tli(74) : error C2143: syntax error : missing ';' before 'tag::id'

z:\server.tli(74) : error C2433: '_TypePtr' : 'inline' not permitted on data declarations

z:\server.tli(74) : error C2501: '_TypePtr' : missing storage-class or type specifiers

z:\server.tli(74) : fatal error C1004: unexpected end of file found

z:\server.tlh(111):错误 C2146:语法错误:缺少“;” 在标识符“GetType”之前

z:\server.tlh(111):错误 C2501:'_TypePtr':缺少存储类或类型说明符

z:\server.tli(74):错误 C2143:语法错误:缺少“;” 在 'tag::id' 之前

z:\server.tli(74):错误 C2433:“_TypePtr”:数据声明中不允许使用“内联”

z:\server.tli(74):错误 C2501:“_TypePtr”:缺少存储类或类型说明符

z:\server.tli(74):致命错误 C1004:发现文件意外结束

The TLH looks like:

TLH 看起来像:

_bstr_t GetToString();
VARIANT_BOOL Equals (const _variant_t & obj);
long GetHashCode();
_TypePtr GetType();
long Open();

I am not really interested in the having the base object .NET object methods like GetType(), Equals(), etc. But GetType() seems to be causing problems.

我对拥有像 GetType()、Equals() 等基础对象的 .NET 对象方法并不真正感兴趣。但 GetType() 似乎引起了问题。

Some google research indicates I could #import mscorlib.tlb(or put it in path), but I can't get that to compile either.

一些谷歌研究表明我可以#import mscorlib.tlb(或将其放在路径中),但我也无法编译。

Any tips?

有小费吗?

采纳答案by jm.

Added no_namespace and raw_interfaces_only to my #import:

将 no_namespace 和 raw_interfaces_only 添加到我的 #import 中:

#import "server.tlb" no_namespace named_guids

Also using TLBEXP.EXE instead of REGASM.EXE seems to help this issue.

同样使用 TLBEXP.EXE 而不是 REGASM.EXE 似乎有助于解决这个问题。

回答by jm.

Also, make sure your C# class doesn't have this attribute:

另外,请确保您的 C# 类没有此属性:

[ClassInterface(ClassInterfaceType.AutoDual)] <-- Seems to cause errors in C++ with _TypePtr

[ClassInterface(ClassInterfaceType.AutoDual)] <-- 似乎在 C++ 中使用 _TypePtr 导致错误

回答by jm.

Often, when MSVC compile COM source to a TLB, there'll be hints left behind like:

通常,当 MSVC 将 COM 源代码编译为 a 时TLB,会留下如下提示:

#import "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb"

You should add that to stdafx.hbefore the line

您应该将其添加到stdafx.h该行之前

#import "your_own.tlb"

After that, basic types like _Type, _ObjRefwill be added to your project for the prototypes generated.

之后,诸如_Type, 之类的基本类型_ObjRef将添加到您的项目中以生成原型。

I hope that solves your problem.

我希望这能解决你的问题。

but the bigger problem is that after everything is done, there might be runtime errors when you call a Ptr in you program

但更大的问题是,在一切都完成后,在程序中调用 Ptr 时可能会出现运行时错误

anyone can help?

任何人都可以帮忙吗?

回答by OndrejP_SK

It seems that you need to use

看来你需要使用

[ClassInterface(ClassInterfaceType.None)]

Here is another discussionabout the similar problem.

这是关于类似问题的另一个讨论

回答by ggo

#import "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb"

Was the solution for me.

是我的解决方案。