在C 6.0程序中调用C#函数 [英] Calling a C# functions in a C 6.0 program

查看:72
本文介绍了在C 6.0程序中调用C#函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们从客户端收到了一个C#库(如果使用特定设置编译该库,我们对此库一无所知),我们需要使用用C编写的旧程序中的函数之一.调用该函数,但是预期返回的参数是错误的(该函数是加密的,我们提供了密钥和字符串,并期望返回另一个加密的字符串).有谁能提供一个简单的例子.

We receive a C# library from a client (we don''t know anything about this library if it was compiled with specific settings) and we need to use one of the function in our old program written in C. We succeed to call the function but the expected returned parameter is wrong (the function is an encryption, we provide a key and a string and expect in return another string crypted). Does anyone can provide a simple example.

推荐答案

没有一个简单的例子.大多数人说,将C#方法导出为非托管是不可能的.一种解决方法是通过COM执行此操作,我不喜欢也不推荐这样做.

因此,如何导出.NET组装方法如何:是否可能.事实是:是和不是.在纯C#中,这是不可能的,但是我可以在一些CodeProject文章中显示解决方法是可行的.在高级语言中没有这样的东西,但是IL可以做到.这是标准的一部分,因此解决方案不是黑客.这个想法是标记要导出的方法,该方法不受特殊的自定义.NET属性的管理.需要一个额外的构建步骤;将对目标程序集进行反编译,修改标记有属性的方法以允许对其进行非托管导出,并将获得的代码再次组装到修改后的程序集中.该程序集在功能上与原始程序集等效,但是某些方法被导出为非托管方法.生成的程序集可以双重使用:就像常规的.NET程序集一样,也可以用作可以在C项目中使用的本机DLL.

有关更多详细信息和对原始文章的引用,请参阅我过去的回答:
如何使用在Visual Basic 6.0中的Visual Basic 2008中创建的dll [从Unmanged Code VC ++用C#调用托管DLL [ ^ ],
.a中的 API:托管或非托管代码 [ ^ ].

另一种方法是使用以C ++和C ++/CLI编写的混合模式(托管+非托管)项目.您可以引用.NET程序集,在C ++/CLI中创建包装器代码,在某些要导出的C ++方法中使用此包装器.这需要中间的混合模式项目,但也有其吸引人的一面,特别是对于那些喜欢C ++的人.

—SA
There is no a simple example. Most people say that exporting C# method as unmanaged is impossible. One work-around is doing it through COM, which I dislike and don''t recommend.

So, how about exporting of methods of .NET assembly as umnanaged: is it possible or not. The truth is: yes and no. In pure C# this is impossible, but I can across some CodeProject articles showing that a work-around is possible. There is not such thing in high-level language, but IL can do, it; and this is a part of the standard, so the solution is not a hack. The idea is marking of the method to be exported unmanaged by a special custom .NET attribute. An additional build step is required; a target assembly is decompiled, methods marked with the attributes are modified to allow exporting them unmanaged, and obtained code is assembled again into the modified assembly. This assembly is functionally equivalent to the original one, but some methods are exported as unmanaged. The resulting assembly can be used dually: like a regular .NET assembly or as a native DLL which can be used in a C project.

For further detail and referenced to original articles, please see my past answers:
How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^],
Call Managed DLL written in C# from Unmanged Code VC++[^],
API''s in .Net: Managed or UnManaged Code[^].

Alternative method is using mixed-mode (managed + unmanaged) project written in C++ and C++/CLI. You can reference your .NET assembly, create a wrapper code in C++/CLI, use this wrapper in some C++ methods to be exported. This requires intermediate mixed-mode project but has its attractive sides, especially for those who prefers C++.

—SA


这篇关于在C 6.0程序中调用C#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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