search
HomeWeb Front-endFront-end Q&AExplore how to implement DDE using JavaScript

JavaScript is a general scripting language mainly used to implement interactive functions in front-end web pages. However, we can also use it to implement a variety of operations, and even use it to implement DDE (Dynamic Data Exchange).

DDE is a method of Windows inter-application communication (IPC). It allows data to be shared between two or more programs. Specifically, it allows one Windows application to pass data to another, typically another running application.

In this article, we will explore how to implement DDE using JavaScript. Before you begin, make sure you have basic knowledge of working with JavaScript and Windows.

Understanding DDE

Before we begin, let’s briefly understand DDE. DDE is an IPC (Inter-Process Communication) technology in Windows. It allows applications to send and receive various messages and commands. As we mentioned earlier, it allows one application to send data to another application. This is called the "sender" application and the "receiver" application.

To understand the functions of DDE, you need to understand the following two concepts:

  • DDE Client: It is the application through which we send data.
  • DDE Server: It is the application that we use to receive data.

When the DDE client sends data to the DDE server, it first packages the data into a DDE transaction. This DDE transaction includes the following:

  • Application name
  • Topic name
  • Item name
  • Operation type (for example, open, close, Send, etc.)
  • Status (success or failure)

The DDE client sends the DDE transaction to the DDE server. When the DDE server receives a transaction, it parses the transaction, extracts the data (if needed) and sends a response to the DDE client.

Now, let’s see how to implement DDE using JavaScript.

Implementing the DDE client

Implementing the DDE client in JavaScript requires the use of ActiveXObject objects. This object is a type of "ActiveX control" developed by Microsoft that provides common Windows components that allow Web developers to perform many Windows operations.

Next, we will use the ActiveXObject object to create a DDE client.

The following is a simple example demonstrating how to create a DDE client using JavaScript:

function sendDDEMessage(appName, topicName, itemName, command) {
  var ddeClient = new ActiveXObject("DDE.DdeClient");
  ddeClient.Connect(appName, topicName);

  var ddeTransaction = ddeClient.BeginTransaction();
  var ddeData = ddeTransaction.AddItem(itemName);
  ddeTransaction.SetData(ddeData, command);
  ddeTransaction.SetFormats(ddeData, 1); // 1 = CF_TEXT
  ddeTransaction.CommitTransaction();
  
  ddeClient.Disconnect();
}

As shown above, the sendDDEMessage function accepts four parameters:

  • appName: Contains the application name of the DDE server.
  • topicName: Contains the name of the DDE server context.
  • itemName: Contains the name of the object to be accessed in the DDE server application.
  • command: The command or message to be sent to the DDE server.

This function first uses new ActiveXObject("DDE.DdeClient") to create a DDE client. Next, it uses the Connect() method to connect the client to the specified application and topic.

Next, it creates a DdeTransaction instance and adds the item to be accessed using the AddItem() method. Then, use the SetData() method to set the command or message to be sent into the DdeData instance. Finally, use the SetFormats() method to set the data format. 1 is used here, indicating the CF_TEXT format.

Finally, the function uses the CommitTransaction() method to commit the transaction and the Disconnect() method to disconnect the client from the DDE server.

Implementing the DDE server

Although the implementation of the DDE server is relatively complex, we can easily simulate it using JavaScript and ActiveXObject objects.

In this example, we will simulate a DDE server with the following functions:

  • It can receive commands from the client and save them in an array.
  • When it receives a command named "get_commands", it will use the returnCommandList() method to return a string containing all previously received commands.

The following is the JavaScript code of the DDE server:

function DDEServer(appName, topicName) {
  var self = this;
  self.appName = appName;
  self.topicName = topicName;
  self.commandList = [];

  self.connect = function() {
    self.ddeServer = new ActiveXObject("DDE.DdeServer");
    self.ddeServer.Register(appName, topicName);
  };

  self.disconnect = function() {
    self.ddeServer.Unregister();
  };

  self.handleTransaction = function(ddeTransaction) {
    var command = ddeTransaction.GetData(ddeTransaction.FirstItem);
    self.commandList.push(command);
  };

  self.returnCommandList = function() {
    return self.commandList.join('\r\n');
  };
}

