C#实现DDE(附完整源码)

C#实现DDE


下面是一个简单的示例,演示如何使用C#实现DDE(Dynamic Data Exchange)。

using System;
using NDde.Client;

class Program
{
    static void Main()
    {
        var client = new DdeClient("excel", "sheet1");
        client.Connect();

        client.Advise += OnAdvise;

        client.StartAdvise("A1", 1, true, 60000); // 请求接收单元格A1的更新

        Console.WriteLine("正在等待DDE数据更新...");
        Console.ReadLine();

        client.StopAdvise();
        client.Disconnect();
    }

    static void OnAdvise(object sender, DdeAdviseEventArgs args)
    {
        Console.WriteLine($"接收到DDE数据更新:{args.Text}");
    }
}

你需要引用 NDde 命名空间,可以通过NuGet安装 NDde 库。

这个示例连接到Excel的 sheet1 工作表,并请求接收A1单元格的更新。当Excel更新A1单元格时,OnAdvise 方法将被调用,并打印接收到的数据。

请注意,DDE是一种过时的通信协议,不再被广泛使用。它是由Microsoft在1980年代引入的,用于在应用程序之间传输数据。今天,更常见的是使用更现代的通信协议,如TCP/IP或WebSocket。

该博文为原创文章,未经博主同意不得转载。本文章博客地址:https://cplusplus.blog.csdn.net/article/details/137175477

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码大师

赏点狗粮吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值