是否可以从C ++调用C#代码? [英] Is it possible to call C# code from C++ ?

查看:65
本文介绍了是否可以从C ++调用C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我知道我们可以使用COM互操作从C#调用C ++代码,怎么样呢?

反过来呢?


Ricky

Hi

I know that we can call C++ code from C# using COM interop, how about do
the reverse?

Ricky

推荐答案

Ricky,


如果要调用COM组件在.NET中,您只需在VS.NET项目中添加一个

引用(转到添加引用)。


如果你在命令行工作,你将运行TLBIMP

实用程序来生成一个Runtime Callable Wrapper,然后设置一个引用

那个。


如果C ++代码

通过DLL公开函数,你也可以通过P / Invoke层调用C ++代码。


或者,最后,您可以将C ++代码包装在托管包装器中(使用

Managed Extensions for C ++),然后将其称为普通程序集。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Ricky" < SP ** @ spam.com>在留言中写道

新闻:喔************** @ TK2MSFTNGP14.phx.gbl ...
Ricky,

If you want to call a COM component from .NET, you just have to add a
reference to it in your VS.NET project (go to "Add References").

If you were working from the command line, you would run the TLBIMP
utility to generate a Runtime Callable Wrapper, and then set a reference to
that.

You can also call C++ code through the P/Invoke layer, if the C++ code
exposes functions through a DLL.

Or, finally, you can wrap the C++ code in a managed wrapper (using
Managed Extensions for C++) and then call it like a normal assembly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricky" <sp**@spam.com> wrote in message
news:Ow**************@TK2MSFTNGP14.phx.gbl...


我知道我们可以使用COM互操作从C#调用C ++代码,反过来怎么样?

Ricky
Hi

I know that we can call C++ code from C# using COM interop, how about do
the reverse?

Ricky



Ricky,


如果你想从.NET调用一个COM组件,你只需要在它中添加一个

引用你的VS.NET项目(转到添加引用)。


如果你在命令行工作,你将运行TLBIMP

实用程序生成一个Runtime Callable Wrapper,然后设置一个引用

那个。


你也可以通过P / Invoke层调用C ++代码,如果C ++代码

通过DLL公开函数。


或者,最后,你可以将C ++代码包装在托管包装器中(使用

Managed Extensions for C ++)然后把它称为普通程序集。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard .caspershouse.com


" Ricky" < SP ** @ spam.com>在留言中写道

新闻:喔************** @ TK2MSFTNGP14.phx.gbl ...
Ricky,

If you want to call a COM component from .NET, you just have to add a
reference to it in your VS.NET project (go to "Add References").

If you were working from the command line, you would run the TLBIMP
utility to generate a Runtime Callable Wrapper, and then set a reference to
that.

You can also call C++ code through the P/Invoke layer, if the C++ code
exposes functions through a DLL.

Or, finally, you can wrap the C++ code in a managed wrapper (using
Managed Extensions for C++) and then call it like a normal assembly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricky" <sp**@spam.com> wrote in message
news:Ow**************@TK2MSFTNGP14.phx.gbl...


我知道我们可以使用COM互操作从C#调用C ++代码,反过来怎么样?

Ricky
Hi

I know that we can call C++ code from C# using COM interop, how about do
the reverse?

Ricky



是的,您可以将C#类公开为COM类。

但是,您必须遵守一些基本规则,并且最好为此设置一些

属性要正常工作,你还必须明确地注册

COM互操作程序集。 [1]。

有关详细信息,请查看COM互操作教程。在C#程序员中

参考"在MSDN上。


Willy。


[1] Windows XP SP2提供免注册COM互操作。设施。


" Ricky" < SP ** @ spam.com>在留言中写道

新闻:喔************** @ TK2MSFTNGP14.phx.gbl ...
Yes, you can expose C# classes as a COM classes.
However, you have to respect some basic rules and preferably set some
attributes for this to work correctly, you also have to explicitly "register
the assembly for COM interop" [1].
For details, check the "COM interop tutorials" in the C# programmers
reference" on MSDN.

Willy.

[1] Windows XP SP2 offers a "registration free COM interop" facility.

"Ricky" <sp**@spam.com> wrote in message
news:Ow**************@TK2MSFTNGP14.phx.gbl...


我知道我们可以使用COM互操作从C#调用C ++代码,反过来怎么样?

Ricky
Hi

I know that we can call C++ code from C# using COM interop, how about do
the reverse?

Ricky



这篇关于是否可以从C ++调用C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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