为MT4导出非托管C#DLL(带名称空间)

时间:2016-05-18 16:41:43

标签: c# dll namespaces unmanaged metatrader4

我在 C# 中有代码,此代码应导出为未损坏的代码,因为它将由 MetaTrader Terminal 4 作为#import - ed库( .dll )。

  

最大的问题是:

   Q1 ) 我可以使用system.netsystem.net.mail名称空间导出为非托管DLL吗?

   Q2 ) 如果没有,有什么更好的解决方法(导出DLL作为非托管和引用名称空间)?

   Q3 ) 为什么导出不适用于此功能(EnviarCorreo)?

感谢。

using System;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Mail;
namespace Testme
{
    class Test
    {
        [DllExport("EnviarCorreo", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.LPWStr)]
        public static int EnviarCorreo(string ServidorSMTP, string usuario, string pass, bool HTML, string Asunto, string Cuerpo, string Desde, string Para)
    {
        MailMessage mail = new MailMessage();
        //direccion desde
        mail.From = new MailAddress("tester@domain.com", "Name");
        mail.ReplyTo = new MailAddress("info@domain.com");
        MailAddress toa = new MailAddress("admin@domain.com");

        string contenido = "Aquí vamos a escribir en HTML <br>Para ver</br>";
        mail.BodyEncoding = Encoding.Unicode;
        mail.SubjectEncoding = Encoding.Unicode;
        mail.To.Clear();
        mail.To.Add(toa);
        mail.Subject = "PRUEBA DE CORREO";
        mail.Body = contenido;// +Constants.vbCr + Constants.vbLf;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient("mail.domain.com");
        smtp.Credentials = new NetworkCredential("tester@domain.com", "blabla");
        smtp.Send(mail);
        return 158;
    }

        [DllExport("Add", CallingConvention = CallingConvention.StdCall)]
        public static int Add(int left, int right)
        {
            return left + right;
        }

        [DllExport("Sub", CallingConvention = CallingConvention.StdCall)]
        public static int Sub(int left, int right)
        {
            return left - right;
        }

        [DllExport("AddDouble", CallingConvention = CallingConvention.StdCall)]
        public static double AddDouble(double left, double right)
        {
            return left + right;
        }

        [DllExport("AddFloat", CallingConvention = CallingConvention.StdCall)]
        public static float AddFloat(float left, float right)
        {
            return left + right;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

.Net是一种托管语言,因此您无法创建非托管dll。它有点复杂,因为你的dll取决于框架。有办法做到这一点,但他们需要一些工作。看一下这篇文章的答案。

https://social.msdn.microsoft.com/Forums/en-US/afb244b3-1cd4-48b4-93ba-f019fdce6a8e/creating-an-unmanaged-dll-in-c?forum=csharplanguage