C#与delphi集成 [英] C# with delphi integration

查看:109
本文介绍了C#与delphi集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,

I would like to know if it is possible to have a C# windows form integrate into delphi, so that the form sits inside a delphi frame.

Thanks





我尝试过:



我创建了一个Windows窗体项目,通过更改输出类型将其转换为dll,从控制台应用程序调用,它可以工作。



What I have tried:

I have created a windows form project, converted it to dll by changing the output type, called from a console application, and it works.

推荐答案

如果你在谈论Delphi for .NET,那么这个问题就不存在了。您可以使用不同的.NET语言创建不同的程序集并自由组合它们。



如果您指的是针对本机代码的Delphi项目,这真的很难做到。反过来说,使用在Delphi中创建的DLL不会有问题。但是,在本机项目中使用.NET UI的常用方法是使用.NET创建ActiveX控件,但我不想讨论它,因为它使用了过时的技术。您可以随时在MSDN文档中阅读它。



另一种方法是将您在.NET程序集中创建的.NET方法导出为本机。这不是一个简单的方法(许多人甚至会说这是不可能的,但实际上它并不是由Visual Studio常规支持,但它允许由CLI支持)。请参阅这些CodeProject答案:

.Net中的API:托管代码或非托管代码 [ ^ ],

从无人码VC ++中调用用C#编写的托管DLL [ ^ ],

我如何使用创建的DLL在Visual Basic 2008中的Visual Basic 2008中 [ ^ ],

在MFC中加载C#DLL [ ^ ]。



另一种方法是使用C ++ / CLI而不是C#。更准确地说,您的主要程序集可以在C#中,但您也可以在C ++ / CLI中创建一个中间层项目,其唯一目的是通过将托管代码包装在一些非托管函数中来公开托管代码,然后您可以在Delphi项目中使用它们。以通常的方式链接DLL并使用这些导出的函数。请看我过去的答案:

如何在c ++ cli dll中将ac#app中的字符串传递给类ctor? [ ^ ],

键盘挂钩如何工作以及如何实施 [ ^ ]。



请不要混淆:尽管我讨论使用C ++ / CLI调用非托管代码,但它可以用于其他方式。



现在,最后但并非最不重要:认真思考:你真的需要麻烦吗?

Delphi非常强大,你可以在没有.NET帮助的情况下使用Delphi做很多事情。如果您在使用某些功能时遇到问题,也许您可​​能想问一些问题。更好的是,你可以使用像Lazarus那样的开源Free Pascal这样一个很好的.NET替代品。 Lazarus不仅是IDE,它还提供了lcl UI库,Delphi VCL的模拟。主要区别在于:它确实是多平台的。在实践中,它涵盖了比.NET和许多其他技术更多的平台。令人惊讶的是,lcl也是多平台的。你真的可以为很多操作系统提供相同的代码,即使对于非常小的嵌入式或智能手机设备也是如此。

请参阅:

Lazarus wiki [ ^ ],
Lazarus主页 [ ^ ]。



当您下载并安装Lazarus时,Free Pascal和所有库都包含在内。可能这是.NET最强的替代品,但它是原生的;您必须分别在每个平台上编译项目。还提供了一些跨平台编译的支持,但此时此刻更难。



-SA
If you are talking about Delphi for .NET, such problem simply does not exist. You can create different assemblies in different .NET languages and freely combine them.

This is really difficult to do if you mean Delphi projects targeted to native code. The other way around, using a DLL created in Delp would not be a problem. However, the usual way of using .NET UI in native projects would be creation of an ActiveX control using .NET, but I don't want to discuss it, as it uses obsolete technology. You can always read about it in MSDN documentation.

Another alternative is exporting .NET methods you create in your .NET assembly to native. This is not a trivial approach (many would even say it is impossible, but in fact it is not routinely supported by, say, Visual Studio, but it allowed by the CLI standad). Please see these CodeProject answers:
API's in .Net: Managed or UnManaged Code[^],
Call Managed DLL written in C# from Unmanged Code VC++[^],
How can I use a dll created in Visual Basic 2008 in Visual Basic 6.0[^],
loading C# DLL in MFC[^].

Another approach would be using C++/CLI instead of C#. More exactly, you main assemblies can be in C#, but you can also created an intermediate-layer project in C++/CLI with the sole purpose of exposing managed code by wrapping it in some unmanaged functions which you can then use in your Delphi project by linking a DLL in a usual way and using those exported functions. Please see my past answers:
How do I pass a string from a c# app to class ctor in a c++ cli dll?[^],
How the Keyboard Hook works and how it is being implemented[^].

Please don't be confused: even though I discuss using C++/CLI for invocation of unmanaged code, it can be used for the other way around.

And now, the last but not the least: think seriously: do you really need all the trouble?
Delphi is very powerful, you can do a lot with Delphi along, without the help of .NET. If you have trouble with some functionality, perhaps you may want to ask some questions. Better yet, you can use such a great alternative to .NET as open-source Free Pascal, with Lazarus. Lazarus is not only the IDE, it also provides the lcl UI library, the analog of Delphi VCL. The major difference is: it is truly multi-platfrorm. In practice, it covers a lot more platforms than .NET and many other technologies. Amazingly, lcl is multi-platform, too. You really can have the same code for very many OS, even for really tiny embedded or smartphone devices.
Please see:
Lazarus wiki[^],
Lazarus Homepage[^].

When you download and install Lazarus, Free Pascal and all the libraries are included. Probably this is the strongest alternative to .NET available, but it's native; you will have to compile your projects on each platform separately. Some support of cross-platform compilation is also provided, but at this moment it's more difficult.

—SA


这篇关于C#与delphi集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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