简体   繁体   English

如何从Delphi脚本中调用用c#编写的外部自定义.dll文件?

[英]how can i call the external custom .dll file written in c# from Delphi Script?

using System;

namespace Example1
{
class Program
  {
   public static void portAvailable(IPAddress ipa,int port)
    {
try
{
      System.Net.Sockets.Socket sock = new    
      System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
      System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
  sock.Connect(ipa, portno);
  if (sock.Connected == true)  // Port is in use and connection is successful
  MessageBox.Show("Port is Closed");
  sock.Close();

}
catch (System.Net.Sockets.SocketException ex)
{
if (ex.ErrorCode == 10061)  // Port is unused and could not establish connection 
     MessageBox.Show("Port is Open!");
else
     MessageBox.Show(ex.Message);
}

  }


 static void Main(string[] args)
  { 
            string hostName=Dns.GetHostname();
    int port=Convert.ToInt32(Console.ReadLine());
    IPAddress ipa = (IPAddress) Dns.GetHostAddresses(hostName)[0];
             portAvailable(ipa,port);


  }
 }
}

how can i call portAvailable() function from this .dll in Delphi for InnoSetup? 如何在Delphi中为此InnoSetup调用此.dll中的portAvailable()函数? what's wrong with code posted above ?i tried to access this .dll in my InnoSetup but when user entering port number it is not checking. 上面发布的代码出了什么问题?我试图在我的InnoSetup中访问这个.dll但是当用户输入端口号时它没有检查。 i supposed to install the setup in the available ports only.when the user enters the port which is in us then it should notify to ask for other port number.please solve this problem 我应该只在可用的端口安装设置。当用户进入我们的端口时,它应该通知要求其他端口号。请解决这个问题

  function portAvailable(
      ipa :IPAddress;
      port : int;):void;external 'Program.dll';

i am trying to call the function in Program.dll here. 我试图在这里调用Program.dll中的函数。

.NET DLL are not native DLL so you can't call methods directly usually, you have to expose them somehow using either a mixed mode C++/CLI assembly or COM. .NET DLL不是本机DLL,所以你不能直接调用方法,你必须使用混合模式C ++ / CLI程序集或COM以某种方式公开它们。

I'm not sure what COM interop Inno has (if any). 我不确定COM interop Inno有什么(如果有的话)。 You might find it easy to just build out an executable and call it from Inno with command line arguments. 您可能会发现构建可执行文件并使用命令行参数从Inno调用它很容易。

Addendum: 附录:

There's also a third party package called Unmanaged Exports you can try which works similar to DllImport , although I've not tried this myself. 还有一个叫做Unmanaged Exports的第三方软件包,你可以试试它类似于DllImport ,虽然我自己没试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM