C#中应用DDE技术实现动态数据交换

 DDE是个很老的东西了,基本已经被淘汰。但有些地方还在使用这个东西,最近遇到个问题要使用这个东西,

比如MT4就运用DDE让自己成为服务端,程序可利用DDE获取MT4的外汇报价数据实现实时更新。

 
using NDde.Client; 
 
private void MainForm_Load(object sender, EventArgs e) 
{ 
    try 
    { 
        //申明并实例化一个DdeClient对象 
        client = new DdeClient("MT4", "BID", this); 
        client.Advise += client_Advise; 
        //连接到DDE服务器 
        client.Connect(); 
        //循环获取数据 
        client.StartAdvise("USDCHF", 1, true, 60000); 
    } 
    catch (Exception ex) 
    { 
        displayTextBox.Text = "MainForm_Load: " + ex.Message; 
    } 
} 
 
private void client_Advise(object sender, DdeAdviseEventArgs args) 
{ 
    //显示更新数据 
    displayTextBox.Text = "OnAdvise: " + args.Text; 
} 

这样就能实时获取MT4服务器的数据了。


  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
DDE(Dynamic Data Exchange)是一种在 Windows 操作系统使用的进程间通信(IPC)机制。使用DDE,一个应用程序可以向另一个应用程序发送数据或请求数据。在C#,可以使用System.Windows.Forms命名空间DdeClient类来实现DDE客户端。 以下是一个简单的DDE客户端示例,它将连接到Microsoft Excel并获取单元格A1的值: ```csharp using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Security.Permissions; namespace DDEClientExample { public partial class Form1 : Form { private const int WM_DDE_ACK = 0x03; private const int WM_DDE_DATA = 0x05; [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] protected override void WndProc(ref Message m) { if (m.Msg == WM_DDE_DATA) { // Get the data from the DDE server string data = Marshal.PtrToStringAuto(m.LParam); // Display the data MessageBox.Show(data); } else if (m.Msg == WM_DDE_ACK) { // The DDE server has acknowledged our connection request MessageBox.Show("Connected to DDE server."); } base.WndProc(ref m); } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Connect to Microsoft Excel DdeClient ddeClient = new DdeClient("excel", "sheet1"); // Subscribe to the DDE server's events ddeClient.Disconnected += DdeClient_Disconnected; ddeClient.Advise += DdeClient_Advise; // Connect to the DDE server ddeClient.Connect(); // Request data from the DDE server byte[] data = ddeClient.Request("A1"); // Disconnect from the DDE server ddeClient.Disconnect(); } private void DdeClient_Disconnected(object sender, EventArgs e) { MessageBox.Show("Disconnected from DDE server."); } private void DdeClient_Advise(object sender, DdeAdviseEventArgs e) { // Handle DDE advise events here } } } ``` 在此示例,我们使用 DdeClient 类连接到 Microsoft Excel 的“sheet1”工作表,并使用 Request 方法获取单元格 A1 的值。我们还订阅了 DDE 服务器的 Disconnected 和 Advise 事件。 请注意,DDE 已经过时,Microsoft 建议使用其他通信机制,例如 COM、.NET Remoting 或 WCF。如果可能的话,应该尽量避免使用 DDE

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值