As shown above, the DDEServer constructor accepts two parameters: appName and topicName, these parameters are used to connect to the DDE server application and topic.

connect() method uses new ActiveXObject("DDE.DdeServer") Create a DDE server and use the Register() method Register it under the specified application and theme. The

disconnect() method uses the Unregister() method to unregister the DDE server.

When the DDE server receives the transaction, the handleTransaction() method is called. It fetches the data from the transaction and adds it to the server-side command list.

Finally, when the server receives the command named "get_commands", the returnCommandList() method will use the join() method to join all the commands in the command list The command is concatenated into a string and returned.

测试示例

现在我们已经开始实现 DDE 客户端和服务端,让我们来看看一些示例。为了测试客户端和服务端,我们将创建一个简单的 HTML 页面,该页面包含两个文本框和两个按钮。第一个文本框将用于输入命令,第二个文本框将用于显示服务器端的响应。

在按钮上单击时,客户端将尝试连接到服务器端,并将命令发送到它。一旦服务器端接收到命令,它将保存它,并返回一个包含所有已接收到的命令的字符串。

以下是示例代码:

nbsp;html>

  
    <meta>
    <title>DDE Client Demo</title>
  
  
    <h1 id="DDE-Client-Demo">DDE Client Demo</h1>
    <label>Enter a command:</label>
    <input>
    <button>Send Command</button>
    <br>
    <label>Output:</label>
    <textarea></textarea>
    <script>
      var ddeServer = new DDEServer("dde_demo", "demo_topic");
      ddeServer.connect();

      function sendCommand() {
        var command = document.getElementById("command").value;
        sendDDEMessage("dde_demo", "demo_topic", "command", command);
        var output = document.getElementById("output");
        output.value = sendDDEMessage("dde_demo", "demo_topic", "get_commands", "");
      }

      document.getElementById("sendCommand").addEventListener("click", sendCommand);
    </script>
  

如上所示,我们使用了 var ddeServer = new DDEServer("dde_demo", "demo_topic") 创建了一个 DDE 服务端,并使用 ddeServer.connect() 连接到它。

我们还定义了 sendCommand() 函数,该函数将获取命令并使用 sendDDEMessage() 函数将其发送到 DDE 服务端。然后,它将获取 DDE 服务端的响应并将其设置为第二个文本框中的值。

最后,我们监听按钮上的单击事件,并在单击时调用 sendCommand() 函数。

结论

在本文中,我们了解了什么是 DDE 和如何使用 JavaScript 和 ActiveXObject 对象实现它。虽然这种方法不是最佳的,但它可以让我们学习如何在 JavaScript 中使用 ActiveXObject 对象和 Windows API,以及如何进行应用程序和操作系统级别的操作。

The above is the detailed content of Explore how to implement DDE using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is useEffect? How do you use it to perform side effects?What is useEffect? How do you use it to perform side effects?Mar 19, 2025 pm 03:58 PM

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Explain the concept of lazy loading.Explain the concept of lazy loading.Mar 13, 2025 pm 07:47 PM

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?Mar 18, 2025 pm 01:44 PM

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

How does currying work in JavaScript, and what are its benefits?How does currying work in JavaScript, and what are its benefits?Mar 18, 2025 pm 01:45 PM

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

How does the React reconciliation algorithm work?How does the React reconciliation algorithm work?Mar 18, 2025 pm 01:58 PM

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

How do you connect React components to the Redux store using connect()?How do you connect React components to the Redux store using connect()?Mar 21, 2025 pm 06:23 PM

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

What is useContext? How do you use it to share state between components?What is useContext? How do you use it to share state between components?Mar 19, 2025 pm 03:59 PM

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

How do you prevent default behavior in event handlers?How do you prevent default behavior in event handlers?Mar 19, 2025 pm 04:10 PM

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool